On Jul 18, 10:20 am, John Joyce <dangerwillrobinsondan... / gmail.com>
wrote:

<snip>

> The file command and bindings to it are OK, but results are not  
> consistent across common image file types.  What's worse is that the  
> code would be unportable. Ideally, the solution would rely simply on  
> the file format internally and thus be portable.

How's this for a portable version? I based this on a 5 minute overview
of the Wikipedia content at http://en.wikipedia.org/wiki/Magic_number_(programming),
combined with some trial and error.

class File
   def self.image?(file)
      bmp?(file) || jpg?(file) || png?(file) || gif?(file)
   end

   def self.bmp?(file)
      IO.read(file, 3) == "BM6" && File.extname(file).downcase ==
'.bmp'
   end

   def self.jpg?(file)
      IO.read(file, 10) == "\377\330\377\340\000\020JFIF" &&
File.extname(file).downcase == '.jpg'
   end

   def self.png?(file)
      IO.read(file, 4) == "\211PNG" && File.extname(file).downcase ==
'.png'
   end

   def self.gif?(file)
      ['GIF89a', 'GIF97a'].include?(IO.read(file, 6)) &&
File.extname(file).downcase == '.gif'
   end
end

Regards,

Dan