#!/usr/bin/perl #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,"$ARGV[0]") || 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); #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.