3 # Copyright 1999-2000, Gisle Aas.
5 # This library is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
11 use vars qw($VERSION @EXPORT_OK);
13 $VERSION = '1.02'; # $Date: 2000/11/10 18:17:32 $
16 *import = \&Exporter::import;
18 @EXPORT_OK = qw(image_info dim html_dim);
25 return { error => "Extra arguments reserved for future extensions" };
30 my $fh = Symbol::gensym();
31 open($fh, $source) || return _os_err("Can't open $source");
35 elsif (ref($source) eq "SCALAR") {
37 $source = IO::String->new($$source);
40 seek($source, 0, 0) or return _os_err("Can't rewind");
44 read($source, $head, 32) == 32 or return _os_err("Can't read head");
45 if (ref($source) eq "IO::String") {
46 # XXX workaround until we can trap seek() with a tied file handle
50 seek($source, 0, 0) or _os_err("Can't rewind");
53 if (my $format = determine_file_format($head)) {
55 my $mod = "Image::Info::$format";
56 my $sub = "$mod\::process_file";
57 my $info = bless [], "Image::Info::Result";
59 unless (defined &$sub) {
62 die "$mod did not define &$sub" unless defined &$sub;
65 &$sub($info, $source, @_);
68 return { error => $@ } if $@;
69 return wantarray ? @$info : $info->[0];
71 return { error => "Unrecognized file format" };
76 return { error => "$_[0]: $!",
81 %%DETERMINE_FILE_FORMAT%%
85 my $img = shift || return;
86 my $x = $img->{width} || return;
87 my $y = $img->{height} || return;
88 wantarray ? ($x, $y) : "${x}x$y";
98 package Image::Info::Result;
102 my($self, $n, $key) = splice(@_, 0, 3);
103 push(@{$self->[$n]{$key}}, @_);
110 for my $k (keys %$_) {
112 $_->{$k} = $a->[0] if @$a <= 1;
118 my($self, $n, $key, $delete) = @_;
119 my $v = $delete ? delete $self->[$n]{$key} : $self->[$n]{$key};
130 Image::Info - Extract meta information from image files
134 use Image::Info qw(image_info dim);
136 my $info = image_info("image.jpg");
137 if (my $error = $info->{error}) {
138 die "Can't parse image info: $error\n";
140 my $color = $info->{color_type};
142 my($w, $h) = dim($info);
146 This module provide functions to extract various kind of meta
147 information from image files. The following functions are provided by
148 the C<Image::Info> module:
152 =item image_info( $file )
154 =item image_info( \$imgdata )
156 This function takes the name of a file or a file handle as argument
157 and will return one or more hashes (actually hash references)
158 describing the images inside the file. If there is only one image in
159 the file only one hash is returned. In scalar context, only the hash
160 for the first image is returned.
162 In case of error, and hash containing the "error" key will be
163 returned. The corresponding value will be an appropriate error
166 If a reference to a scalar is passed as argument to this function,
167 then it is assumed that this scalar contains the raw image data
170 =item dim( $info_hash )
172 Takes an hash as returned from image_info() and returns the dimensions
173 ($width, $height) of the image. In scalar context returns the
174 dimensions as a string.
176 =item html_dim( $info_hash )
178 Returns the dimensions as a string suitable for embedding directly
179 into HTML <img>-tags. E.g.:
181 print "<img src="..." @{[html_dim($info)]}>\n";
185 =head1 Image descriptions
187 The image_info() function returns meta information about each image in
188 the form of a reference to a hash. The hash keys used are in most
189 cases based on the TIFF element names. All lower case keys are
190 mandatory for all file formats and will always be there unless an
191 error occured (in which case the "error" key will be present.) Mixed
192 case keys will only be present when the corresponding information
193 element is available in the image.
195 The following key names are common for any image format:
199 =item file_media_type
201 This is the MIME type that is appropriate for the given file format.
202 The corresponding value is a string like: "image/png" or "image/jpeg".
206 The is the suggested file name extention for a file of the given file
207 format. The value is a 3 letter, lowercase string like "png", "jpg".
211 This is the number of pixels horizontally in the image.
215 This is the number of pixels vertically in the image. (TIFF use the
216 name ImageLength for this field.)
220 The value is a short string describing what kind of values the pixels
221 encode. The value can be one of the following:
231 These names can also be prefixed by "Indexed-" if the image is
232 composed of indexes into a palette. Of these, only "Indexed-RGB" is
235 (It is similar to the TIFF field PhotometricInterpretation, but this
236 name was found to be too long, so we used the PNG inpired term
241 The value of this field normally gives the physical size of the image
242 on screen or paper. When the unit specifier is missing then this field
243 denotes the squareness of pixels in the image.
245 The syntax of this field is:
248 <xres> "/" <yres> <unit>
251 The <res>, <xres> and <yres> fields are numbers. The <unit> is a
252 string like C<dpi>, C<dpm> or C<dpcm> (denoting "dots per
255 =item SamplesPerPixel
257 This says how many channels there are in the image. For some image
258 formats this number might be higher than the number implied from the
263 This says how many bits are used to encode each of samples. The value
264 is a reference to an array containing numbers. The number of elements
265 in the array should be the same as C<SamplesPerPixel>.
269 Textual comments found in the file. The value is a reference to an
270 array if there are multiple comments found.
274 If the image is interlaced, then this tell which interlace method is
279 This tell which compression algorithm is used.
285 =item LastModificationTime
291 =head1 Supported Image Formats
293 The following image file formats are currently supported:
307 Copyright 1999-2000 Gisle Aas.
309 GIF fixes by Ralf Steines <metamonk@yahoo.com>
311 SVG, X*M support added by Jerrad Pierce <belg4mit@mit.edu>/<webmaster@pthbb.org>
313 This library is free software; you can redistribute it and/or
314 modify it under the same terms as Perl itself.