简体   繁体   中英

Cropping image in PHP imagecopyresampled - squashing instead of cropping

I am using jCrop to crop an image. This code was working in another page before, but when I implemented it for a different page it seems to be playing up.

When I run this, the code pics up the original image, squashes it down and then fits the entire image into the box I was intending on cropping to.

The values are coming back from jCrop correctly in the $_POST values.

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

You can simply use imagecopy() . Something like...

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

Of course, you'll also want to check for out of bounds conditions and handle them appropriately. Not sure why you're setting and / or using $targ_w and $targ_h if you're getting cropping data from the $_POST array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM