8

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

4

1 回答 1

21

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

于 2013-09-12T11:32:38.917 回答