SYNOPSIS

    use Image::Size;
    # Get the size of globe.gif
    ($globe_x, $globe_y) = imgsize("globe.gif");
    # Assume X=60 and Y=40 for remaining examples

    use Image::Size 'html_imgsize';
    # Get the size as 'width="X" height="Y"' for HTML generation
    $size = html_imgsize("globe.gif");
    # $size == 'width="60" height="40"'

    use Image::Size 'attr_imgsize';
    # Get the size as a list passable to routines in CGI.pm
    @attrs = attr_imgsize("globe.gif");
    # @attrs == ('-width', 60, '-height', 40)

    use Image::Size;
    # Get the size of an in-memory buffer
    ($buf_x, $buf_y) = imgsize(\$buf);
    # Assuming that $buf was the data, imgsize() needed a
    $ reference to a scalar

DESCRIPTION

The Image::Size library is based upon the \*(C`wwwis\*(C' script written by Alex Knowles ([email protected]), a tool to examine \s-1HTML\s0 and add 'width' and 'height' parameters to image tags. The sizes are cached internally based on file name, so multiple calls on the same file name (such as images used in bulleted lists, for example) do not result in repeated computations.

SUBROUTINES/METHODS

Image::Size provides three interfaces for possible import:

imgsize(stream)

Returns a three-item list of the X and Y dimensions (width and height, in that order) and image type of stream. Errors are noted by undefined (undef) values for the first two elements, and an error string in the third. The third element can be (and usually is) ignored, but is useful when sizing data whose type is unknown.

html_imgsize(stream)

Returns the width and height (X and Y) of stream pre-formatted as a single string 'width="X" height="Y"' suitable for addition into generated \s-1HTML\s0 \s-1IMG\s0 tags. If the underlying call to \*(C`imgsize\*(C' fails, undef is returned. The format returned is dually suited to both \s-1HTML\s0 and \s-1XHTML\s0.

attr_imgsize(stream)

Returns the width and height of stream as part of a 4-element list useful for routines that use hash tables for the manipulation of named parameters, such as the Tk or \s-1CGI\s0 libraries. A typical return value looks like \*(C`("-width", X, "-height", Y)\*(C'. If the underlying call to \*(C`imgsize\*(C' fails, undef is returned.

By default, only \*(C`imgsize()\*(C' is exported. Any one or combination of the three may be explicitly imported, or all three may be with the tag :all.

Input Types

The sort of data passed as stream can be one of three forms:

string

If an ordinary scalar (string) is passed, it is assumed to be a file name (either absolute or relative to the current working directory of the process) and is searched for and opened (if found) as the source of data. Possible error messages (see \s-1DIAGNOSTICS\s0 below) may include file-access problems.

scalar reference

If the passed-in stream is a scalar reference, it is interpreted as pointing to an in-memory buffer containing the image data. # Assume that &read_data gets data somewhere (WWW, etc.) $img = &read_data; ($x, $y, $id) = imgsize(\$img); # $x and $y are dimensions, $id is the type of the image

Open file handle

The third option is to pass in an open filehandle (such as an object of the \*(C`IO::File\*(C' class, for example) that has already been associated with the target image file. The file pointer will necessarily move, but will be restored to its original position before subroutine end. # $fh was passed in, is IO::File reference: ($x, $y, $id) = imgsize($fh); # Same as calling with filename, but more abstract.

Recognized Formats

Image::Size natively understands and sizes data in the following formats:

\s-1GIF\s0
\s-1JPG\s0
\s-1XBM\s0
\s-1XPM\s0
\s-1PPM\s0 family (\s-1PPM/PGM/PBM\s0)
\s-1XV\s0 thumbnails
\s-1PNG\s0
\s-1MNG\s0
\s-1TIF\s0
\s-1BMP\s0
\s-1PSD\s0 (Adobe PhotoShop)
\s-1SWF\s0 (ShockWave/Flash)
\s-1CWS\s0 (FlashMX, compressed \s-1SWF\s0, Flash 6)
\s-1PCD\s0 (Kodak PhotoCD, see notes below)
\s-1EMF\s0 (Windows Enhanced Metafile Format)

Additionally, if the Image::Magick module is present, the file types supported by it are also supported by Image::Size. See also \*(L"\s-1CAVEATS\s0\*(R".

When using the \*(C`imgsize\*(C' interface, there is a third, unused value returned if the programmer wishes to save and examine it. This value is the identity of the data type, expressed as a 2-3 letter abbreviation as listed above. This is useful when operating on open file handles or in-memory data, where the type is as unknown as the size. The two support routines ignore this third return value, so those wishing to use it must use the base \*(C`imgsize\*(C' routine.

Note that when the Image::Magick fallback is used (for all non-natively supported files), the data type identity comes directly from the 'format' parameter reported by Image::Magick, so it may not meet the 2-3 letter abbreviation format. For example, a \s-1WBMP\s0 file might be reported as 'Wireless Bitmap (level 0) image' in this case. When a filename is passed to any of the sizing routines, the default behavior of the library is to cache the resulting information. The modification-time of the file is also recorded, to determine whether the cache should be purged and updated. This was originally added due to the fact that a number of \s-1CGI\s0 applications were using this library to generate attributes for pages that often used the same graphical element many times over.

However, the cacheing can lead to problems when the files are generated dynamically, at a rate that exceeds the resolution of the modification-time value on the filesystem. Thus, the optionally-importable control variable $NO_CACHE has been introduced. If this value is anything that evaluates to a non-false value (be that the value 1, any non-null string, etc.) then the cacheing is disabled until such time as the program re-enables it by setting the value to false.

The parameter $NO_CACHE may be imported as with the imgsize routine, and is also imported when using the import tag \*(C`:all\*(C'. If the programmer chooses not to import it, it is still accessible by the fully-qualified package name, $Image::Size::NO_CACHE.

Sharing the Cache Between Processes

If you are using Image::Size in a multi-thread or multi-process environment, you may wish to enable sharing of the cached information between the processes (or threads). Image::Size does not natively provide any facility for this, as it would add to the list of dependencies.

To make it possible for users to do this themselves, the %CACHE hash-table that Image::Size uses internally for storage may be imported in the use statement. The user may then make use of packages such as \s-1IPC::MMA\s0 (\s-1IPC::MMA\s0) that can \*(C`tie\*(C' a hash to a shared-memory segment:

use Image::Size qw(imgsize %CACHE); use IPC::MMA;

...

tie %CACHE, 'IPC::MM::Hash', $mmHash; # $mmHash via mm_make_hash # Now, forked processes will share any changes made to the cache

Sizing PhotoCD Images

With version 2.95, support for the Kodak PhotoCD image format is included. However, these image files are not quite like the others. One file is the source of the image in any of a range of pre-set resolutions (all with the same aspect ratio). Supporting this here is tricky, since there is nothing inherent in the file to limit it to a specific resolution.

The library addresses this by using a scale mapping, and requiring the user (you) to specify which scale is preferred for return. Like the $NO_CACHE setting described earlier, this is an importable scalar variable that may be used within the application that uses Image::Size. This parameter is called $PCD_SCALE, and is imported by the same name. It, too, is also imported when using the tag \*(C`:all\*(C' or may be referenced as $Image::Size::PCD_SCALE.

The parameter should be set to one of the following values:

base/16 base/4 base base4 base16 base64

Note that not all PhotoCD disks will have included the \*(C`base64\*(C' resolution. The actual resolutions are not listed here, as they are constant and can be found in any documentation on the \s-1PCD\s0 format. The value of $PCD_SCALE is treated in a case-insensitive manner, so \*(C`base\*(C' is the same as \*(C`Base\*(C' or \*(C`BaSe\*(C'. The default scale is set to \*(C`base\*(C'.

Also note that the library makes no effort to read enough of the \s-1PCD\s0 file to verify that the requested resolution is available. The point of this library is to read as little as necessary so as to operate efficiently. Thus, the only real difference to be found is in whether the orientation of the image is portrait or landscape. That is in fact all that the library extracts from the image file.

Controlling Behavior with \s-1GIF\s0 Images

\s-1GIF\s0 images present a sort of unusual situation when it comes to reading size. Because GIFs can be a series of sub-images to be isplayed as an animated sequence, what part does the user want to get the size for?

When dealing with \s-1GIF\s0 files, the user may control the behavior by setting the global value $Image::Size::GIF_BEHAVIOR. Like the \s-1PCD\s0 setting, this may be imported when loading the library. Three values are recognized by the GIF-handling code:

0

This is the default value. When this value is chosen, the returned dimensions are those of the \*(L"screen\*(R". The \*(L"screen\*(R" is the display area that the \s-1GIF\s0 declares in the first data block of the file. No sub-images will be greater than this in size; if they are, the specification dictates that they be cropped to fit within the box. This is also the fastest method for sizing the \s-1GIF\s0, as it reads the least amount of data from the image stream.

1

If this value is set, then the size of the first sub-image within the \s-1GIF\s0 is returned. For plain (non-animated) \s-1GIF\s0 files, this would be the same as the screen (though it doesn't have to be, strictly-speaking). When the first image descriptor block is read, the code immediately returns, making this only slightly-less efficient than the previous setting.

2

If this value is chosen, then the code loops through all the sub-images of the animated \s-1GIF\s0, and returns the dimensions of the largest of them. This option requires that the full \s-1GIF\s0 image be read, in order to ensure that the largest is found.

Any value outside this range will produce an error in the \s-1GIF\s0 code before any image data is read.

The value of dimensions other than the view-port (\*(L"screen\*(R") is dubious. However, some users have asked for that functionality.

Image::Size AND WEBSERVERS

There are a few approaches to getting the most out of Image::Size in a multi-process webserver environment. The two most common are pre-caching and using shared memory. These examples are focused on Apache, but should be adaptable to other server approaches as well.

Pre-Caching Image Data

One approach is to include code in an Apache start-up script that reads the information on all images ahead of time. A script loaded via \*(C`PerlRequire\*(C', for example, becomes part of the server memory before child processes are created. When the children are created, they come into existence with a pre-primed cache already available.

The shortcoming of this approach is that you have to plan ahead of time for which image files you need to cache. Also, if the list is long-enough it can slow server start-up time.

The advantage is that it keeps the information centralized in one place and thus easier to manage and maintain. It also requires no additional \s-1CPAN\s0 modules.

Shared Memory Caching

Another approach is to introduce a shared memory segment that the individual processes all have access to. This can be done with any of a variety of shared memory modules on \s-1CPAN\s0.

Probably the easiest way to do this is to use one of the packages that allow the tying of a hash to a shared memory segment. You can use this in combination with importing the hash table variable that is used by Image::Size for the cache, or you can refer to it explicitly by full package name:

use IPC::Shareable; use Image::Size;

tie %Image::Size::CACHE, 'IPC::Shareable', 'size', { create => 1 };

That example uses IPC::Shareable (see IPC::Shareable) and uses the option to the \*(C`tie\*(C' command that tells IPC::Shareable to create the segment. Once the initial server process starts to create children, they will all share the tied handle to the memory segment.

Another package that provides this capability is \s-1IPC::MMA\s0 (see \s-1IPC::MMA\s0), which provides shared memory management via the mm library from Ralf Engelschall (details available in the documentation for \s-1IPC::MMA\s0):

use IPC::MMA; use Image::Size qw(%CACHE);

my $mm = mm_create(65536, '/tmp/test_lockfile'); my $mmHash = mm_make_hash($mm); tie %CACHE, 'IPC::MM::Hash', $mmHash;

As before, this is done in the start-up phase of the webserver. As the child processes are created, they inherit the pointer to the existing shared segment.

MORE EXAMPLES

The attr_imgsize interface is also well-suited to use with the Tk extension:

$image = $widget->Photo(-file => $img_path, attr_imgsize($img_path));

Since the \*(C`Tk::Image\*(C' classes use dashed option names as \*(C`CGI\*(C' does, no further translation is needed.

This package is also well-suited for use within an Apache web server context. File sizes are cached upon read (with a check against the modified time of the file, in case of changes), a useful feature for a mod_perl environment in which a child process endures beyond the lifetime of a single request. Other aspects of the mod_perl environment cooperate nicely with this module, such as the ability to use a sub-request to fetch the full pathname for a file within the server space. This complements the \s-1HTML\s0 generation capabilities of the \s-1CGI\s0 module, in which \*(C`CGI::img\*(C' wants a \s-1URL\s0 but \*(C`attr_imgsize\*(C' needs a file path:

# Assume $Q is an object of class CGI, $r is an Apache request object. # $imgpath is a URL for something like "/img/redball.gif". $r->print($Q->img({ -src => $imgpath, attr_imgsize($r->lookup_uri($imgpath)->filename) }));

The advantage here, besides not having to hard-code the server document root, is that Apache passes the sub-request through the usual request lifecycle, including any stages that would re-write the \s-1URL\s0 or otherwise modify it.

DIAGNOSTICS

The base routine, \*(C`imgsize\*(C', returns undef as the first value in its list when an error has occured. The third element contains a descriptive error message.

The other two routines simply return undef in the case of error.

CAVEATS

Caching of size data can only be done on inputs that are file names. Open file handles and scalar references cannot be reliably transformed into a unique key for the table of cache data. Buffers could be cached using the \s-1MD5\s0 module, and perhaps in the future I will make that an option. I do not, however, wish to lengthen the dependency list by another item at this time.

As Image::Magick operates on file names, not handles, the use of it is restricted to cases where the input to \*(C`imgsize\*(C' is provided as file name.

RELATED TO Image::Size…

Image::Magick and Image::Info Perl modules at \s-1CPAN\s0. The Graphics::Magick Perl \s-1API\s0 at <http://www.graphicsmagick.org/perl.html>.

CONTRIBUTORS

Perl module interface by Randy J. Ray ([email protected]), original image-sizing code by Alex Knowles ([email protected]) and Andrew Tong ([email protected]), used with their joint permission.

Some bug fixes submitted by Bernd Leibing ([email protected]). \s-1PPM/PGM/PBM\s0 sizing code contributed by Carsten Dominik ([email protected]). Tom Metro ([email protected]) re-wrote the \s-1JPG\s0 and \s-1PNG\s0 code, and also provided a \s-1PNG\s0 image for the test suite. Dan Klein ([email protected]) contributed a re-write of the \s-1GIF\s0 code. Cloyce Spradling ([email protected]) contributed \s-1TIFF\s0 sizing code and test images. Aldo Calpini ([email protected]) suggested support of \s-1BMP\s0 images (which I really should have already thought of :-) and provided code to work with. A patch to allow html_imgsize to produce valid output for \s-1XHTML\s0, as well as some documentation fixes was provided by Charles Levert ([email protected]). The ShockWave/Flash support was provided by Dmitry Dorofeev ([email protected]). Though I neglected to take note of who supplied the \s-1PSD\s0 (PhotoShop) code, a bug was identified by Alex Weslowski <[email protected]>, who also provided a test image. \s-1PCD\s0 support was adapted from a script made available by Phil Greenspun, as guided to my attention by Matt Mueller [email protected]. A thorough read of the documentation and source by Philip Newton [email protected] found several typos and a small buglet. Ville Skytta\*: ([email protected]) provided the \s-1MNG\s0 and the Image::Magick fallback code. Craig MacKenna ([email protected]) suggested making the cache available so that it could be used with shared memory, and helped test my change before release.

BUGS

Please report any bugs or feature requests to \*(C`bug-image-size at rt.cpan.org\*(C', or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Size <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Size>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

  • \s-1RT:\s0 \s-1CPAN\s0's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Size <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Size>

  • AnnoCPAN: Annotated \s-1CPAN\s0 documentation http://annocpan.org/dist/Image-Size <http://annocpan.org/dist/Image-Size>

  • \s-1CPAN\s0 Ratings http://cpanratings.perl.org/d/Image-Size <http://cpanratings.perl.org/d/Image-Size>

  • Search \s-1CPAN\s0 http://search.cpan.org/dist/Image-Size <http://search.cpan.org/dist/Image-Size>

  • Project page on GitHub http://github.com/rjray/image-size <http://github.com/rjray/image-size>

LICENSE AND COPYRIGHT

This file and the code within are copyright (c) 1996-2009 by Randy J. Ray.

Copying and distribution are permitted under the terms of the Artistic License 2.0 (http://www.opensource.org/licenses/artistic-license-2.0.php <http://www.opensource.org/licenses/artistic-license-2.0.php>) or the \s-1GNU\s0 \s-1LGPL\s0 2.1 (http://www.opensource.org/licenses/lgpl-2.1.php <http://www.opensource.org/licenses/lgpl-2.1.php>).

AUTHOR

Randy J. Ray \*(C`<[email protected]>\*(C'