Created
January 6, 2014 23:48
-
-
Save alfakini/7ac8d5da140e58c09873 to your computer and use it in GitHub Desktop.
Dynamically defining retina images size. Based on: http://stackoverflow.com/questions/16651900/conditional-image-resizing-with-carrierwave?rq=1
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
# -*- encoding : utf-8 -*- | |
class AccountLogoUploader < CarrierWave::Uploader::Base | |
include CarrierWave::MiniMagick | |
if Rails.env.development? || Rails.env.test? | |
storage :file | |
else | |
storage :s3 | |
s3_access_policy 'public-read' | |
end | |
max_file_size = 5.megabytes | |
version :original do | |
process | |
end | |
version :thumbnail do | |
process :resize_to_limit => [100, 75] | |
end | |
version :logo do | |
process :resize_to_fit => [160, 160] | |
end | |
version :retina do | |
process :resize_to_fill => [160, 160] ,:if => :is_not_retina_resolution? | |
process :resize_to_fill => [160*2, 160*2] ,:if => :is_retina_resolution? | |
end | |
def store_dir | |
"account_logos" | |
end | |
def extension_white_list | |
%w(jpg jpeg gif png) | |
end | |
# Override the filename of the uploaded files | |
def filename | |
"#{model.account_id}.#{file.extension}" if original_filename && model && model.account_id | |
end | |
private | |
def is_retina_resolution?(new_file) | |
image = MiniMagick::Image.open(self.file.file) | |
true if image[:height] >= 160*2 and image[:width] >= 160*2 | |
end | |
def is_not_retina_resolution?(new_file) | |
image = MiniMagick::Image.open(self.file.file) | |
true if image[:height] < 160*2 and image[:width] < 160*2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment