ruby-on-rails – Carrierwave图像尺寸

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Carrierwave图像尺寸前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何获得当前载波实例的宽度和高度?

像这样的东西:

car_images.each do | image|
  image_tag( image.photo_url,:width => image.photo_width,:height => image.photo_height)
end

不幸的是,image.photo_width和image.photo_height无效.
我需要指定图像的宽度和高度,这是我正在使用的jquery插件所必需的.

解决方法

结合 https://github.com/jnicklas/carrierwave/wiki/How-to:-Get-version-image-dimensionshttps://github.com/jnicklas/carrierwave/wiki/How-to:-Store-the-uploaded-file-size-and-content-type,你会得到:
class Image
  before_save :update_image_attributes

  private

  def update_image_attributes
    if image.present?
      self.content_type = image.file.content_type
      self.file_size = image.file.size
      self.width,self.height = `identify -format "%wx%h" #{image.file.path}`.split(/x/)
      # if you also need to store the original filename:
      # self.original_filename = image.file.filename
    end
  end
end
原文链接:https://www.f2er.com/ruby/269632.html

猜你在找的Ruby相关文章