amuck-landowner

Removing Letterboxing from Images

Soylent

New Member
I have a site that pulls in hundreds of thousands of images from servers (real estate listings data). When the files are uploaded to the originating systems they're cropped to a desired resolution, but some of those systems do it really stupidly. They generate a white canvas of the desired size and then just do a dumb size reduction and throw the result on the canvas. If the original image isn't the same aspect ratio as the desired resolution you end up with white letterbox bars, usually at the top and bottom horizontally, but occasionally vertically depending on the aspect ratio. This image then gets jpeg compressed, and so the bars are no longer just straight #ffffff, but a range of whiteishness including #fefefe and #fdfcf9 and so on.

I'd like to be able to crop out these bars so that I can display the images more intelligently. Is there an easy way to do this programatically? Something in php with ImageMagick or the like would be preferable, since I get the file contents as a binary object from the server, and so I could theoretically do the cropping on the fly before writing the file so I'm not doing any more disk I/O than I am already. If I had to I'd be willing to resort to a shell script, but it's not my first choice. Speed is obviously a priority given the number of images I'm dealing with.

Has anyone dealt with anything like this before? It's on the "nice to have but not mission critical" list, but it bothers me.
 
Top
amuck-landowner