# SmartImage 0.0.2: a sane cross-platform image compositing and thumbnail library

**URL:** <https://www.ruby-forum.com/t/smartimage-0-0-2-a-sane-cross-platform-image-compositing-and-thumbnail-library/186024>\
**Category:** Ruby\
**Created:** [March 30, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/smartimage-0-0-2-a-sane-cross-platform-image-compositing-and-thumbnail-library/186024 "2010-03-30T16:59:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [March 30, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/smartimage-0-0-2-a-sane-cross-platform-image-compositing-and-thumbnail-library/186024/1 "2010-03-30T16:59:12Z")

</div>

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.

* * *

## Background

> **[GitHub - tarcieri/SmartImage: It's like a Swiss Army Knife for images, but...](https://github.com/tarcieri/smartimage)**
>
> It's like a Swiss Army Knife for images, but one of those tiny ones you can keep on your keychain - GitHub - tarcieri/SmartImage: It's like a Swiss Army Knife for images, but one of those t...

[http://rubygems.org/gems/smartimage](http://rubygems.org/gems/smartimage)

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  
memory management.

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  
work seamlessly  
across both platforms. And thus SmartImage was born.

* * *

## Can it create thumbnails for my Ruby on Rails-based web application?

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  
to config/environment.rb:

```
config.gem 'smartimage'

```

(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  
the following:

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

* * *

## What if I want to work with raw image data instead of files?

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  
image data:

```
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.

* * *

## What backends are supported?

SmartImage works by implementing a platform-specific SmartImage::Canvas  
class  
that encompasses all of the low level image manipulation primitives.  
Two such  
canvases are currently available:

- SmartImage::RMagickCanvas: a canvas backend based on the RMagick gem
- SmartImage::JavaCanvas: a canvas backend based on Java AWT/Graphics2D  
APIs

* * *

## What else can it do?

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:

> <https://github.com/tarcieri/SmartImage/blob/master/README.markdown>

* * *

## Mailing List, Author Info

You can subscribe to the SmartImage mailing list by sending email to:

removed\_email\_address@domain.invalid

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.

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [March 30, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/smartimage-0-0-2-a-sane-cross-platform-image-compositing-and-thumbnail-library/186024/2 "2010-03-30T17:09:10Z")

</div>

Interesting. Does it correctly orient images? There is a flag in the  
EXIF  
data that tells you which orientation the picture was taken in. So if  
you  
turn your camera from landscape to portrait the flag is set. This was  
the  
thumbnail would be the right way up (even if the image file was not).

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [March 30, 2010, 6:20pm UTC](https://www.ruby-forum.com/t/smartimage-0-0-2-a-sane-cross-platform-image-compositing-and-thumbnail-library/186024/3 "2010-03-30T18:20:01Z")

</div>

On Tue, Mar 30, 2010 at 9:08 AM, Peter H. \<  
removed\_email\_address@domain.invalid\> wrote:

> Interesting. Does it correctly orient images? There is a flag in the EXIF  
> data that tells you which orientation the picture was taken in. So if you  
> turn your camera from landscape to portrait the flag is set. This was the  
> thumbnail would be the right way up (even if the image file was not).

Offhand I can’t say… I’d need to write a spec for it.

I would like to provide consistent semantics across all of the backend  
canvas libraries (RMagick and Java AWT/Graphics2D for the time being)  
and  
that’s something worth checking out.
