2011-02-11 119 views
4

我想使用imagecreatetruecolor裁剪图像,并且它总是裁剪出黑色空格,或者缩放太大。我希望图像的宽度为191像素,高度为90像素,因此我还需要调整图像大小和裁剪大小,因为比例必须保持不变。那么,有该项目的一些样品:PHP - 使用imagecopyresampled()裁剪图像?

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 [ '庄稼']阵列包括:

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

但我得到的是:

enter image description here

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

谢谢,迈克。

回答

0

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

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 
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); 
+0

试用身份证,我得到的只是一个黑色的图像。 =/ – Mike 2011-02-11 15:09:56

3

你可以做到这一点,我自己做了,而且它的工作原理

(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); 
+0

这一个作品! – 2014-10-20 22:10:36

0

另外还有imagecrop功能,它允许你在一个数组传递与xywidthheight值。