[EMAIL PROTECTED] wrote: > Hello, All: > > I need to check files to make sure that they are .gif, .jpg, or .png > graphic files before processing them. How can I verify that? > > e.g., In unix, the 'type' command returns the file's type. >
all .gif, .jpg and .png file have a special header id that identify their types. for example, starting from the 7th byte of a JPEG file, you should see the special id as: ox4A 0x46 0x49 0x46 and 0x00, the following checks for that: my $t = undef; my($one,$two,$three,$four,$five); open(IMG,"/tmp/showban.jpg") || die $!; sysread(IMG,$t,6); #-- discard the first 6 bytes #-- read the next five bytes sysread(IMG,$one,1); sysread(IMG,$two,1); sysread(IMG,$three,1); sysread(IMG,$four,1); sysread(IMG,$five,1); #-- unpack them and make sure they are the right magic ID #-- you can get the id from the JPEG specification if(unpack("C",$one) == 74 && unpack("C",$two) == 70 && unpack("C",$three) == 73 && unpack("C",$four) == 70 && unpack("C",$five) == 0){ print "jpeg\n"; }else{ print "not jpeg\n" } close(IMG); similarly, for a .gif file, use the following: open(IMG,"/tmp/res0.gif") || die $!; sysread(IMG,$t,3); ($one,$two,$three) = unpack("CCC",$t); if($one == 71 && $two == 73 && $three == 70){ print "gif\n"; }else{ print "not gif\n"; } close(IMG); you can do pretty much the same thing to png file. just go to google, search for png specification, find out what the maigc id should look like and just code it according to the specfication. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]