Ruby on Rails - Carrierwave get the image dimensio

2020-07-23 08:50发布

I need to know the width and height of the current instance of carrierwave

For example:

image_tag image.photo_url,:width => image.photo.width, :height => image.photo.height

But the problem is that image.photo.width and image.photo.height are not working.

Any ideas ?

1条回答
Rolldiameter
2楼-- · 2020-07-23 09:23

You have to install ImageMagick, RMagick or MiniMagick. Then you open the file (image) before you can find out its height.

Example: https://github.com/jnicklas/carrierwave#conditional-versions (see the is_landscape? method), copied here:

  def is_landscape? picture
    image = MiniMagick::Image.open(picture.path)
    image[:width] > image[:height]
  end

Or this other related question:

Carrierwave and mini_magick finding widths & height

查看更多
登录 后发表回答