Forked from r3ap3r2004/thubmnail_with_dimensions.rb
Created
February 15, 2011 13:37
-
-
Save DanThiffault/827525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Filename: RAILS_ROOT/config/initializers/paperclip_thumbnail_with_dimensions.rb | |
# Required Configuration in the model | |
# | |
# include Paperclip::Dimensions | |
# | |
# has_attached_file :image, | |
# :styles => { | |
# :thumbnail => { | |
# :geometry => "100x100>", | |
# :format => :png | |
# }, | |
# :some_other_style => { | |
# :geometry => "640x480>" | |
# } | |
# } | |
# | |
# after_post_process :save_image_dimensions | |
# | |
# 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::Dimensions | |
def save_image_dimensions | |
attachments = [] | |
self.attributes.each do |attr| | |
attr_name = nil | |
attr_match = attr[0].match(/(.*)_file_name/) | |
attr_name = attr_match[1] unless attr_match.nil? | |
if attr_match && self.respond_to?(attr_name) | |
potential = self.send attr_name | |
attachments << potential if potential.instance_of? Paperclip::Attachment | |
end | |
end | |
# Event may be fired once per attachment, but I'm not sure how to distinguish | |
attachments.each do |attachment| | |
dimensions = attachment.dimensions | |
dimensions ||= {} # make sure he have a hash | |
attachment.styles.each do |style_prop| | |
style = style_prop[0] | |
dimensions[style] = Paperclip::Geometry.from_file(attachment.queued_for_write[style]) | |
end | |
# once for the original | |
dimensions[:original] = Paperclip::Geometry.from_file(attachment.queued_for_write[:original]) | |
attachment.instance_write(:dimensions, dimensions.to_json) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment