Monday, October 5, 2009

Unsuccessful Image Science

‹prev | My Chain | next›

Heh. I really meant to replace the legacy site with the updated Sinatra / CouchDB site (currently in beta). But then I had one last look at the homepage. It was slow:



Instead of taking 1-2 seconds to download each meal photo as it does on the beta site, it takes ~100ms on the legacy site:



Image thumbnails.

To get thumbnails in my application stack, I think I'll try a little Rack trickery. First, though I need ImageScience:
jaynestown% sudo apt-get install libfreeimage3 libfreeimage-dev 
sudo gem install image_science
I try to write Rack middle ware that will translate a thumbnail request into a normal request, then use image science to resize the image. I try, but am ultimately unsuccessful. This is where I am forced to leave off for the night:
class ThumbNail
def initialize(app); @app = app end
def call(env)

if env['REQUEST_URI'] =~ /_sm.jpe?g/
env['REQUEST_URI'].sub!(/_sm\./, '.')
env['REQUEST_PATH'].sub!(/_sm\./, '.')
env['PATH_INFO'].sub!(/_sm\./, '.')
http_code, headers, original_body = @app.call(env)

ImageScience.with_image_from_memory(original_body) do |img|
img.resize(100, 150) do |small|
small.save "/tmp/small.jpg"
end
end

f = File.open "/tmp/small.jpg"
[http_code, headers, f.read]
else
@app.call(env)
end
end
end

use ThumbNail
I am getting this error in the Thin logs:
jaynestown% thin -R config.ru start
>> Thin web server (v1.2.2 codename I Find Your Lack of Sauce Disturbing)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
!! Unexpected error while processing request: wrong argument type Rack::CommonLogger (expected String)
I will pick up tomorrow to continue working with ImageScience. If I am not able to make any more headway, I will drop down to RMagick.

Update: Ah, the original_body is not a string—it is something that responds to :each.

No comments:

Post a Comment