Skip to content

Instantly share code, notes, and snippets.

@r3ap3r2004
Created March 25, 2010 15:37
Show Gist options
  • 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
@wiemann
Copy link

wiemann commented Apr 29, 2010

if you use this, make sure to write a migration that adds "image_dimensions" (type:text) to your table first.

@sgreenfield
Copy link

uninitialized constant Paperclip::ThumbnailWithDimensions

:(

Followed your instructions exactly. Any idea what the problem could be?

@r3ap3r2004
Copy link
Author

I assume that Paperclip is seeing the :processors entry for your attachment and since it is named :thumbnail_with_dimensions it is trying to load Paperclip::ThumbnailWithDimensions and just can't find it. This would tell me that you probably haven't put this code in the proper location in the filesystem for Paperclip to find it.

The code needs to go in RAILS_ROOT/lib/paperclip_processors/thumbnail_with_dimensions.rb

If that isn't the problem then I'm not sure what to tell you without more information. If you want to see if Paperclip/Rails is even finding the file then put something at the top of the file that will cause the Ruby parser to choke when it hits it (ie. asjfpoauhsodf, or any other random string that won't possibly evaluate to a real method). If you don't get an error related to that line of code then you know it isn't even reading the file.

Alternatively you could do something like.

raise "I read in the file"

Which should throw an exception when it is hit. If you don't get the exception then you know the file wasn't read.

If you have the file in the right location, but it still isn't being read then you may need to ask the paperclip guys what they changed.

http://groups.google.com/group/paperclip-plugin

@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