Skip to content

Instantly share code, notes, and snippets.

@alfakini
Created January 6, 2014 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfakini/7ac8d5da140e58c09873 to your computer and use it in GitHub Desktop.
Save alfakini/7ac8d5da140e58c09873 to your computer and use it in GitHub Desktop.
# -*- 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