Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails/Dragonfly:如何更新像图片一样的魔术柱_Ruby On Rails_Ruby_Dragonfly Gem - Fatal编程技术网

Ruby on rails Rails/Dragonfly:如何更新像图片一样的魔术柱

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

我在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
  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)