Skip to content

Instantly share code, notes, and snippets.

@rasputnik
Created October 12, 2009 13:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasputnik/208385 to your computer and use it in GitHub Desktop.
Save rasputnik/208385 to your computer and use it in GitHub Desktop.
Rack Middleware to tell you dimensions of an image
# JRuby version of ImageScience (other image toolkits are available)
require 'image_voodoo'
class ImageSizer
def initialize(app, mime_types=%w[image/jpeg image/png image/gif])
@app = app
@mime_types = mime_types
end
def call(env)
status, headers, response = @app.call(env)
if @mime_types.include? headers['Content-Type']
headers['X-Content-Dimensions'] = getXY(response)
end
[status,headers,response]
end
def getXY(response)
s = ''
response.each { |l| s << l }
return 'n/a' if s.empty? # don't break HEADs
im = ImageVoodoo.with_bytes(StringIO.new(s).read)
return "#{im.width}x#{im.height}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment