4

In web apps, it's good practice to use the width and height attributes for img tags -- this allows the browser to render the page faster.

If I have the width and height of an image, and a imagemagick format string (i.e. '300>'), how can I get the size that that image would be resized to? I'm hoping that imagemagick has some hooks / functions that would allow me to get access to this information.

I don't want to resize or load the image into memory, I just want to know what the dimensions would be.

Essentially, I want the following function:

def get_image_size(width, height, format_string)
  # ... magic here
  return [new_width, new_height]
end

width, height = get_image_size(300, 400, '235x235!')
# would return 235 and 235

width, height = get_image_size(300, 400, '700x700>')
# would return 300 and 400

width, height = get_image_size(300, 400, '100x100')
# would return 75 and 100
Joe Van Dyk
  • 6,828
  • 8
  • 57
  • 73
  • 2
    The obvious solution seems to take the spec (http://imagemagick.org/script/command-line-processing.php#geometry) and implement the function on your own, looks not too hard. Perhaps someone is willing to invest some hours of his spare time for you if you increase the bounty. Or you go directly to vworker.com and ask someone to do this for 50 bucks or less. – Doc Brown Oct 12 '11 at 17:00

1 Answers1

3

Using the following ImageMagick convert command gives what you want. This should easily translate to whichever ImageMagick language API you choose.

$ convert -size 300x400 xc:white -resize '235x235!' info:- | cut -d\  -f3
235x235
$ convert -size 300x400 xc:white -resize '700x700>' info:- | cut -d\  -f3
300x400
$ convert -size 300x400 xc:white -resize '100x100' info:- | cut -d\  -f3
75x100
Go Dan
  • 15,194
  • 6
  • 41
  • 65