Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php |在不拉伸的情况下裁剪图像_Php - Fatal编程技术网

Php |在不拉伸的情况下裁剪图像

Php |在不拉伸的情况下裁剪图像,php,Php,如何使用Php裁剪图像而不是拉伸图像 例如,如果我上传一个600100的图像,我希望它在图像的中心将其裁剪成100x100。从左边算起250像素。 显然,这些数字取决于用户上传的图像 这是我目前的代码: $width = imagesx($base64); $height = imagesy($base64); $tmp = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($tmp, $base64, 0,

如何使用Php裁剪图像而不是拉伸图像

例如,如果我上传一个600100的图像,我希望它在图像的中心将其裁剪成100x100。从左边算起250像素。 显然,这些数字取决于用户上传的图像

这是我目前的代码:

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