【问题标题】:How do you validate attachment_fu's image width and height?你如何验证 attachment_fu 的图像宽度和高度?
【发布时间】:2009-02-01 02:43:33
【问题描述】:

我希望能够验证图像是否完全符合某个高度或某个高度,或者它是否为正方形。

has_attachment的模型的验证块中,当我尝试访问image_sizewidthheight时,总是显示为空。

如果您想了解更多详细信息,我还问了问题here

【问题讨论】:

  • 很抱歉有一段时间没有回复 - 我没有通过电子邮件检查那个东西来更新我。

标签: ruby-on-rails ruby image attachment-fu


【解决方案1】:

是的,你需要稍微修改一下才能让它工作,但不是那么多。改编自 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

【讨论】:

  • 感谢马科斯,这看起来很有希望。我试试看!
  • 谢谢。请注意,这与图像处理器略有不同。对于 MiniMagick,它是 img[:width]img[:height] 而不是 img.columnsimg.rows
【解决方案2】:

您尚未指定正在使用的语言和系统。

不过,对于大多数 Web 框架,我认为这是使用 image magic 的标准方法。试试identify function.

【讨论】:

  • 啊,对不起。我在 Ruby on Rails 中使用 attachment_fu 插件。我使用迷你魔法作为我的处理器。但是,根据您的评论,我似乎需要深入研究 attachment_fu。
【解决方案3】:

你看过迷你魔术吗?

你可以从这里 git clone 它:

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 函数的 ruby​​ 包装器(不确定 attachment_fu 是否在内部使用它),但它绝对比 RMagick 更好(RMagick 非常臃肿,有很多内存问题)。 Anywho, mini-magick 会让你做你需要做的所有事情,然后做一些事情。查看上面 github 链接中列出的 README,它会为您提供如何使用它的纲要。

这是一个sn-p:

#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)

【讨论】:

    【解决方案4】:

    我认为您缺少应该安装的必备 gem,以便使用 attachment_fu 调整图像大小。我使用过依赖于以下 gem 的 attachment_fu 插件

    1. rmagick-2.11.0

    2. image_science-1.2.0

    确保您已经安装了上面的 gems 并更改了 has_attachment 中的宽度和高度,然后您就可以看到更改了。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 2010-11-07
      • 2019-07-25
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多