Is there a simple way to get image dimensions in Ruby?

前端 未结 8 944
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 06:57

I\'m looking for an easy way to get width and height dimensions for image files in Ruby without having to use ImageMagick or ImageScience (running Snow Leapard).

8条回答
  •  天涯浪人
    2020-12-14 07:07

    There's a handy method in the paperclip gem:

    >> Paperclip::Geometry.from_file("/path/to/image.jpg")
    => 180x180
    

    This only works if identify is installed. If it isn't, if PHP is installed, you could do something like this:

    system(%{php -r '$w = getimagesize("#{path}"); echo("${w[0]}x${w[1]}");'})
    # eg returns "200x100" (width x height)
    

提交回复
热议问题