如何使用php从远程服务器下载,调整大小和存储图像? 这是我正在使用的代码
$temp_image = file_get_contents($url);
$image = imagecreatefromstring($temp_image);
$thumb = imageToCanvas($image,100,75,true);
imagejpeg($thumb,$base_image_path . $thumb_path,90)
function imageToCanvas($_image, $_canvasWidth, $_canvasHeight, $forceScale=false,$x=false,$y=false)
{
$newImage = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
$imageinfo = getimagesize($_image);
$sourceWidth = $imageinfo[0];
$sourceHeight = $imageinfo[1];
$sourceImage = openImage($_image);
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $_canvasWidth, $_canvasHeight, $sourceWidth, $sourceHeight);
return $newImage;
}
function openImage($file)
{
// *** Get extension
$extension = strtolower(strrchr($file, '.'));
switch($extension) {
case '.jpg': case '.jpeg':
$img = @imagecreatefromjpeg($file);
break;
case '.gif':
$img = @imagecreatefromgif($file);
break;
case '.png':
$img = @imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
不起作用,我不知道为什么。
$sourceWidth
& $sourceHeight
没有值,所以我认为$image
格式错误
谢谢!
答案 0 :(得分:0)
如果您使用的是linux服务器并安装了ghostscript,这是一种更简单的方法
shell_exec("convert in.jpg -resize 100x75 out.jpg")
答案 1 :(得分:0)
php中没有名为openImage
的函数,如果你自己没有定义它就会出问题。
如果你确定了它,它看起来是什么样的,你收到任何错误吗?
编辑:根据您的评论,问题似乎是您将openImage
函数的输入参数视为文件路径。但是,当你调用它时,你正在为它提供一个图像资源,即imagecreatefromstring
的结果。