Forked from r3ap3r2004/thubmnail_with_dimensions.rb
Last active
August 29, 2015 14:22
-
-
Save jcarhuazv/afe179809d9d86d97640 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment