以下是PHP代码:
function image_scale_and_crop(stdClass $image, $width, $height) {
$scale = max($width / $image->info['width'], $height / $image->info['height']);
$x = ($image->info['width'] * $scale - $width) / 2;
$y = ($image->info['height'] * $scale - $height) / 2;
if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
return image_crop($image, $x, $y, $width, $height);
}
}
简单来说,我们首先按比例调整大小,使图像的较小边缘变为所需大小,然后沿着较长的边缘剪裁出一个尺寸为$width X $height
的图像,每一侧都要等量剪裁(较小的一侧不需要剪裁)。
是否可以在一个convert
命令中完成这个操作?
-extent
参数:convert $input -resize $widthx$height^ -gravity center -extent $widthx$height $output
。该命令用于调整图像大小并添加背景以填充任何空白区域。 - mivk