1

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

4

2 回答 2

1

这里的问题是您不保存文件。后:

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

你应该添加:

imagejpeg($NewCanves, $finalImg);

将其保存在文件系统中

于 2015-04-03T20:21:14.113 回答
1

尝试具有出色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');    
于 2015-04-04T14:43:17.870 回答