简体   繁体   中英

php image upload from url and resize

i have this upload form and i want to resize image when uploaded its upload form from url i did it from file upload but cant for url upload can anyone help me?

<?php
    if($_POST["sub"]){
        $url = trim($_POST["url"]);
        if($url){
            $file = fopen($url,"rb");
            if($file){
                $directory = "../images/news/";
                $valid_exts = array("jpg","jpeg","gif","png");
                $ext = substr(basename($url), strrpos(basename($url), '.'));
                $ext = str_replace('.','',$ext);
                if(in_array($ext,$valid_exts)){
                    $rand = rand(0,9999999999);
                    $filename = $rand.'.'.$ext;
                    $newfile = fopen($directory . $filename, "wb");
                    if($newfile){
                        while(!feof($file)){
                            fwrite($newfile,fread($file,1024 * 8),1024 * 8);
                        }
                        echo ''."";
                        echo ''.$filename.'';
                    } else { echo 'Could not establish new file ('.$directory.$filename.') on local server. Be sure to CHMOD your directory to 777.'; }
                } else { echo 'Invalid file type. Please try another file.'; }
            } else { echo 'Could not locate the file: '.$url.''; }
        } else { echo 'Invalid URL entered. Please try again.'; }
    }
?>

For saving the file from URL:

If you have allow_url_fopen set to true:

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));

Else use cURL :

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

This is the same as the answer in this post...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM