[英]php image upload from url and resize
我有此上传表单,我想从url上传其上传表单时调整图像大小,但我是从文件上传完成的,但无法进行url上传,有人可以帮助我吗?
<?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.'; }
}
?>
使用GD或Image Magic库调整图像大小:
http://php.net/manual/en/imagick.resizeimage.php http://php.net/manual/en/function.imagecopyresized.php
要从URL保存文件:
如果将allow_url_fopen设置为true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
否则使用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);
这是一样的,在回答这个帖子...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.