我希望能夠驗證圖像是完全確定的或具有一定的高度,或者如果它是方形的。你如何驗證attachment_fu的圖像寬度和高度?
在模型has_attachment
,的驗證模塊,當我嘗試訪問image_size
,width
,或height
,它永遠是空。
我還問了here如果你想了解更多的細節。
我希望能夠驗證圖像是完全確定的或具有一定的高度,或者如果它是方形的。你如何驗證attachment_fu的圖像寬度和高度?
在模型has_attachment
,的驗證模塊,當我嘗試訪問image_size
,width
,或height
,它永遠是空。
我還問了here如果你想了解更多的細節。
是啊,你需要破解了一下,以得到它的工作,但沒有這麼多。從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-09-13 00:39:18
您尚未指定您正在處理的語言和系統。
但是,對於大多數Web框架,我認爲使用image magic來實現這一點的標準方法。試試identify function.。
啊,我很抱歉。我在Ruby on Rails中使用attachment_fu插件。我使用迷你magick作爲我的處理器。不過,根據你的評論,似乎我需要挖掘attachment_fu。 – 2009-02-01 03:30:45
你看過迷你魔術師嗎?
你可以克隆它從這裏的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 screencasts)
這是一個紅寶石包裝紙nd imagemagick函數(不確定attachment_fu是否在內部使用這個函數),但它絕對比RMagick好得多(RMagick非常臃腫,大量內存問題)。任何人,迷你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)
我認爲你缺少應該安裝才能使用attachment_fu用於調整圖像的前提寶石。我曾與attachment_fu插件,它是依賴於以下寶石
工作rmagick-2.11.0
image_science-1.2.0
確保已安裝上述寶石和更改到has_attachment中的寬度和高度,然後您可以看到更改。
祝你好運!
對不起,我不回覆一段時間 - 我沒有檢查那件事通過電子郵件更新我。 – 2009-09-13 00:37:38