imgio helps you resizing images along the lines of src.sencha.io. In short: add a prefix to an image URL, and get the image resized to whatever size you need. imgio will never size up an image though: what you'll get instead is an image matching the requested ratio.
imgio creates PNG and JPEG output files, and supports a plethora of input formats - everything RMagick handles.
The following <img> tag will always be 120px wide and 90px high. The requested image will be rescaled to fill that size. Overflowing parts of the image will be cut off.
<img src="http://<yourserver>/fill/120/90/http://www.google.de/images/srpr/logo3w.png" width="120" height="90">
</img>
The requested image will be rescaled to fit completely into the requested size. Empty parts are filled in white (JPEG) or transparent (PNG).
<img src="http://<yourserver>/fit/120/90/http://www.google.de/images/srpr/logo3w.png" width="120" height="90">
</img>
Such views allow you to scale & fill properly by themselves. However, escecially on mobile
platforms scaling down the image to whatever is actually needed still helps with not
wasting precious memory and CPU resources. To do so, just request http://<yourserver>/120/90/http://www.google.de/images/srpr/logo3w.png instead.
The full URL syntax is http://<yourserver>/[mode]/[format[quality]]/width/[height]/uri
- mode: "fit", "fill", "scale_down", optional, defaults to "scale_down"
- format: "png" or "jpg", optional, defaults to "jpg"
- quality: the quality used when generating jpg, optional, defaults to 85
- width: the width of the resulting image
- height: the height of the resulting image, optional, defaults to whatever height would match the original image's aspect ratio.
- uri: the URI to fetch the original image from
bundle install
bundle exec thin start
This script parses the URL you pass it, fetches the image at the URL, uses RMagick to convert the image, and spits out the result. Pretty basic stuff, actually.
The HTTP response has the proper cache headers set to cache the result for 1 day (on default). That means each requested image is built only a few times per day - assuming your webserver is configured to use a cache like varnish. Hint: Heroku runs its instances behind a varnish cache.
This is a simple sinatra application. Should work out of the box using the usual sinatra deployment options. How to deploy on heroku
- Clone. Extend. Fix. Send pull request.
Improvement areas are:
- error handling: What happens if an URL is unresponsive? We certainly should not cache a 404 or something similar, but we should not request the same URL over and over again. Any ideas welcome, especially when they do not need a database or some other kind of "local" storage.
- testing: @sebastianspier contributed some tests that do real work, i.e. actually fetch images off the net (thanks!). It would be nice though to have a) a web mock which just fake-delivers images, and b) some code that actually compares generated images with expected ones.
- Make sure you have ImageMagick installed
- Imgio only works with ruby >= 1.9
- Bundler is used for dependency management, so use
$ bundle installto fetch the needed dependencies - You can run the included tests with
$ rake
- @radiospiel
- @sebastianspier
- @Overbryd


