1 package File::Type::WebImages;
6 @EXPORT_OK = 'mime_type';
10 our $VERSION = "1.01";
13 # magically route argument
16 return undef unless defined $argument;
18 if (length $argument > 1024 || $argument =~ m/\n/) {
19 # assume it's data. Saves a stat call if the data's long
20 # also avoids stat warning if there's a newline
21 return checktype_contents($argument);
26 return checktype_filename($argument);
28 return undef; # directories don't have mime types
31 # otherwise, fall back to checking the string as if it's data again
32 return checktype_contents($argument);
35 # reads in 16k of selected file, or returns undef if can't open,
36 # then checks contents
37 sub checktype_filename {
39 my $fh = IO::File->new($filename) || return undef;
41 $fh->read($data, 16*1024);
43 return checktype_contents($data);
46 # Matches $data against the magic database criteria and returns the MIME
48 sub checktype_contents {
52 return undef unless defined $data;
54 if ($data =~ m[^\x89PNG]) {
57 elsif ($data =~ m[^GIF8]) {
60 elsif ($data =~ m[^BM]) {
64 if (length $data > 1) {
65 $substr = substr($data, 1, 1024);
66 if (defined $substr && $substr =~ m[^PNG]) {
70 if (length $data > 0) {
71 $substr = substr($data, 0, 2);
72 if (pack('H*', 'ffd8') eq $substr ) {
86 File::Type::WebImages - determine web image file types using magic
90 use File::Type::WebImages 'mime_type';
92 my $type_1 = mime_type($file);
93 my $type_2 = mime_type($data);
97 C<mime_type()> can use either a filename, or file contents, to determine the
98 type of a file. The process involves looking the data at the beginning of the file,
99 sometimes called "magic numbers".
101 =head1 THE BIG TRADE OFF
103 For minimum memory consumption, only the following common web image file types are supported:
105 BMP, GIF, JPEG and PNG.
106 ( image/bmp, image/gif, image/jpeg and image/png ).
108 Unlike with L<File::Type> and L<File::MMagic>, 'undef', not
109 "application/octet-stream" will be returned for unknown formats.
111 Unlike L<File::Type>, we return "image/png" for PNGs, I<not> "image/x-png";
113 If you want more mime types detected use L<File::Type> or some other module.
117 It would be even better to have a pluggable system that would allow you
118 to plug-in different sets of MIME-types you care about.
122 L<File::Type>. Similar, but supports over 100 file types.
124 =head1 ACKNOWLEDGMENTS
126 File::Type::WebImages is built from a mime-magic file from cleancode.org. The original
127 can be found at L<http://cleancode.org/cgi-bin/viewcvs.cgi/email/mime-magic.mime?rev=1.1.1.1>.
131 Paul Mison <pmison@fotango.com> - wrote original File::Type
132 Mark Stosberg <mark@summersault.com> - hacked up this.
136 Copyright 2003-2004 Fotango Ltd.
140 Licensed under the same terms as Perl itself.