如何使用imagemagick convert进行缩放和裁剪?
给定以下PHP代码:如何使用imagemagick convert进行缩放和裁剪?,imagemagick,Imagemagick,给定以下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['hei
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
命令中执行此操作?我相信答案是convert“$input”-resize“${width}x${height}^”-重心-裁剪“${width}x${height}+0+0”$output
,您可以使用-extent
:转换$input-调整$widthx$height^-重心-extent$widthx$height$output
@mivk您应该回答这个问题。它对我有效,而被接受的答案没有。顺便说一句,你可以根据需要添加-背景白色
或-背景黑色
。