2012-09-19 58 views
0

我正在使用jCrop裁剪图像。这段代码之前在另一个页面中工作,但是当我为不同的页面实现它时,它似乎在起作用。在PHP中裁剪图像imagecopyresampled - 压扁而不是裁剪

当我运行这个时,代码拍摄了原始图像,压扁它,然后将整个图像放入我打算修剪的盒子中。

这些值正确地从jCrop中返回$ _POST值。

$origURL=$_POST['CelebrityPicURL']; 
$x = $_POST['x']; 
$y = $_POST['y']; 
$w = $_POST['w']; 
$h = $_POST['h']; 

$targ_w = $targ_h = 300; 
$jpeg_quality = 90; 

$img_r = imagecreatefromjpeg($origURL); 
$dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], 
$targ_w,$targ_h,$_POST['w'],$_POST['h']); 


imagejpeg($dst_r,$origURL,$jpeg_quality); 

回答

0

您可以简单地使用imagecopy()。就像...

$dst_r = imagecreatetruecolor((int) $w, (int) $h); 
imagecopy($dst_r, $img_r, 0, 0, (int) $x, (int) $y, (int) $w, (int) $h); 

当然,你也想检查越界条件并适当地处理它们。不知道你为什么设置和/或使用$targ_w$targ_h如果你从$_POST阵列中裁剪数据。