Created
January 2, 2012 03:33
-
-
Save hiroaki/1549186 to your computer and use it in GitHub Desktop.
This program checks whether an image is able to be embedded by AtomicParsley.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
=pod | |
This program checks whether an image is able to be embedded by AtomicParsley. | |
=cut | |
use strict; | |
use warnings; | |
my $img = $ARGV[0] or die "usage: $0 image\n"; | |
open IN, "<$img" or die "image file could not be opened: $!\n"; | |
my $header = undef; | |
my $status = read IN, $header, 8; | |
close IN; | |
die "image file could not be read.\n" unless( defined $status ); | |
my @bytes = unpack("C*", $header); | |
printf '%d bytes read, %s%s', $status, join(' ', map { sprintf('%02X',$_) } @bytes), "\n"; | |
my $head8 = join '', ( map { sprintf('\x%02X', $_) } @bytes ); | |
my $head4 = join '', ( map { sprintf('\x%02X', $_) } (@bytes[0..3]) ); | |
if( $head4 eq q'\xFF\xD8\xFF\xE0' || $head4 eq q'\xFF\xD8\xFF\xE1' ){ | |
print "jpeg\n"; | |
}elsif( $head8 eq q'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A' ){ | |
print "png\n"; | |
}else{ | |
print "image file is not jpg/png and cannot be embedded.\n"; | |
} | |
1; | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment