我正在尝试调整从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复制Image,将其保存在我的服务器中,然后尝试调整它的大小。 无法理解这段代码的错误。
答案 0 :(得分:1)
此处的问题是您不保存文件。后:
imagecopyresized($NewCanves, $img_org, 0, 0, 0, 0, $CurWidth, $CurHeight, $width, $height);
$finalImg = '/upload/'.$filename.'.jpeg'
你应该添加:
imagejpeg($NewCanves, $finalImg);
将其保存在文件系统
中答案 1 :(得分:1)
尝试intervention/image包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');