加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Carrierwave图像尺寸

发布时间:2020-12-17 04:38:32 所属栏目:百科 来源:网络整理
导读:如何获得当前载波实例的宽度和高度? 像这样的东西: 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无效. 我需要指定图像的宽度和
如何获得当前载波实例的宽度和高度?

像这样的东西:

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-dimensions和 https://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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读