ActivePerl Documentation
|
NAMEImage::Size - read the dimensions of an image in several popular formats
SUPPORTED PLATFORMS
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);
DESCRIPTIONThe Image::Size library is based upon the Image::Size provides three interfaces for possible import:
By default, only
Input TypesThe sort of data passed as stream can be one of three forms:
Recognizd FormatsImage::Size understands and sizes data in the following formats:
When using the
DIAGNOSTICSThe base routine, The other two routines simply return undef in the case of error.
MORE EXAMPLESThe 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 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 HTML generation
capabilities of the CGI module, in which
# 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 URL or otherwise modify it.
CAVEATSCaching 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 MD5 module, and perhaps in the future I will make that an option. I do not, however, wish to lengthen the dependancy list by another item at this time.
SEE ALSO
AUTHORSPerl module interface by Randy J. Ray (rjray@tsoft.com), original image-sizing code by Alex Knowles (alex@ed.ac.uk) and Andrew Tong (werdna@ugcs.caltech.edu), used with their joint permission. Some bug fixes submitted by Bernd Leibing (bernd.leibing@rz.uni-ulm.de). PPM/PGM/PBM sizing code contributed by Carsten Dominik (dominik@strw.LeidenUniv.nl). Tom Metro (tmetro@vl.com) re-wrote the JPG and PNG code, and also provided a PNG image for the test suite. Dan Klein (dvk@lonewolf.com) contributed a re-write of the GIF code. Cloyce Spradling (cloyce@headgear.org) contributed TIFF sizing code and test images. Aldo Calpini (a.calpini@romagiubileo.it) suggested support of BMP 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 XHTML, as well as some documentation fixes was provided by Charles Levert (charles@comm.polymtl.ca).
|