FastImage Resize
Not maintained since 2015. Let me know if you’d like to help out.
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 for compiling and managing the C extension code.
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.
Examples
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>
Giving a zero value for width or height causes the image to scale proportionately.
Installation
First check the requirements section below.
Gem
gem install fastimage_resize
Rails
Install the gem as above, and for Rails 2 configure it in your environment.rb file as below:
... Rails::Initializer.run do |config| ... config.gem "fastimage_resize" ... end ...
For Rails 3, add this to your Gemfile:
gem 'fastimage_resize'
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:
ENV['INLINEDIR'] = RAILS_ROOT + "/tmp" # for RubyInline
Then you’re off – just use FastImage.resize() in your code as in the examples.
Requirements
- RubyInline
gem install RubyInline
- FastImage
gem install fastimage
- Libgd
Libgd is commonly available on most unix platforms, including OSX.
On OSX, if you have macports you can use.
sudo port install gd2
It is also available in homebrew (search for gd), and fink (try fink install gd2 gd2-bin).
Documentation
http://rdoc.info/projects/sdsykes/fastimage_resize
Caveats
Because of the way that libgd works, gif files that have transparency may not always come through with the transparency perfectly retained.
Tests
You’ll need to ‘gem install fakeweb’ to be able to run the tests
References
Licence
MIT, see file MIT_LICENCE
Author
Stephen Sykes, @sdsykes