【问题标题】:How to scale-and-crop with imagemagick convert?如何使用 imagemagick convert 进行缩放和裁剪?
【发布时间】:2013-09-16 17:18:13
【问题描述】:

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

【问题讨论】:

    标签: imagemagick


    【解决方案1】:

    我相信答案是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
    • @mivk 您应该将此作为答案。它对我有用,而接受的答案却没有。
    • 顺便说一句,您可以根据需要添加-background white-background black
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 2015-06-20
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2015-01-21
    相关资源
    最近更新 更多