Вы находитесь на странице: 1из 3

8/3/2014 Efficiently converting image formats carrierwaveuploader/carrierwave Wiki

https://github.com/carrierwaveuploader/carrierwave/wiki/Efficiently-converting-image-formats 1/3
[
v Pages 54
Find a Page
CarrierWave and multiple
databases
Efficiently converting image
formats
Home
How to: "Upload" from a local file
How to: Cleanup after your Rspec
tests
How to: Cleanup original file that
wasn't uploaded like as Tempfile
How to: create a thumbnail with
both color and grayscale
versions in one image
How to: Create random and
unique filenames for all
versioned files
How to: Customize your version
file names
How to: Define different storage
configuration for each Uploader.
How to: Delete cache garbage
directories
How to: Detect a new file in a
mounted uploader
How to: Do conditional
processing
How to: Dynamically set storage
type
How to: Easy Ajax file uploads
with Remotipart
Show 39 more pages
Clone this wiki locally
Efficiently converting image formats
Antiarchitect edited this page on Oct 5, 2012 3 revisions
When uploading images, adding more that 1 process to a version can cause RMagick or
MimiMagick to run multiple commands. This leads to slower processing times and increased file
sizes.
MiniMagick has a solution for this.
https://github.com/probablycorey/mini_magick/blob/master/lib/mini_magick.rb#L346
So to use this we can create an uploader like this.
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
version :normal do
process :efficient_conversion => [640, 960]
end
private
def efficient_conversion(width, height)
manipulate! do |img|
img.combine_options do |c|
c.fuzz "3%"
c.trim
c.resize "#{width}x#{height}>"
c.resize "#{width}x#{height}<"
end
img
end
end
end
There is a catch with changing the format. To change format we must change the name of the
output file. This is how ImageMagick changes the format. So we need to use the
MiniMagicks#format method, luckily format takes a block just like combine_options .
def efficient_conversion(width, height)
manipulate! do |img|
img.format("png") do |c|
c.fuzz "3%"
c.trim
c.resize "#{width}x#{height}>"
c.resize "#{width}x#{height}<"
end
img
end
end
# You can use multiple commands together using this method. Very easy to use!
#
# @example
# image.combine_options do |c|
# c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
# c.thumbnail "300x500>"
# c.background background
# end
#
lcosta + y (
130 5,090 802 @ Watch Unstar y Fork
carrierwaveuploader / carrierwave
g
O

Q
7
+
@
Edit New Page
https://github.com/carrierwaveuploader/carrierwave.wiki.git _
g Clone in Desktop
Changing the format
Explore Gist Blog Help Search or type a command This repository @
8/3/2014 Efficiently converting image formats carrierwaveuploader/carrierwave Wiki
https://github.com/carrierwaveuploader/carrierwave/wiki/Efficiently-converting-image-formats 2/3
There is another catch with changing the format. Using the format method will only change the
name of the tmp file. The final version of the file is named by carrier wave and even though the file it
creates will be a genuine file of the specified format with the correct mime type, it's extension will
be not be changed. We need to use the filename method to set this.
def filename
"#{model.nicely_formatted_filename}.png"
end
TLDR; use this
Most of ImageMagick's args look like -resize \"640x960\" .
Adding a profile is done with -profile .
Removing a profile is done with +profile . Not exactly intuitive, but it does the job.
MiniMagick uses method missing to create the args, so c.rotate "90" turns into -rotate
\"90\" .
To add an arg that starts with a '+', like the +profile arg, we need to use the push method
followed by the + method. The + method turns the previously pushed arg into a '+' arg and then
adds the passed options to it.
This code will strip all profiles except 'xmp' then add the 'sRGB.icc' profile.
Full list of mogrify arguments - http://www.imagemagick.org/www/command-line-
options.html
Notes on geometry which is used by many of the commands -
http://www.imagemagick.org/www/command-line-processing.html#geometry
c.push '+profile'
c.+ "!xmp,*"
c.profile "path/to/color_profiles/sRGB_v4_ICC_preference_displayclass.icc"
c.push '+profile'
c.+ "!xmp,*"
c.profile "path/to/color_profiles/sRGB_v4_ICC_preference_displayclass.icc"
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
version :normal do
process :mogrify => [{
:resolution => '108x369'
}]
end
def default_url
"#{model.class.to_s.underscore.downcase}/#{mounted_as}/missing/" + [version_name
end
def extension_white_list
%w(jpg jpeg gif png bmp tif tiff)
end
def filename
"#{model.nicely_formatted_filename}.png"
end
private
def mogrify(options = {})
manipulate! do |img|
img.format("png") do |c|
Striping out the useless color profiles
Full example
8/3/2014 Efficiently converting image formats carrierwaveuploader/carrierwave Wiki
https://github.com/carrierwaveuploader/carrierwave/wiki/Efficiently-converting-image-formats 3/3
c.fuzz "3%"
c.trim
c.rotate "#{options[:rotate]}" if options.has_key?(:rotate)
c.resize "#{options[:resolution]}>" if options.has_key?(:resolution
c.resize "#{options[:resolution]}<" if options.has_key?(:resolution
c.push '+profile'
c.+ "!xmp,*"
c.profile "#{Rails.root}/lib/color_profiles/sRGB_v4_ICC_preference_displayclass.icc"
c.colorspace "sRGB"
end
img
end
end
end
Status API Training Shop Blog About 2014 GitHub, Inc. Terms Privacy Security Contact
[

Вам также может понравиться