PHP – 使用imagecopyresampled()裁剪图像?

我想使用imagecreatetruecolor裁剪图像,它总是裁剪它留下黑色空间,或者缩放太大.我希望图像精确到191px宽和90px高,所以我还需要调整图像大小以及裁剪,因为必须保持比例.那么,有一些项目的样本:

调整大小脚本(简化)如下所示:

$src_img=imagecreatefromjpeg($photoTemp);    
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);

$newImage [‘crop’]数组包括:

['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']

但我得到的是:

有人看到,我做错了什么?

谢谢,迈克.

解决方法:

你也可以这样做,我自己也做了,而且它有效

(X1,Y1)=&GT作物开始的地方

(X2,Y2)=&GT作物结束的地方

$filename = $_GET['imageurl'];
$percent = 0.5;

list($width, $height) = getimagesize($filename);

$new_width  = $_GET['x2'] - $_GET['x1'];
$new_height = $_GET['y2'] - $_GET['y1'];

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height);

// Outputs the image
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
上一篇:BLOB-PHP调整图片大小


下一篇:如何让java的ImageBuffer正确读取PNG文件?