Php |在不拉伸的情况下裁剪图像
如何使用Php裁剪图像而不是拉伸图像 例如,如果我上传一个600100的图像,我希望它在图像的中心将其裁剪成100x100。从左边算起250像素。 显然,这些数字取决于用户上传的图像 这是我目前的代码:Php |在不拉伸的情况下裁剪图像,php,Php,如何使用Php裁剪图像而不是拉伸图像 例如,如果我上传一个600100的图像,我希望它在图像的中心将其裁剪成100x100。从左边算起250像素。 显然,这些数字取决于用户上传的图像 这是我目前的代码: $width = imagesx($base64); $height = imagesy($base64); $tmp = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($tmp, $base64, 0,
$width = imagesx($base64);
$height = imagesy($base64);
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $base64,
0, 0,
0, 0,
$newWidth, $newHeight,
$width, $height);
imagejpeg($tmp, $path)
如果我没有弄错的话,这段代码将拍摄一张600x100的图像,并将其向下延伸到100x100。反对修剪。请尝试使用:
您需要设置$uploadedfile、$CROPT\U WITH、$CROPT\U height和$filename变量的值
$new = imagecreatefromjpeg($uploadedfile);
$crop_width = imagesx($new);
$crop_height = imagesy($new);
$size = min($crop_width, $crop_height);
if($crop_width >= $crop_height) {
$newx= ($crop_width-$crop_height)/2;
$im2 = imagecrop($new, ['x' => $newx, 'y' => 0, 'width' => $size, 'height' => $size]);
}
else {
$newy= ($crop_height-$crop_width)/2;
$im2 = imagecrop($new, ['x' => 0, 'y' => $newy, 'width' => $size, 'height' => $size]);
}
imagejpeg($im2,$filename,90);