Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CarrierWave extension fix_exif_rotation, strip, quality, resize_to_fill_if_larger
class ArticleImageUploader < ImageUploader
process :fix_exif_rotation
process :strip
process :convert => 'jpg'
process :quality => 85 # Percentage from 0 - 100
version :gallery_thumb do
process :resize_to_fill => Settings.images.article_images.processing.gallery_thumb #44x44
end
version :gallery_large do
process :resize_to_fill_if_larger => Settings.images.article_images.processing.gallery_large #604x300
end
version :side_bar do
process :resize_to_fill => Settings.images.article_images.processing.side_bar #140x106
end
version :large_lightbox do
process :resize_to_limit => Settings.images.article_images.processing.large_lightbox #1200x720
end
version :featured do
process :resize_to_fill => Settings.images.article_images.processing.featured #303x300
end
version :category_list_top do
process :resize_to_fill => Settings.images.article_images.processing.category_list_top #222x167
end
version :category_list_thumb do
process :resize_to_fill => Settings.images.article_images.processing.category_list_thumb #60x45
end
end
#config/initializers/carrierwave.rb
module CarrierWave
module RMagick
# Rotates the image based on the EXIF Orientation
def fix_exif_rotation
manipulate! do |img|
img.auto_orient!
img = yield(img) if block_given?
img
end
end
# Strips out all embedded information from the image
def strip
manipulate! do |img|
img.strip!
img = yield(img) if block_given?
img
end
end
# Reduces the quality of the image to the percentage given
def quality(percentage)
manipulate! do |img|
img.write(current_path){ self.quality = percentage }
img = yield(img) if block_given?
img
end
end
def resize_to_fill_if_larger(width, height, gravity=::Magick::CenterGravity)
geometry = get_geometry
if geometry.first>width && geometry.last > height
manipulate! do |img|
img.crop_resized!(width, height, gravity)
img = yield(img) if block_given?
img
end
end
end
def get_geometry
img = ::Magick::Image::read(current_path).first
geometry = [ img.columns, img.rows ]
end
module ClassMethods
def resize_to_fill_if_larger(width, height, gravity=::Magick::CenterGravity)
process :resize_to_fill_if_larger => [width, height]
end
end
end
end
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# include CarrierWave::ImageScience
# Including support for the asset pipeline
# https://github.com/jnicklas/carrierwave/issues/493
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# 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
"#{CarrierWave::Uploader::Base.store_dir}/#{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
asset_path("defaults/#{model.class.to_s.underscore}/default#{'_' if version_name.present?}#{version_name}.jpg")
#"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png bmp)
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
@relrod

This comment has been minimized.

Copy link

commented May 21, 2012

What license is this code under? Specifically fix_exif_rotation()?

@jcsrb

This comment has been minimized.

Copy link
Owner Author

commented May 21, 2012

@relrod

This comment has been minimized.

Copy link

commented May 21, 2012

Thanks! I just left a comment on there asking. I know it's a pretty trivial example, but I did look at it and base some code off of it, and I want to be fair and legal. :)

@fatuhoku

This comment has been minimized.

Copy link

commented Jan 10, 2017

What happens if we want to upload a PDF w/ the same systemm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.