如何使用图像魔术转换进行缩放和裁剪



给定以下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命令中执行此操作?

我相信

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

最新更新