Can you intelligently apply styles to Paperclip uploads?

174 views
Skip to first unread message

tallp

unread,
Jan 1, 2010, 11:55:46 PM1/1/10
to Paperclip Plugin
Hello,

I have the following definition in my model:

has_attached_file :file, :styles => { :medium => "300x200#", :large =>
"600x400#" }

Right now the user is allowed to upload JPEGs or PDFs. I would like to
apply the styles as defined if the upload is a JPEG and skip the
styles completely if the upload is a PDF.

Any way to do this? Thanks in advance.

-Poul

William Ross

unread,
Jan 2, 2010, 5:14:05 AM1/2/10
to papercli...@googlegroups.com


With the current master you can do something like this:

has_attached_file :file, :styles => lambda { |attachment|
if attachment.instance_read(:content_type) == 'image/jpeg'


{ :medium => "300x200#", :large => "600x400#" }

else
{}
end
}

If it gets any more complex than jpeg-or-not, you'll run into all sorts of annoying quirks in the variety and inconsistency of uploaded content types. I've ended up moving all that into an AssetType class which looks up styles and processors based on uploaded mime-type.

best,

will

matt

unread,
Jan 5, 2010, 11:31:40 AM1/5/10
to Paperclip Plugin
I can confirm this works, with the very latest paperclip code from
master at github. My early vendored version of the plugin returned
nil for attachment.instance_read(:content_type) in the styles lamdba.

Thanks,

matt

tallp

unread,
Jan 10, 2010, 11:34:02 PM1/10/10
to Paperclip Plugin
Yes, I too was able to get this to work once I upgraded to the latest
version of paperclip. Thanks to all for the push in the right
direction!

-Poul

Reply all
Reply to author
Forward
0 new messages