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

use*_*922 2 gem bundle ruby-on-rails ruby-on-rails-3 carrierwave

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

例如:

image_tag image.photo_url,:width => image.photo.width, :height => image.photo.height
Run Code Online (Sandbox Code Playgroud)

但问题是image.photo.width和image.photo.height不起作用.

有任何想法吗 ?

ron*_*chn 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
Run Code Online (Sandbox Code Playgroud)

或者这个其他相关的问题:

Carrierwave和mini_magick查找宽度和高度