Reputation: 173
I'm trying to crop my image, but when I do it shows up as the right size, but all black. I've tried at least a dozen different scripts but I can't seem to get any of them to work :(
Oh and the rotation script works fine, and all of the echos are just for testing and will be removed :D
<?php
$staffID = $_POST['u'];
$actCode = $_POST['a'];
$tempAvatar = $_POST['tempAvatar'];
$x1 = $_POST['x'];
$y1 = $_POST['y'];
$wH = $_POST['w'];
$scale = $_POST['scale'];
$angle = $_POST['angle'];
$destFolder = "../../../avatars/";
$imagePath = "tempAvatars/".$tempAvatar.".jpg";
$imagePathRot = "tempAvatars/".$tempAvatar."ROTATED.jpg";
$imagePathCropped= "tempAvatars/".$tempAvatar."CROPPED.jpg";
echo 'X1: '.$x1.'<br>Y1: '.$y1.'<br>Width/Height: '.$wH.'<br>Angle: '.$angle;
if ($angle != 0) {
$source = imagecreatefromjpeg($imagePath) or notfound();
$rotate = imagerotate($source,$angle,0);
imagejpeg($rotate, $imagePathRot);
$imagePath = $imagePathRot;
}
echo '<br>X2: '.$x2.'<br>Y2: '.$y2;
$targ_w = 300;
$jpeg_quality = 90;
$img_r = imagecreatefromjpeg($imagePath);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_w );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['w']);
imagejpeg($dst_r, $imagePathCropped, $jpeg_quality);
echo '<br><img src="'.$imagePathCropped.'">';
?>
Upvotes: 3
Views: 243
Reputation: 7896
Your problem is that $targ_h
is not defined, therefore you are copying 0 pixel "rows" from the source image. It's in the correct size because it's decided by ImageCreateTrueColor
and of course initialized to black. The correct call according to the rest of your code should be:
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_w,$_POST['w'],$_POST['w']);
Upvotes: 2