Ruby on rails Rails/Dragonfly:如何更新像图片一样的魔术柱
我在rails应用程序中使用dragonfly处理图像附件。 我在模型中使用了magic列image_width和image_height。 这很有效。现在我在模型中获得了一些带有Ruby on rails Rails/Dragonfly:如何更新像图片一样的魔术柱,ruby-on-rails,ruby,dragonfly-gem,Ruby On Rails,Ruby,Dragonfly Gem,我在rails应用程序中使用dragonfly处理图像附件。 我在模型中使用了magic列image_width和image_height。 这很有效。现在我在模型中获得了一些带有image\u uid的图像,可以访问图像,但是没有设置image\u width和image\u height。(发生在简单的蜻蜓预览插件上) 现在我如何强制重新计算这些值?所以在模型中类似这样的东西: before_save :update_image_fields def update_image_fields
image\u uid
的图像,可以访问图像,但是没有设置image\u width
和image\u height
。(发生在简单的蜻蜓预览插件上)
现在我如何强制重新计算这些值?所以在模型中类似这样的东西:
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
您可以使用ImageMagic分析器计算图像的尺寸 鉴于您已经配置了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
调用只使用了magic列,我一直得到空维度。根据它应该调用Imagemagick的identify
命令来获得宽度和高度当我调试时,它调用“确保使用缓存的\u magic\u属性”,这当然会发现我定义了魔法属性。@Meier请查看我的更新答案。您应该使用photo.image.analysis(:width)