Created
August 27, 2013 19:58
-
-
Save atomgiant/6358340 to your computer and use it in GitHub Desktop.
Paperclip attachment settings for width / height and thumbnail determination
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
# contains common attachment methods | |
# NOTE: this currently assumes the attachment is called 'attachment' | |
module Attachable | |
# determines if an attachment may have a thumbnail | |
def thumbnailable? | |
!(attachment_content_type =~ %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png|pdf)$}).nil? | |
end | |
def image_dimensions(max=400) | |
return [0,0] unless thumbnailable? | |
d = self.dimensions.max | |
return self.dimensions if d <= max | |
self.dimensions.map{|e| (max.to_f / d * e).to_i} | |
end | |
# Retrieves dimensions for image assets | |
# @note Do this after resize operations to account for auto-orientation. | |
def extract_image_dimensions | |
return unless thumbnailable? | |
tempfile = attachment.queued_for_write[:original] | |
unless tempfile.nil? | |
geometry = Paperclip::Geometry.from_file(tempfile) | |
self.dimensions = [geometry.width.to_i, geometry.height.to_i] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment