ScaleDown
An on demand image scaling server.
Images are scaled based upon their URL. An HMAC signature is used to prevent unauthorized scaling of images.
Supports cropping images and converts CMYK to RGB.
URL Schema
Your file on example.com
has a public path of /images/john/picture.png
. You want it scaled to fit in a 400x400 pixel box. ScaleDown is running on the subdomain images
.
The URL to your image would be:
http://images.example.com/images/john/scaled/400x400/picture.png?HMAC_SIGNATURE
The scaled file is saved in a public path identical to the request. Once an image is scaled your webserve can statically server the file.
The schema is
http://:host/:path_to_file/scaled/:geometry/:filename?:hmac_signature
:host the address running ScaleDown
:path_to_file the public path of the original file
`scaled` keyword
:geometry width x height and an optional `-crop`
:filename the filename of the original image to scale
:hmac_signature security measure to validate the request
Geometry & Cropping
To crop an image include the -crop
option. The image will be scaled and cropped to fit the geometry.
http://images.example.com/images/scaled/400x300-crop/logo.png?A3SDACEDF
Using auto
as a dimension will fit the image to the other dimension.
For example, to ensure an image is 300 pixels wide set the height to auto
http://images.example.com/images/scaled/300xauto/logo.png/?A3SDACEDF
There is a very simple /info
function for getting image dimensions. It just returns a string with the WIDTHxHEIGHT.
http://images.example.com/images/logo.png/info
HMAC
You don't want someone taking down your server with malicious URLs, so an HMAC signature is used. This ensures that your URL was generated by a trusted source.
HMAC requires a shared key between the application generating the URL and the ScaleDown server.
Sample Ruby URL Generator
require 'ruby-hmac'
def signed_image_url(absolute_path, filename, geometry)
shared_secret = "secret"
hmac = HMAC::SHA1.new(shared_secret).update([absolute_path, 'scaled', geometry, filename].join("/")).to_s[0...8]
"http://images.example.com#{[absolute_path, 'scaled', geometry, CGI.escape(filename)].join("/")}?#{hmac}"
end
Sample Node.js URL Generator
// Uses the Node.js crypot library
var crypto = require('crypto')
function hmac(string){
var shared_secret = "secret"
return crypto.createHmac('sha1',shared_secret).update(string).digest('hex').substr(0,8)
}
function signed_image_url(absolute_path, filename, geometry){
signature = hmac( [ absolute_path, '/scaled', "/" + geometry, "/", filename].join("") )
return [global.$FD.assetHost, absolute_path, '/scaled', "/", geometry, "/",escape(filename)].join("") + "?"+ signature
}
PNG, JPG, TIFF and other images supported
ScaleDown will handle any image that Image Magick can process. But there are some rules:
- PNGs remain PNGs
- All other images are converted to JPG and issued a 301 redirect
Installation & Configuration
gem install scale_down
Create a Rackup file (config.ru). See https://github.com/jweir/ScaleDown/tree/master/config_sample.ru more options
require 'rubygems'
require 'scale_down'
ScaleDown.tap do |config|
# Shared secret for generating the HMAC signature
config.hmac_key = "secret"
# Path to the public directory
config.public_folder = File.(File.dirname(__FILE__))+"/public"
end
run ScaleDown::Controller
Configure Nginx, Apache, etc to run the server.
Known Issues
Pluses in filenames will not route properly. Filenames need to have these removed or replaced.
Dependencies
- Sinatra
- RMagick
- Ruby-HMAC
RMagick can be a bit tricky to install, these links http://www.google.com/search?q=install+rmagick might help.
LICENSE
(The MIT License)
Copyright © 2011 John Weir & Fame Driver LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.