[英]Rotate and then crop image with PHP
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 哦,旋转脚本工作正常,所有的回声只是用于测试,将被删除: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.'">';
?>
Your problem is that $targ_h
is not defined, therefore you are copying 0 pixel "rows" from the source image. 您的问题是
$targ_h
未定义,因此您正在从源图像复制0像素“行”。 It's in the correct size because it's decided by ImageCreateTrueColor
and of course initialized to black. 它的大小正确,因为它由
ImageCreateTrueColor
决定,当然初始化为黑色。 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']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.