图片无法使用 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);
将其保存在文件系统中
尝试 intervention/image package with great Laravel integration:
// 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');
我正在尝试调整从 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);
将其保存在文件系统中
尝试 intervention/image package with great Laravel integration:
// 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');