如何使用imagemagick转换进行缩放和裁剪?

chx*_*chx 7 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['height'] * $scale - $height) / 2;

  if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
    return image_crop($image, $x, $y, $width, $height);
  }
}
Run Code Online (Sandbox Code Playgroud)

换句话说,首先我们调整大小,保持宽高比,使图像的较小边缘成为所需的大小,然后沿着较长的边缘裁剪得到的图像$width X $height,每边切割的数量相等(较小的一面赢了)不需要裁剪.

是否可以在一个convert命令中执行此操作?

chx*_*chx 19

我相信答案是convert "$input" -resize "${width}x${height}^" -gravity center -crop "${width}x${height}+0+0" $output.

  • 或者,您可以使用 `-extent` : `convert $input -resize $widthx$height^ -gravity center -extent $widthx$height $output` (3认同)