使用PHP无法调整图像大小


Image cannot resize using PHP

我正在尝试调整从Flickr复制的图像的大小。但我似乎得到了原来的尺寸。这是我的代码:

$img = Input::get('FlickrUrl');
$filename = gmdate('Ymdhis', time());
copy($img, $_SERVER["DOCUMENT_ROOT"].'/upload/'.$filename.'.jpeg');
$newImg = '/upload/'.$filename.'.jpeg';
list($CurWidth, $CurHeight) = getimagesize($_SERVER["DOCUMENT_ROOT"].$newImg);
$width = $CurWidth;
$height = $CurHeight;
$image_ratio = $CurWidth / $CurHeight;
//resize image according to container
$container_width = 300;
$container_height = 475;
if($CurWidth > $container_width)
{
    $CurWidth = $container_width;
    $CurHeight = $CurWidth / $image_ratio;
}
if($CurHeight > $container_height)
{
    $CurHeight = $container_height;
    $CurWidth = $CurHeight * $image_ratio;
}
if($CurWidth < $container_width)
{
    $CurWidth = $container_width;
    $CurHeight = $CurWidth / $image_ratio;
}
if($CurHeight < $container_height){
    $CurHeight = $container_height;
    $CurWidth = $CurHeight * $image_ratio;
}
$img_orginal = $_SERVER["DOCUMENT_ROOT"].'/upload/'.$filename.'.jpeg';
$img_org = ImageCreateFromJPEG($img_orginal);
$NewCanves  = imagecreatetruecolor($CurWidth, $CurHeight);
imagecopyresized($NewCanves, $img_org, 0, 0, 0, 0, $CurWidth, $CurHeight, $width, $height);
$finalImg = '/upload/'.$filename.'.jpeg';

return Response::json(["success"=>"true", "images"=>$finalImg, "width"=>$CurWidth, "height"=>$CurHeight]);

首先,我从URL复制图像,将其保存在服务器中,然后尝试调整其大小。无法理解此代码出了什么问题。

这里的问题是你没有保存你的文件。之后:

imagecopyresized($NewCanves, $img_org, 0, 0, 0, 0, $CurWidth, $CurHeight, $width, $height);
$finalImg = '/upload/'.$filename.'.jpeg'

您应该添加:

imagejpeg($NewCanves, $finalImg);

将其保存在文件系统中

尝试具有强大Laravel集成的干预/图像包:

// open an image file
$img = Image::make('FlickrUrl');
// now you are able to resize the instance
$img->resize($container_width, $container_height);
// finally we save the image as a new file
$img->save('/upload/'.$filename.'.jpeg');