This code integrates with paperclip to save “WxH” image dimensions to the database.
Only runs after a post_processing event has succeeded and the sizes are written to the database during the normal rails model save method.
Columns are auto-detected allowing for optional autosizing of each attached_file or specific style of attachment.
- Author
-
Andrew Eisberg
- Copyright
-
Copyright © 2009 Andrew Eisberg
- License
-
MIT License (www.opensource.org/licenses/mit-license.php)
Currently only the ImageMagick ‘>’ geometry flag is supported. One could write additional methods to support other IM geometries.
class User < PaperclipAutosizer has_attached_file :avatar, :styles => { :thumb => "100x100>" }, :processors => [:autosize] has_attached_file :center, :styles => { :large => "500x500>" }, :processors => [:autosize] after_post_process :autosize_attached_files end
In the migration for the model:
t.string :avatar_thumb_size t.string :center_large_size
View code:
image_tag(@user.avatar.url(:thumb), :size => @user.avatar_thumb_size) image_tag(@user.center.url(:large), :size => @user.center_large_size)
# Paperclip Method for ${1/[[:alpha:]]+|(_)/(?1::\u$0)/g} Attachment with Autosizing has_attached_file :${1:attachment_name}, :styles => { :${2:large} => “${3:750}x${4:750}>”, :${5:thumb} => “${6:150}x${7:150}>” }, :processors => [:autosize], :url => “/system/auto_load/${1}s/:id/:style/:basename.:extension”, :path => “:rails_root/public/system/auto_load/:attachment/:id/:style/:basename.:extension”, :default_url => “/system/auto_load/defaults/default_:style.png”
validates_attachment_size :${1}, :less_than => 1.megabytes validates_attachment_content_type :${1}, :content_type => [‘image/jpeg’, ‘image/png’] after_${1}_post_process :autosize_attached_files
$0