<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>@Lathi.net: Image Handling in Ruby on Rails</title>
    <link>http://blog.lathi.net/articles/2005/07/05/image-handling-in-ruby-on-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>Image Handling in Ruby on Rails</title>
      <description>&lt;p&gt;&lt;a href="http://lathi.net/diary/Software/RubyOnRails/speed_with_ruby_on_rails.html" target="_top"&gt;Speaking of images&lt;/a&gt;, I&amp;#8217;ve been doing more image work on my commercial &lt;a href="http://rubyonrails" target="_top"&gt;Ruby on Rails&lt;/a&gt; project.  
&lt;p /&gt;
I had some experience with imaging from my &lt;a href="http://lathi.net/twiki-bin/view/Main/RailsDay"&gt;RailsDay&lt;/a&gt; project.  The bad news is that because of how old my server is (running Debian Woody), I was unable to get the Ruby bindings for Image Magick installed (which is what nearly everyone seems to be using for imaging).  So, I ended up using the &lt;a href="http://www.pablotron.org/software/imlib2-ruby/" target="_top"&gt;ruby bindings&lt;/a&gt; for &lt;a href="http://www.enlightenment.org/Libraries/Imlib2.html" target="_top"&gt;Imlib2&lt;/a&gt;.
&lt;p /&gt;
My main problem came from how to cleanly test the image handling.  Uploading and saving images isn&amp;#8217;t very hard.  Tracking the images and the various scaled versions gets a little tricky.
&lt;p /&gt;
After several false starts and finally pairing with &lt;a href="http://lathi.net/twiki-bin/view/Main/MarkWindholtz"&gt;MarkWindholtz&lt;/a&gt;, here&amp;#8217;s what I&amp;#8217;ve come up with:
&lt;p /&gt;
I have an Image class based on ActiveRecord.  This pretty much just stores the filename of where the image is on the filesystem.  
&lt;p /&gt;
I have an ImageFiler object that is responsible for taking either the StringIO or File object from Ruby&amp;#8217;s &lt;span class="caps"&gt;CGI&lt;/span&gt; and writing it out to the disk.  It also deletes the file when appropriate.  Its purpose is to be the interface between the Image and the Filesystem.  
&lt;p /&gt;
I also have an Imager object.  This is a wrapper around the Imlib2 functions.  It does things like scale the image appropriately and returns the image&amp;#8217;s size.
&lt;p /&gt;
The containing Image object has pass through functions to a contained Imager and ImageFiler object.  Overall, that works pretty well.  It was easy to test all three objects independent of each other.  My test fixtures were very simple.
&lt;p /&gt;
The second wrinkle came when I tried to create and cache scaled versions of the images.  I wanted the thumbnail (and other scaled versions) to be actual Image objects, but I didn&amp;#8217;t want them stored in the database as saved ActiveRecords.  So, I made creation functions off of Image to create new Image objects, but just never saved them.  These &amp;#8220;sub-Images&amp;#8221; are created on the fly or loaded from disk.  
&lt;p /&gt;
It was a little tricky to get all the cached versions deleted when the Image object is deleted.  I ended up having a flag on the Image determine what type of Image it is: normal, thumbnail, scaled medium, etc.  This keeps me from going into a recursive loop and not deleting the thumbnail of the thumbnail Image.
&lt;p /&gt;
The whole thing was kind of tricky.  I&amp;#8217;d have thought image handling would be easier.  It could be that I&amp;#8217;m just a &lt;a href="http://lathi.net/diary/Software/RubyOnRails/speed_with_ruby_on_rails.html" target="_top"&gt;sub-standard programmer&lt;/a&gt; and my code is more complicated than it needs to be.&lt;/p&gt;</description>
      <pubDate>Tue, 05 Jul 2005 08:54:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cf20c9cfe96de037fe5dcc17e4446f45</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2005/07/05/image-handling-in-ruby-on-rails</link>
      <category>Ruby on Rails</category>
    </item>
  </channel>
</rss>

