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
Get Professional Ruby on Rails™ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.