h1. FastImage Resize

h4. FastImage Resize is an extremely light solution for resizing images in ruby by using libgd

FastImage Resize will resize gifs, jpegs, and png files.

It uses resampling to get good looking results.

And it doesn't rely on installing external heavy libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).

FastImage Resize uses *Libgd*, which is commonly available on most unix platforms, including OSX.  It is very likely that you already have this software installed on your server.

As its input, FastImage Resize can take a URI, a filename, or an IO object (or anything that responds to :read).
If you do not supply an output filename in the :outfile option, FastImage Resize will return you a Tempfile object. This will be unlinked when it is finalized during garbage collection.

FastImage Resize relies on "RubyInline":https://github.com/seattlerb/rubyinline for compiling and managing the C extension code.

h4. Incompatible API change Version 2.0.0 and above

Note that the parameters for version 2.0.0 have changed, the output filename is no longer the second parameter. See the examples.

h2. Examples

<pre>
<code>
  require 'fastimage_resize'

  FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", 100, 20, :outfile=>"my.gif")
  => nil

  outfile = FastImage.resize("nonexistentfile.png", 50, 50)
  =>FastImage::ImageFetchFailure: FastImage::ImageFetchFailure

  outfile = FastImage.resize("afile.png", 50, 150)
  => #<File:/var/folders/vm/vd65y2597xl_by6c73_m9j1h0000gn/T/FastImage20111003-7638-x3l8r7-0.png>
  
  File.open("afile.png", "r") {|f| FastImage.resize(f, 100, 100)}
  => #<File:/var/folders/vm/vd65y2597xl_by6c73_m9j1h0000gn/T/FastImage20111003-7638-y1ofh-0.png>
</code>
</pre>

h2. Installation

First check the requirements section below.

h4. Gem

<pre>
<code>
  gem install fastimage_resize
</pre>
</code>

h4. Rails

Install the gem as above, and for Rails 2 configure it in your environment.rb file as below:
<pre>
<code>
...
Rails::Initializer.run do |config|
  ...
  config.gem "fastimage_resize"
  ...
end
...
</code>
</pre>

For Rails 3, add this to your Gemfile:

<pre>
<code>
gem 'fastimage_resize'
</code>
</pre>

For any Rails version, you may also need this in your environment.rb so that the rails process puts the compiled C code in a place it can access:

<pre>
<code>
ENV['INLINEDIR'] = RAILS_ROOT + "/tmp"  # for RubyInline
</code>
</pre>

Then you're off - just use FastImage.resize() in your code as in the examples.

h2. Requirements

* RubyInline

<pre>
<code>
  gem install RubyInline
</pre>
</code>

* FastImage

<pre>
<code>
  gem install fastimage
</pre>
</code>

* Libgd

See "http://www.libgd.org/":http://www.libgd.org/

Libgd is commonly available on most unix platforms, including OSX.

On OSX, if you have "macports":http://www.macports.org/ you can use.

<pre>
<code>
  sudo port install gd2
</code>
</pre>

It is also available in "homebrew":http://github.com/mxcl/homebrew (search for gd), and "fink":http://www.finkproject.org/ (try fink install gd2 gd2-bin).

h2. Documentation

"http://rdoc.info/projects/sdsykes/fastimage_resize":http://rdoc.info/projects/sdsykes/fastimage_resize

h2. Caveats

Because of the way that libgd works, gif files that have transparency may not always come through with the transparency perfectly retained.


h2. Tests

You'll need to 'gem install fakeweb' to be able to run the tests

h2. References

* "http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing":http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing

h2. Licence

MIT, see file MIT_LICENCE

h2. Author

Stephen Sykes, @sdsykes