• Stars
    star
    858
  • Rank 53,134 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 9 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High-level image processing wrapper for libvips and ImageMagick/GraphicsMagick

ImageProcessing

Provides higher-level image processing helpers that are commonly needed when handling image uploads.

This gem can process images with either ImageMagick/GraphicsMagick or libvips libraries. 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).

Goal

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, ActiveStorage, 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.

Installation

  1. Install ImageMagick and/or libvips:

In a Mac terminal:

  $ brew install imagemagick vips

In a debian/ubuntu terminal:

  $ sudo apt install imagemagick libvips
  1. Add the gem to your Gemfile:
gem "image_processing", "~> 1.0"

Usage

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 derivates:

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 method

You 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_class=>ImageProcessing::MiniMagick::Processor}

The source object needs to responds to #path, or be a String, a Pathname, or a Vips::Image/MiniMagick::Tool object. 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")

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.

Instrumentation

You can register an #instrumenter block for a given pipeline, which will wrap the pipeline execution, allowing you to record performance metrics.

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 instrumenter

Contributing

Our test suite requires both imagemagick and libvips libraries to be installed.

In a Mac terminal:

$ brew install imagemagick vips

In a debian/ubuntu terminal:

sudo apt install imagemagick libvips

Afterwards you can run tests with

$ bundle exec rake test

Feedback

We 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!

Credits

The ImageProcessing::MiniMagick functionality was extracted from refile-mini_magick. The chainable interface was heavily inspired by HTTP.rb.

License

MIT

More Repositories

1

down

Streaming downloads using Net::HTTP, http.rb or HTTPX
Ruby
1,024
star
2

rodauth-rails

Rails integration for Rodauth authentication framework
HTML
424
star
3

tus-ruby-server

Ruby server for tus resumable upload protocol
Ruby
215
star
4

paperclip-dropbox

[OBSOLETE] Extends Paperclip with Dropbox storage.
Ruby
148
star
5

as-duration

Extraction of ActiveSupport::Duration from Rails
Ruby
125
star
6

sequel-activerecord_connection

Allows Sequel to reuse Active Record's database connection
Ruby
121
star
7

uppy-s3_multipart

Provides Ruby endpoints for aws-s3-multipart Uppy plugin
Ruby
63
star
8

tic-tac-toe

Play tic-tac-toe in your Terminal
Ruby
43
star
9

rodauth-demo-rails

Example Rails app that uses Rodauth for authentication
Ruby
36
star
10

rodauth-omniauth

OmniAuth login and registration for Rodauth authentication framework
Ruby
34
star
11

flickr-objects

An object-oriented wrapper for the Flickr API.
Ruby
28
star
12

shrine-example

[MOVED] Roda & Sequel demo for Shrine
JavaScript
19
star
13

rodauth-i18n

I18n integration and translations for Rodauth authentication framework
Ruby
16
star
14

flickrie

[DEPRECATED] A wrapper gem for the Flickr API
Ruby
14
star
15

rodauth-model

Password attribute and associations for Rodauth account model
Ruby
13
star
16

rodauth-pwned

Rodauth extension that checks user passwords against the Pwned Passwords API
Ruby
12
star
17

goliath-rack_proxy

[DEPRECATED] Goliath as a web server for your Rack app
Ruby
11
star
18

budget

Roda & Sequel app for tracking expenses
Ruby
8
star
19

janko.io

My blog
HTML
7
star
20

dotfiles

My dotfiles
Vim Script
6
star
21

hanami-rodauth-example

Example Hanami 2 application using Rodauth authentication framework
Ruby
6
star
22

musique

A Ruby gem for manipulating musical constructs.
Ruby
6
star
23

capybara-vs-webdriverio

Battle between Capybara and Webdriver.io for browser testing
JavaScript
5
star
24

flickr-login

A gem that provides Flickr authentication.
Ruby
2
star
25

roda-symbolized_params

A Roda plugin which symbolizes request params
Ruby
2
star
26

shrine-example-melbourne

Shrine tutorial application for Coder Factory Academy
Ruby
1
star
27

refactoring_practice

Ruby
1
star
28

sequel-jsonapi_eager

DEPRECATED in favor of `tactical_eager_loading` Sequel plugin
Ruby
1
star
29

rodauth-guest

Guest accounts for Rodauth authentication framework
Ruby
1
star