【问题标题】:Rails/Dragonfly: how to update magic colums like image_widthRails/Dragonfly:如何更新像 image_width 这样的魔法列
【发布时间】:2015-09-22 15:04:09
【问题描述】:

我使用蜻蜓来处理我的 rails 应用程序中的图像附件。 我在模型中使用了神奇的列 image_width 和 image_height。 这很好用。现在我在模型中得到了一些带有image_uid 的图像,图像是可访问的,但是image_widthimage_height 没有设置。 (它发生在 simple_dragonfly_preview 插件上) 现在我该如何强制重新计算值?所以在模型中是这样的:

before_save :update_image_fields

def update_image_fields
  logger.info "Image size update"
  if image_uid.present?
    self.image_width = image.width # what to call here?
    self.image_height = image.height
   end
 end

【问题讨论】:

    标签: ruby-on-rails ruby dragonfly-gem


    【解决方案1】:

    您可以使用 ImageMagic 分析器 http://markevans.github.io/dragonfly/imagemagick/#analysers 计算图像的尺寸。

    鉴于您已经配置了 ImageMagick 插件,重新计算图像高度和宽度的代码如下所示:

    Photo.where(image_width:nil).each do |photo|
      photo.image_width = photo.image.analyse(:width)
      photo.image_height = photo.image.analyse(:height)
      photo.save
    end
    

    【讨论】:

    • 看起来image.width 调用只是使用了魔法列,我不断得到空维度。
    • 根据github.com/markevans/dragonfly/blob/master/lib/dragonfly/…应该调用Imagemagick的identify命令来获取宽度和高度
    • 我debug的时候调用"ensure_uses_cached_magic_attributes",当然发现我定义了magic属性。
    • @Meier 查看我的更新答案。你应该使用photo.image.analyse(:width)
    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多