Ruby on Rails - Carrierwave get the image dimension width and height

这一生的挚爱 提交于 2019-12-06 12:01:09

问题


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:


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



来源:https://stackoverflow.com/questions/12022653/ruby-on-rails-carrierwave-get-the-image-dimension-width-and-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!