How to scale and crop using imagemagick convert?

Given the following PHP code:

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); } } 

To place it in English, we first resize, keeping the aspect ratio so that the smaller edge of the image becomes the desired size, then the resulting image is cropped along the longer edge to $width X $height with equal amounts cut on each (the smaller side does not need cropped).

Is it possible to do this in a single convert command?

+6
source share
1 answer

I believe the answer is convert "$input" -resize "${width}x${height}^" -gravity center -crop "${width}x${height}+0+0" $output .

+18
source

Source: https://habr.com/ru/post/953693/


All Articles