开发者

How to validate image based on resolution when using rails + paperclip

开发者 https://www.devze.com 2023-02-20 01:15 出处:网络
The jpeg header contains width and height in pixels I want to validate using these values (开发者_StackOverflow中文版max 19200x19200) Can i do that with paperclip?

The jpeg header contains width and height in pixels I want to validate using these values (开发者_StackOverflow中文版max 19200x19200) Can i do that with paperclip?

Or maybe I can validate it when the file is being uploaded. I'm using. uploadify


If its just JPEGs you're concerned with, you could read the EXIF data with something like: http://www.nihilogic.dk/labs/exifjquery/ You could trigger this at one of the uploadify callbacks (eg: onselect): http://www.uploadify.com/documentation/events/onselect/

Otherwise, if you cant read the EXIF, you'll need to upload it, process it, and if its more than you need just throw it away and send back an error.

From the paperclip readme (https://github.com/thoughtbot/paperclip/blob/master/README.md):

Before and after the Post Processing step, Paperclip calls back to the model with a few callbacks, allowing the model to change or cancel the processing step. The callbacks are before_post_process and after_post_process

Anyway, you can use paperclip to file out the dimensions: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/geometry.rb#L14

dimensions = Paperclip::Geometry.from_file(original_file)

Paperclip will also resize for you if the file is too large, so it won't matter if your users upload a large file:

has_attached_file :avatar, :styles => { :original => ["19200x19200>", :jpg] }


There is no resolution information available in a full standard JPEG file. It is meant to be displayed in 72dpi, as JPEG is made for the web. See http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format#Resolution_and_aspect_ratio

But most graphic programs hack EXIF data into a JPEG (which is not standard conform and more or less a proprietary hack of Adobe). You could try to fetch the resolution out of this EXIF data, it is saved there as x-Resolution and y-Resolution. The rMagick gem will be a good start.

0

精彩评论

暂无评论...
验证码 换一张
取 消