1

我希望能够验证图像是否完全具有一定的高度或一定的高度,或者它是否是正方形的。

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

如果您想了解更多详细信息,我还在这里问了这个问题。

4

4 回答 4

4

是的,你需要破解一下才能让它工作,但不是那么多。改编自 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
于 2009-08-12T14:01:29.930 回答
0

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

尽管如此,对于大多数 Web 框架,我认为这是使用图像魔术的标准方法。尝试识别功能。.

于 2009-02-01T03:03:22.503 回答
0

你看过迷你魔术吗?

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

这是一个片段:

#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)
于 2009-02-01T04:49:43.257 回答
0

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

  1. rmagick-2.11.0

  2. image_science-1.2.0

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

祝你好运 !

于 2009-08-13T08:39:26.693 回答