Image::Size - read the dimensions of an image in several popular formats |
Image::Size - read the dimensions of an image in several popular formats
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 "HEIGHT=X WIDTH=Y" for HTML generation $size = html_imgsize("globe.gif"); # $size == "HEIGHT=40 WIDTH=60"
use Image::Size 'attr_imgsize'; # Get the size as a list passable to routines in CGI.pm @attrs = attr_imgsize("globe.gif"); # @attrs == ('-HEIGHT', 40, '-WIDTH', 60)
use Image::Size; # Get the size of an in-memory buffer ($buf_x, $buf_y) = imgsize($buf);
The Image::Size library is based upon the wwwis
script written by
Alex Knowles (alex@ed.ac.uk), a tool to examine HTML and add HEIGHT and
WIDTH 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.
Image::Size provides three interfaces for possible import:
"HEIGHT=X WIDTH=Y"
suitable for addition into generated HTML IMG
tags. If the underlying call to imgsize
fails, undef is returned.
("-HEIGHT", Y, "-WIDTH", X)
. If the underlying call to imgsize
fails,
undef is returned.
By default, only imgsize()
is imported. Any one or
combination of the three may be imported, or all three may be with the
tag :all.
The sort of data passed as stream can be one of three forms:
# 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
IO::File
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.
Image::Size understands and sizes data in the following formats:
When using the imgsize
interface, there is a third, unused value returned
if the programmer wishes to save and examine it. This value is the three-
letter identity of the data type. 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 imgsize
routine.
The base routine, imgsize
, 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.
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 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.
http://www.tardis.ed.ac.uk/~ark/wwwis/
for a description of wwwis
and how to obtain it.
Perl module interface by Randy J. Ray (rjray@uswest.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.
Image::Size - read the dimensions of an image in several popular formats |