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

时间:2011-02-11 14:59:29

标签: php image resize crop

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

enter image description here

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

$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']

但我得到的是:

enter image description here

任何人都看到了,我做错了什么?

谢谢,迈克。

4 个答案:

答案 0 :(得分:4)

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

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

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

$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);

答案 1 :(得分:1)

尝试

<?php

$dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']);

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height);

答案 2 :(得分:0)

好的,我自己发现了问题,代码应该是这样的:

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 

答案 3 :(得分:0)

还有imagecrop功能,可让您通过xywidthheight传入数组值。

相关问题