From: John Joyce Date: 2007-07-19T12:46:04+09:00 Subject: Re: Validating an Image file is an image file --Apple-Mail-1-351141013 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed On Jul 18, 2007, at 4:13 PM, Daniel Berger wrote: > On Jul 18, 10:20 am, John Joyce > wrote: > > > >> 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/=20 > 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) =3D=3D "BM6" && File.extname(file).downcase =3D=3D= > '.bmp' > end > > def self.jpg?(file) > IO.read(file, 10) =3D=3D "\377\330\377\340\000\020JFIF" && > File.extname(file).downcase =3D=3D '.jpg' > end > > def self.png?(file) > IO.read(file, 4) =3D=3D "\211PNG" && File.extname(file).downcase = =3D=3D > '.png' > end > > def self.gif?(file) > ['GIF89a', 'GIF97a'].include?(IO.read(file, 6)) && > File.extname(file).downcase =3D=3D '.gif' > end > end > > Regards, > > Dan > > I've not had time to test it out or kick the tires yet, but I've =20 dubbed it 'The Daniel Berger Detector' =EF=BF=BC John Joyce= --Apple-Mail-1-351141013 Content-Type: multipart/mixed; boundary=Apple-Mail-2-351141013 --Apple-Mail-2-351141013 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1
On Jul 18, 2007, at 4:13 PM, Daniel Berger wrote:

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 =A0

consistent across common image = file types.=A0 What's worse = is that the =A0

=

code would be unportable. = Ideally, the solution would rely simply on =A0

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

=A0=A0 def = self.image?(file)

=A0 =A0 =A0 = bmp?(file) || jpg?(file) || png?(file) || gif?(file)

=

=A0=A0 end


=A0=A0 def = self.bmp?(file)

=A0 =A0 =A0 = IO.read(file, 3) =3D=3D "BM6" && = File.extname(file).downcase =3D=3D

'.bmp'

=A0=A0 = end


=A0=A0 def = self.jpg?(file)

=A0 =A0 =A0 = IO.read(file, 10) =3D=3D "\377\330\377\340\000\020JFIF" = &&

File.extname(file).downcase =3D=3D '.jpg'

=A0=A0= end


=A0=A0 def = self.png?(file)

=A0 =A0 =A0 = IO.read(file, 4) =3D=3D "\211PNG" && = File.extname(file).downcase =3D=3D

'.png'

=A0=A0 = end


=A0=A0 def = self.gif?(file)

=A0 =A0 =A0 = ['GIF89a', 'GIF97a'].include?(IO.read(file, 6)) = &&

File.extname(file).downcase =3D=3D '.gif'

=A0=A0= end

end


Regards,


Dan



I've not had time to = test it out or kick the tires yet, but I've dubbed it 'The Daniel Berger = Detector'
= --Apple-Mail-2-351141013 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; x-mac-creator=54784D74; name=daniel_berger_detector.rb Content-Disposition: attachment; filename=daniel_berger_detector.rb #!/usr/bin/env ruby # The Daniel Berger Detector # A script to detect graphics file formats accurately. Uses the internal 'magic nubmers' and reads into the files. =begin Subject: Re: Validating an Image file is an image file From: djberg96@gmail.com Date: July 18, 2007 4:13:49 PM CDT To: ruby-talk@ruby-lang.org Reply-To: ruby-talk@ruby-lang.org On Jul 18, 10:20 am, John Joyce wrote: 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. Regards, Dan =end 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 --Apple-Mail-2-351141013 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=US-ASCII

John Joyce
--Apple-Mail-2-351141013-- --Apple-Mail-1-351141013--