【问题标题】:Image cannot resize using PHP图像无法使用 PHP 调整大小
【发布时间】:2015-06-08 20:25:17
【问题描述】:

我正在尝试调整从 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 复制图像,将其保存在我的服务器中,然后尝试调整它的大小。 无法理解这段代码有什么问题。

【问题讨论】:

  • 您可以通过将代码减少到最低限度来重现您的问题来帮助潜在的答案。在此过程中,您甚至会发现您可以自己解决问题。

标签: php laravel image-processing gd2


【解决方案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');    

【讨论】:

  • 是的,我用过 :) 谢谢你的建议。
【解决方案2】:

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

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

你应该添加:

imagejpeg($NewCanves, $finalImg);

将其保存在文件系统中

【讨论】:

  • 它仍然给我同样的大小。
猜你喜欢
  • 2012-02-28
  • 1970-01-01
  • 2014-09-21
  • 2020-03-03
  • 2013-08-21
  • 2012-08-12
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
相关资源
最近更新 更多