Skip to content

Instantly share code, notes, and snippets.

@r3ap3r2004
Created March 25, 2010 15:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save r3ap3r2004/343678 to your computer and use it in GitHub Desktop.
Save r3ap3r2004/343678 to your computer and use it in GitHub Desktop.
Paperclip processor to save dimensions of an image
# Filename: RAILS_ROOT/lib/paperclip_processors/thumbnail_with_dimensions.rb
# Required Configuration in the model
# has_attached_file :image,
# :styles => {
# :thumbnail => {
# :geometry => "100x100>",
# :format => :png,
# :processors => [:thumbnail_with_dimensions],
# :mystyle => :thumbnail
# },
# :some_other_style => {
# :geometry => "640x480>",
# :processors => [:thumbnail_with_dimensions],
# :mystyle => :some_other_style
# }
# }
#
# NOTE: Since the name of the style isn't available in the processor we need to pass it in using a configuration variable.
# I've called it :mystyle just to make sure I don't step on anything in the core.
#
# To get a dimension in a view just do:
# <%= @object.image.width(:thumbnail) %> # Gives the width of the thumbnail
# <%= @object.image.height(:thumbnail) %> # Gives the height of the thumbnail
# <%= @object.image.width %> # Gives the width of the original
# <%= @object.image.dimensions %> Will return the dimensions hash
module Paperclip
class Attachment
def width style = :original
@dimensions = dimensions unless @dimensions
begin
Integer(@dimensions[style.to_s]['width'])
rescue
# Integer() throws an exception if it can't do it,
# we'll eat the exception incase any of our items don't have dimensions saved
return nil
end
end
def height style = :original
@dimensions = dimensions unless @dimensions
begin
Integer(@dimensions[style.to_s]['height'])
rescue
# Integer() throws an exception if it can't do it,
# we'll eat the exception incase any of our items don't have dimensions saved
return nil
end
end
def dimensions
unless @dimensions
dim = instance_read(:dimensions)
@dimensions = JSON.parse(dim) unless dim.nil?
end
@dimensions
end
end
end
module Paperclip
class ThumbnailWithDimensions < Thumbnail
def initialize file, options = {}, attachment = nil
super
@myinstance = attachment
@mystyle = options[:mystyle]
@dimensions = attachment.dimensions
end
def make
dst = super
@dimensions ||= {} # make sure he have a hash
@dimensions[@mystyle] = Geometry.from_file dst
# make sure the original dimensions are saved since we don't actually process the original
@dimensions[:original] ||= @current_geometry
@myinstance.instance_write(:dimensions, @dimensions.to_json)
return dst
end
end
end
@sgreenfield
Copy link

sgreenfield commented May 31, 2011 via email

@andersonspb
Copy link

Just added the processor to my app. Everything work fine if i manually call save on my model after a file is uploaded. Trying to reprocess all my attachements calling rake paperclip:refresh CLASS=MyModel I see that that processor is executed but results are not persisted to the database. Please advice what's wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment