Skip to content

Instantly share code, notes, and snippets.

@jordantomax
Created May 9, 2013 18:50
Show Gist options
  • Save jordantomax/5549627 to your computer and use it in GitHub Desktop.
Save jordantomax/5549627 to your computer and use it in GitHub Desktop.
task :update_images => :environment do
WorkImage.all.each do |model|
if model.image.present?
model.image.recreate_versions!
model.save
end
end
end
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
version :display, :if => :not_thumbnail? do
process :resize_to_fit => [875, 1500]
end
version :thumbnail, :if => :thumbnail? do
process :resize_to_fit => [150, 200]
process :sepiatone
end
process :store_geometry
def thumbnail? file
model.thumbnail_enabled?
end
def not_thumbnail? file
!model.thumbnail_enabled?
end
def store_geometry
if @file && model
img = ::Magick::Image::read(@file.file).first
model.width = img.columns
model.height = img.rows
end
end
def sepiatone
manipulate! do |img|
# Convert the color image to monochrome
img = img.quantize(256, Magick::GRAYColorspace).colorize(0.30, 0.30, 0.30, '#cc9933')
end
end
# white list
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment