如何验证attachment_fu的图像宽度和高度?
-
20-08-2019 - |
题
我希望能够验证图像是否具有一定的高度或是否是正方形。
在模型的验证块中 has_attachment
, ,当我尝试访问时 image_size
, width
, , 或者 height
, ,它总是显示为空。
我也问了这个问题 这里 如果您想了解更多详细信息。
解决方案
是啊,你需要破解了一下,以得到它的工作,但没有这么多。从attachment_fu自己的图像处理器适应:
validate :validate_image_size
private
def validate_image_size
w, h = width, height
unless w or h
with_image do |img|
w, h = img.columns, img.rows
end
end
errors.add(:width, "must less than 250px") if w > 250
errors.add(:height, "must less than 250px") if h > 250
end
end
其他提示
您已经采取了看看迷你magick?
您可以克隆它的git从这里:
http://github.com/probablycorey/mini_magick/tree/master
如果您需要了解git的,请查看以下链接:
http://git.or.cz/course/svn.html (崩溃当然与GIT,相比颠覆)
http://github.com/guides/git-screencasts (GitHub的截屏)
这是围绕ImageMagick的功能红宝石包装(不确定,如果attachment_fu使用这个内部),但它绝对是突飞猛进比RMagick更好(RMagick极其臃肿,大量的内存问题)。 Anywho,迷你magick会让你做所有你需要的东西,然后一些。看看上面的链接GitHub的上列出的自述,这会给你如何使用它的破败。
这里的一个片段:
#For resizing an image
image = MiniMagick::Image.from_file("input.jpg")
image.resize "100x100"
image.write("output.jpg")
#For determining properties of an image...
image = MiniMagick::Image.from_file("input.jpg")
image[:width] # will get the width (you can also use :height and :format)
我认为您缺少应安装的必备 gems 以便使用attachment_fu 调整图像大小。我曾经使用attachment_fu插件,它依赖于以下gems
rmagick-2.11.0
image_science-1.2.0
确保您已经安装了上面的 gem,并更改了 has_attachment 中的宽度和高度,然后您就可以看到更改。
祝你好运 !