SmartImage is a new, powerful, cross-platform Ruby library for
compositing images and making thumbnails:
gem install smartimage
The above command should work on MRI (or any other platform that can
load the RMagick C extension) as well as JRuby.
Once upon a time there was RMagick. It was powerful but it was large
and confusing. Wading through a sea of documentation to figure out
how to do something simple like create a thumbnail was a monumental
undertaking! It was also much maligned for leaking memory, due to its
API providing low level access to the C library, which included manual
Then along came ImageScience. It made thumbnails and it was simple
and it was good! But it was only designed for the standard Matz Ruby
Interpreter. Then along came JRuby, and JRuby couldn’t run
ImageScience. The JRuby people conjured up ImageVoodoo to fill in for
ImageScience, but even then, it wasn’t seamless. And making
thumbnails was all that ImageScience and ImageVoodoo could do!
ImageVoodoo added more, but it wasn’t present in the ImageScience API.
I’ve often felt myself wishing that there was an alternative between
these two that incorporates more powerful image processing features
without drowning you in a sea
of APIs. I also work on both MRI and JRuby and prefer libraries that
across both platforms. And thus SmartImage was born.
Yes, SmartImage CAN create thumbnails for your Ruby on Rails-based
web application! And it can do it in the most cross-platform manner
imaginable! If you are looking for a thumbnail solution that will
allow you to safely migrate your web application to JRuby in the
future, look no further than SmartImage.
To use SmartImage in your Rails application, simply add the following
(there is an appropriate place to put this line, BTW. The exact
location is left as an exercise to the reader)
That’s it! Now wherever you would like to generate thumbnails, use
SmartImage.thumbnail_file( "path/to/input.jpg", "path/to/output.jpg", :width => 69, :height => 42 )
This will generate a thumbnail which is at most 69 pixels wide (but
could be smaller) and at most 42 pixels tall (but again, could be
smaller). It looks at the file extension to determine the output
format. We specified a .jpg so it will output a JPEG encoded image.
Why could it be smaller, you ask? Because SmartImage preserves the
aspect ratio of the original image by default. SmartImage allows you
to set aside space of a predetermined width/height, but will ensure
images are scaled with their aspect ratio preserved.
Don’t like this behavior? Want to stretch out your thumbnails all
weird? Just turn it off:
SmartImage.thumbnail_file( "path/to/input.jpg", "path/to/output.jpg", :width => 69, :height => 42, :preserve_aspect_ratio => false )
Tada! Stretched-out images! Yay!
SmartImage provides both file-based and data-based methods for every
API. All the APIs are the same, except file-based APIs have “_file”
on the end.
For example, above we used the SmartImage.thumbnail_file API.
However, there’s also a SmartImage.thumbnail API that works on raw
thumbnail = SmartImage.thumbnail image, :width => 69, :height => 42, :format => :jpg
This API produces a thumbnail in-memory from the given input image,
also in-memory. We’ve requested a .jpg thumbnail, with a max width of
69 and a max height of 42.
If an image format isn’t specified, the default is PNG.
SmartImage works by implementing a platform-specific SmartImage::Canvas
that encompasses all of the low level image manipulation primitives.
canvases are currently available:
- SmartImage::RMagickCanvas: a canvas backend based on the RMagick gem
- SmartImage::JavaCanvas: a canvas backend based on Java AWT/Graphics2D
SmartImage is a full-blown image compositing library which can
automate tasks similar to Photoshop macros, such as assembling and
compositing layers and alpha masks. For more information on these
features, please see the README:
You can subscribe to the SmartImage mailing list by sending email to:
SmartImage was created by Tony A… You can contact him through
github or follow him on Twitter at @bascule. He also frequents IRC on
freenode as tarcieri.