Created
October 12, 2009 13:33
-
-
Save rasputnik/208385 to your computer and use it in GitHub Desktop.
Rack Middleware to tell you dimensions of an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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