Provides higher-level image processing helpers that are commonly needed when handling image uploads.
This gem can process images with ImageMagick or libvips. ImageMagick is a good default choice, especially if you are migrating from another gem or library that uses ImageMagick. Libvips is a newer library that can process images very rapidly (often multiple times faster than ImageMagick).
The goal of this project is to have a single gem that contains all the helper methods needed to resize and process images.
Currently, existing attachment gems (like Paperclip, CarrierWave, Refile, Dragonfly, Active Storage, and others) implement their own custom image helper methods. But why? That's not very DRY, is it?
Let's be honest. Image processing is a dark, mysterious art. So we want to combine every great idea from all of these separate gems into a single awesome library that is constantly updated with best-practice thinking about how to resize and process images.
-
Install ImageMagick and/or libvips:
On macOS:
$ brew install imagemagick # if using ImageMagick $ brew install vips # if using libvips
On Debian/Ubuntu:
$ sudo apt install imagemagick # if using ImageMagick $ sudo apt install libvips # if using libvips
-
Add the gem(s) to your Gemfile:
gem "image_processing", "~> 2.0" gem "mini_magick", "~> 5.0" # if using ImageMagick gem "ruby-vips", "~> 2.0" # if using libvips
Processing is performed through ImageProcessing::Vips or
ImageProcessing::MiniMagick modules. Both modules share the same
chainable API for defining the processing pipeline:
require "image_processing/mini_magick"
processed = ImageProcessing::MiniMagick
.source(file)
.resize_to_limit(400, 400)
.convert("png")
.call
processed #=> #<Tempfile:/var/folders/.../image_processing20180316-18446-1j247h6.png>This allows easy branching when generating multiple derivatives:
require "image_processing/vips"
pipeline = ImageProcessing::Vips
.source(file)
.convert("png")
large = pipeline.resize_to_limit!(800, 800)
medium = pipeline.resize_to_limit!(500, 500)
small = pipeline.resize_to_limit!(300, 300)The processing is executed on #call or when a processing method is called
with a bang (!).
processed = ImageProcessing::MiniMagick
.convert("png")
.resize_to_limit(400, 400)
.call(image)
# OR
processed = ImageProcessing::MiniMagick
.source(image) # declare source image
.convert("png")
.resize_to_limit(400, 400)
.call
# OR
processed = ImageProcessing::MiniMagick
.source(image)
.convert("png")
.resize_to_limit!(400, 400) # bang methodYou can inspect the pipeline options at any point before executing it:
pipeline = ImageProcessing::MiniMagick
.source(image)
.loader(page: 1)
.convert("png")
.resize_to_limit(400, 400)
.strip
pipeline.options
# => {:source=>#<File:/path/to/source.jpg>,
# :loader=>{:page=>1},
# :saver=>{},
# :format=>"png",
# :operations=>[[:resize_to_limit, [400, 400]], [:strip, []]],
# :processor=>ImageProcessing::MiniMagick::Processor}The source object needs to respond to #path, or be a String, a Pathname, or
a Vips::Image/MiniMagick::Tool object. If no source is given,
ImageProcessing::Error is raised. Note that the processed file is always saved
to a new location, in-place processing is not supported.
ImageProcessing::Vips.source(File.open("source.jpg"))
ImageProcessing::Vips.source("source.jpg")
ImageProcessing::Vips.source(Pathname.new("source.jpg"))
ImageProcessing::Vips.source(Vips::Image.new_from_file("source.jpg"))When #call is called without options, the result of processing is a
Tempfile object. You can save the processing result to a specific location by
passing :destination to #call, or pass save: false to retrieve the raw
Vips::Image/MiniMagick::Tool object.
pipeline = ImageProcessing::Vips.source(image)
pipeline.call #=> #<Tempfile ...>
pipeline.call(save: false) #=> #<Vips::Image ...>
pipeline.call(destination: "/path/to/destination.jpg")When saving to a tempfile, the output format is the one passed to #convert,
falling back to the extension of the source file, and finally to JPEG. When
saving to a :destination path, the format is determined by the path's
extension; if the path has no extension (e.g. /dev/fd/N), the format passed
to #convert is used.
If processing fails while saving to a :destination path that didn't exist
before, the partially written file is deleted before the error is re-raised.
You can continue reading the API documentation for specific modules:
See the wiki for additional "How To" guides for common scenarios. The wiki is publicly editable, so you're encouraged to add your own guides.
Instead of chaining methods, you can pass operations as a hash to #apply,
which is handy when the processing steps are stored as data (e.g. Active
Storage variants). Hash keys are the builder methods to call, and values are
the arguments: a single argument, an array of arguments, a hash of keyword
arguments, or true/nil for no arguments.
ImageProcessing::Vips
.source(image)
.apply(resize_to_limit: [400, 400], sharpen: true, convert: "png")
.call
# equivalent to
ImageProcessing::Vips
.source(image)
.resize_to_limit(400, 400)
.sharpen
.convert("png")
.callTo call the same method more than once, pass an array of pairs instead:
ImageProcessing::Vips
.apply([[:rotate, 90], [:rotate, 90]])You can register an #instrumenter block for a given pipeline, which will wrap
the pipeline execution, allowing you to record performance metrics. The block
receives the pipeline options as keyword arguments (including :destination
when one was given), and the pipeline must be executed by calling the yielded
block.
pipeline = ImageProcessing::Vips.instrumenter do |**options, &processing|
options[:source] #=> #<File:...>
options[:loader] #=> { fail: true }
options[:saver] #=> { quality: 85 }
options[:format] #=> "png"
options[:operations] #=> [[:resize_to_limit, [500, 500]], [:flip, [:horizontal]]]
options[:processor] #=> ImageProcessing::Vips::Processor
ActiveSupport::Notifications.instrument("process.image_processing", **options) do
processing.call # calls the pipeline
end
end
pipeline
.source(image)
.loader(fail: true)
.saver(quality: 85)
.convert("png")
.resize_to_limit(500, 500)
.flip(:horizontal)
.call # calls instrumenterOur test suite requires both imagemagick and libvips libraries to be installed. Afterwards you can run tests with:
$ bundle exec rake testWe welcome your feedback! What would you like to see added to image_processing? How can we improve this gem? Open an issue and let us know!
The ImageProcessing::MiniMagick functionality was extracted from
refile-mini_magick. The chainable interface was heavily inspired by
HTTP.rb.