jon
jon

Reputation: 1581

how to set height only when resizing using ImageMagick

The following code resizes an image so that its width is 200 and its height is whatever... How would I do it so that the height was 200 and the width was whatever...

$command = MAGICK_PATH."convert ".$filename." -resize 200 ".$filename; exec($command);

Kind regards J

Upvotes: 5

Views: 4784

Answers (2)

ghostmansd
ghostmansd

Reputation: 3455

You can get width and height of the input image and then convert it. I'm not familiar with PHP so I'll post code for bash.

To change size of image to 500x200:

convert "$filename" -resize 500x200 "$filename"

To change size, while keep width or height old:

width=$(identify -format "%w" "$filename")
height=$(identify -format "%h" "$filename")
convert "$filename" -resize "200x$height" "$filename"

Note: instead of using

convert "$filename" [opts] "$filename"

it's better to use

mogrify "filename"

Upvotes: 0

Pekka
Pekka

Reputation: 449385

The manual has a complete overview over resizing options.

You are looking for

xheight Height given, width automagically selected to preserve aspect ratio.

Upvotes: 10

Related Questions