Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

Copy link

sgreenfield commented May 27, 2011

uninitialized constant Paperclip::ThumbnailWithDimensions

:(

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

@r3ap3r2004

This comment has been minimized.

Copy link
Owner Author

r3ap3r2004 commented May 28, 2011

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

This comment has been minimized.

Copy link

sgreenfield commented May 31, 2011

@andersonspb

This comment has been minimized.

Copy link

andersonspb commented Jun 7, 2011

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
You can’t perform that action at this time.