Ruby on Rails - Carrierwave获取图像尺寸宽度和高度

时间:2012-08-18 22:21:08

标签: ruby-on-rails ruby-on-rails-3 gem bundle carrierwave

我需要知道当前载波实例的宽度和高度

例如:

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

但问题是image.photo.width和image.photo.height无效。

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

您必须安装ImageMagick,RMagick或MiniMagick。然后打开文件(图像),然后才能找到它的高度。

示例:https://github.com/jnicklas/carrierwave#conditional-versions(请参阅is_landscape?方法),复制到此处:

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

或其他相关问题:

Carrierwave and mini_magick finding widths & height