image_optim
Command line tool and ruby interface to optimize (lossless compress, optionally lossy) jpeg, png, gif and svg images using external utilities:
Based on ImageOptim.app.
Documentation for latest gem version and master branch.
A test application with latest image_optim and image_optim_pack is available on render: https://iopack.onrender.com/.
Gem installation
You may also want to install image_optim_pack (see Binaries pack).
gem install image_optim_pack
Bundler
Add to your Gemfile:
With image_optim_pack:
gem 'image_optim'
gem 'image_optim_pack'
With version:
gem 'image_optim', '~> 0.32'
If you want to check latest changes:
gem 'image_optim', :git => 'git://github.com/toy/image_optim.git'
Docker
This gem is also be available as docker image containing most binaries:
docker run --rm ghcr.io/toy/image_optim --version # image_optim version
docker run --rm ghcr.io/toy/image_optim --info # image_optim info including bin versions
docker run --rm -v "$PWD":/here -w /here ghcr.io/toy/image_optim image-in-this-folder.jpg
See image_optim_pack repository for Dockerfile and instructions.
Binaries location
Simplest way for image_optim to locate binaries is to install them in common location present in PATH (see Binaries installation).
If you cannot install to common location, then install to custom one and add it to PATH.
Specify custom bin location using XXX_BIN environment variable (JPEGOPTIM_BIN, OPTIPNG_BIN, JPEG_RECOMPRESS_BIN, …).
Besides permanently setting environment variables in ~/.profile, ~/.bash_profile, ~/.bashrc, ~/.zshrc, … they can be set:
-
before command:
PATH="/custom/location:$PATH" image_optim *.jpg
for example:
PATH="/Applications/ImageOptim.app/Contents/MacOS:$PATH" image_optim *.jpg
-
inside script:
ENV['PATH'] = "/custom/location:#{ENV['PATH']}"; ImageOptim.optimize_images([…])
for example:
ENV['PATH'] = "/Applications/ImageOptim.app/Contents/MacOS:#{ENV['PATH']}"; ImageOptim.optimize_images([…])
Binaries installation
Binaries pack
Easiest way to get latest versions of most binaries for image_optim for Linux and Mac OS X is by installing image_optim_pack gem.
Check installation instructions in Gem installation section.
Pack doesn't include pngout and svgo binaries, their installation instructions are provided below.
Linux - Debian/Ubuntu
sudo apt-get install -y advancecomp gifsicle jhead jpegoptim libjpeg-progs optipng pngcrush pngquant
If you get an old version of pngquant, please check how to install up-to-date version or compile from source at http://pngquant.org/.
Linux - RHEL/Fedora/Centos
sudo yum install -y advancecomp gifsicle jhead libjpeg optipng pngquant
You may also need to install libjpeg-turbo-utils instead of libjpeg:
sudo yum install -y libjpeg-turbo-utils
You will also need to install jpegoptim and pngcrush from source:
jpegoptim
Replace X.Y.Z with latest version number from http://www.kokkonen.net/tjko/projects.html#jpegoptim.
JPEGOPTIM_VERSION=X.Y.Z
cd /tmp
curl -O http://www.kokkonen.net/tjko/src/jpegoptim-$JPEGOPTIM_VERSION.tar.gz
tar zxf jpegoptim-$JPEGOPTIM_VERSION.tar.gz
cd jpegoptim-$JPEGOPTIM_VERSION
./configure && make && make install
pngcrush
Replace X.Y.Z with latest version number from http://sourceforge.net/projects/pmt/files/pngcrush/.
PNGCRUSH_VERSION=X.Y.Z
cd /tmp
curl -O http://iweb.dl.sourceforge.net/project/pmt/pngcrush/$PNGCRUSH_VERSION/pngcrush-$PNGCRUSH_VERSION.tar.gz
tar zxf pngcrush-$PNGCRUSH_VERSION.tar.gz
cd pngcrush-$PNGCRUSH_VERSION
make && cp -f pngcrush /usr/local/bin
OS X: Macports
sudo port install advancecomp gifsicle jhead jpegoptim jpeg optipng pngcrush pngquant
OS X: Brew
brew install advancecomp gifsicle jhead jpegoptim jpeg optipng oxipng pngcrush pngquant jonof/kenutils/pngout
oxipng installation (optional)
Unless it is available in your chosen package manager, can be installed using cargo:
pngout installation (optional)
If you installed the dependencies via brew, pngout should be installed already. Otherwise, you can install pngout by downloading and installing the binary versions.
Note: pngout is free to use even in commercial soft, but you can not redistribute, repackage or reuse it without consent and agreement of creator. license
svgo installation (optional)
svgo is available from NPM.
If you prefer to install svgo to your project directory, use one of the following commands instead:
npm install svgo
yarn add svgo
When installing svgo to the project directory, you must add the following to your environment:
SVGO_BIN='node_modules/svgo/bin/svgo'
jpeg-recompress installation (optional)
Download and install the jpeg-recompress binary from the JPEG-Archive Releases page,
or follow the instructions to build from source.
Usage
From shell
image_optim *.{jpg,png,gif,svg}
image_optim -r .
image_optim -h
From ruby
Initialize optimizer (or you can call optimization methods directly on ImageOptim):
image_optim = ImageOptim.new
image_optim = ImageOptim.new(:pngout => false)
image_optim = ImageOptim.new(:nice => 20)
Optimize image getting temp path:
image_optim.optimize_image('a.png')
Optimize image in place:
image_optim.optimize_image!('b.jpg')
Optimize image data:
image_optim.optimize_image_data(data)
Multiple images:
image_optim.optimize_images(Dir['*.png']) do |unoptimized, optimized|
if optimized
puts "#{unoptimized} => #{optimized}"
end
end
image_optim.optimize_images!(Dir['*.*'])
image_optim.optimize_images_data(datas)
From rails
Rails image assets optimization is extracted into image_optim_rails gem.
Configuration
Configuration in YAML format will be read and prepended to options from two paths:
$XDG_CONFIG_HOME/image_optim.yml (by default ~/.config/image_optim.yml)
.image_optim.yml in current working directory
Paths can be changed using :config_paths option and --config-paths argument.
Example configuration:
nice: 20
pngout: false # disable
optipng:
level: 5
Temporary directory
image_optim uses standard ruby library for creating temporary files. Temporary directory can be changed using one of TMPDIR, TMP or TEMP environment variables.
Benchmark
Run with --benchmark to compare the performance of each individual tool on your images:
image_optim --benchmark=isolated -r /tmp/corpus/
benchmarking: 100.0% (elapsed: 3.9m)
BENCHMARK RESULTS
name files elapsed kb saved kb/s
-------- ----- ------- -------- -------
oxipng 50 8.906 1867.253 209.664
pngquant 50 1.980 214.597 108.386
pngcrush 50 22.529 1753.704 77.841
optipng 50 142.940 1641.101 11.481
advpng 50 137.753 962.549 6.987
pngout 50 426.706 444.679 1.042
Options
:nice — Nice level, priority of all used tools with higher value meaning lower priority, in range -20..19, negative values can be set only if run by root user (defaults to 10)
:threads — Number of threads or disable (defaults to number of processors)
:verbose — Verbose output (defaults to false)
:pack — Require image_optim_pack or disable it, by default image_optim_pack will be used if available, will turn on :skip-missing-workers unless explicitly disabled (defaults to nil)
:skip_missing_workers — Skip workers with missing or problematic binaries (defaults to false)
:allow_lossy — Allow lossy workers and optimizations (defaults to false)
:cache_dir — Configure cache directory
:cache_worker_digests - Also cache worker digests along with original file digest and worker options: updating workers invalidates cache
:timeout — Maximum time in seconds to spend on one image, note multithreading and cache (defaults to unlimited)
Worker can be disabled by passing false