裁剪扭曲/缩放图像

时间:2012-11-17 05:27:57

标签: php image gd crop image-resizing

我有以下PHP代码:

$w = 300; // Width of new image
$h = 300; // Height of new image
$oh = 540; // Original file height
$ow = 720; // Original file width
$x = 196;
$y = 50;

$image = imagecreatefromjpeg('fileToCrop.jpg');
$cropped_image = imagecreatetruecolor($w, $h);
imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $ow, $oh, $w, $h);

imagejpeg($cropped_image, 'fileToCrop.jpg', 100);

想要剪裁图像,但我的图像比原始图像扭曲/更高,例如:

原始

enter image description here

裁剪(“不”表示“不”:

enter image description here

我看不出我的代码出了什么问题,图片发生了什么变得更大......

2 个答案:

答案 0 :(得分:1)

您反转了imagecopyresampled

的最后4个参数
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

将其更改为

imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $w, $h, $ow, $oh);

但事实上,你确定你不是在寻找裁剪区域的直接副本吗?

imagecopy($cropped_image, $image, 0, 0, $x, $y, $ow, $oh);

答案 1 :(得分:0)

因为你省略了imagejpeg()的质量,它默认为75% http://php.net/manual/en/function.imagejpeg.php

你可以尝试在第三个参数上添加95吗?

imagejpeg($cropped_image, 'fileToCrop.jpg', 95);

P.S。 如果你可以使用第三方PHP脚本,我建议你只使用timthumb http://www.binarymoon.co.uk/projects/timthumb/

如果您需要更改裁剪位置,

http://www.binarymoon.co.uk/2010/08/timthumb-part-4-moving-crop-location/

相关问题