Skip to Main Content
Professional Ruby on Rails™
book

Professional Ruby on Rails™

by Noel Rappin
February 2008
Intermediate to advanced content levelIntermediate to advanced
479 pages
14h
English
Wrox
Content preview from Professional Ruby on Rails™

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher
Rails 4 in Action

Rails 4 in Action

Yehuda Katz, Rebecca Skinner, Stephen Klabnik, Ryan Bigg

Publisher Resources

ISBN: 9780470223888Purchase book