Saturday, March 15. 2008Rough Weather
A stormy day today! There were rain and thunderstorms on and off, all day, especially in the afternoon. Our neighborhood had several trees knocked down including some big pines in undeveloped lots. A tree came down in our backyard and smashed our earliest-flowering tree as it fell. But we came out pretty well compared to other folks in the upstate.
I made an ill-advised trip to pick up pizzas for a birthday party. The skies in Greenwood were spooky with some very low hanging dark clouds. Winds and hail came as I arrived at the pizza place. Finally it abated and I was able to get the food and drive home through some heavy rains. It may be just me, but it seems that South Carolina's roads typically don't drain very well. It's been years since I lived in Ohio, but I don't recall dealing with water in the roads as much when I lived there. The birthday party was late on account of the weather, and attendees got to enjoy another episode of rain and hail, but the party went well, all things considered. A big thanks to everyone who braved the storms to be here. Monday, March 10. 2008Determining Image File Types in Ruby
Today I came across a PNG file that had been uploaded from a browser with a .JPG extension and image/jpeg MIME type. It's too bad that MIME types are apparently unreliable when it comes to file uploads. I went looking for a way to determine the file type by actually reading the file. This is probably a solved problem, but I was unsuccessful Googling for the answer. I came up with the following Ruby method which decides the image file type using up to the first 10 bytes:
def image_type(file) case IO.read(file, 10) when /^GIF8/: 'gif' when /^\x89PNG/: 'png' when /^\xff\xd8\xff\xe0\x00\x10JFIF/: 'jpg' when /^\xff\xd8\xff\xe1(.*){2}Exif/: 'jpg' else 'unknown' end end This works well on a small set of test files (400+ from a browser temp files directory). Let me know if there's a case where this code doesn't work, or if there's a better solution in general. Update: This idea was expanded and found its way into a gem: ruby-imagespec. Credit for that goes to Brandon Anderson, Michael Sheakoski, and Dimitrij Denissenko.
(Page 1 of 1, totaling 2 entries)
|
CategoriesQuicksearchSyndicate This Blog |