This gem allows you to simply optimize images via jpegoptim or OptiPNG.
Tested against ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.0, ruby-head, jruby-18mode, jruby-19mode, jruby-head, rbx-2.1.0, rbx-2.2.0, and ree
[] (http://travis-ci.org/jtescher/image_optimizer) [] (https://gemnasium.com/jtescher/image_optimizer) [] (https://codeclimate.com/github/jtescher/image_optimizer) [] (https://coveralls.io/r/jtescher/image_optimizer)
-
jpegoptim, which can be installed from freecode.com
-
OptiPNG, which can be installed from sourceforge.net
Or install the utilities via homebrew:
$ brew install optipng jpegoptim
Then add this line to your application's Gemfile:
gem 'image_optimizer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install image_optimizer
OptiPNG is a PNG optimizer that recompresses image files to a smaller size without losing any information and performs PNG integrity checks and corrections.
ImageOptimizer.new('path/to/file.png').optimize
jpegoptim provides lossless optimization for JPEG files based on optimizing the Huffman tables. All jpegs will be progressively optimized for a better web experience
ImageOptimizer.new('path/to/file.jpg').optimize
To have optimization performed in quiet mode without logging progress, an optional quiet
parameter may be passed.
Default is false.
ImageOptimizer.new('path/to/file.jpg', quiet: true).optimize
Pass an optional quality
parameter to target a specific lossy JPG quality level (0-100), default is lossless
optimization. PNGs will ignore the quality setting.
ImageOptimizer.new('path/to/file.jpg', quality: 80).optimize
By default, optipng
is called with the -o7
flag, which controls the level of
optimization. This default level generates the most optimized results, at the
expense of very high execution times, so you may want to lower it if your server
can't handle it.
You can pass an optional level
parameter to change this value. the JPEG
optimizer will ignore the value.
ImageOptimizer.new('path/to/file.png', level: 3).optimize
Pass an optional identify
parameter to identify file types using ImageMagick or GraphicsMagick identify
instead of the filename extension, default is false.
ImageOptimizer.new('path/to/file.jpg', identify: true).optimize
Optionally set binary directories with the OPTIPNG_BIN
, JPEGOPTIM_BIN
and IDENTIFY_BIN
environment variables.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request