O'Reilly logo

Professional Ruby on Rails™ by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.3. Using Your Graphics Library

Now that you've got some images in your system to manipulate, it's time to discuss how to use the libraries to do fancy graphical stuff (to use the technical term). A lot of these examples are going to be in the console because you generally don't want to have long-running graphics manipulation running in your web server process (although MiniMagick spins off a separate command-line process on its own, so including it is much less of a performance problem).

11.3.1. ImageScience

ImageScience has a very simple API, as you might expect from its focus on just resizing images. All the manipulation is done via the method ImageScience.with_image(filename). The method takes the following block, in which you do whatever manipulation you need:

ImageScience.with_image("data/soup.jpg") do |image|
  image.thumbnail(150) do |thumbnail|
    thumbnail.save("data/soup_thumbnail.jpg")
  end
end

The with_image method opens the file and converts it to an ImageScience instance. Within the block, the call to thumbnail takes a single size dimension. The image is scaled proportionally until its longest side is the given length. The method returns another ImageScience instance, which should be used inside another block. The save method saves the new file to the given file location. If you change the extension on the file, then ImageScience will perform the image type conversion for you.

The only attribute information you can get out of an ImageScience instance is the width

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required