View Single Post
  #2 (permalink)  
Old 08-22-2007, 02:56 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Post some basic image handling functions using perl

hi all,

The following functions are provided by the Image::Info module:

image_info( $file )
image_info( \$imgdata )
image_info( $file, key => value,... )


This function takes the name of a file or a file handle as argument and will return one or more hashes (actually hash references) describing the images inside the file. If there is only one image in the file only one hash is returned. In scalar context, only the hash for the first image is returned.

In case of error, and hash containing the "error" key will be returned. The corresponding value will be an appropriate error message.

If a reference to a scalar is passed as argument to this function, then it is assumed that this scalar contains the raw image data directly.

The image_info() function also take optional key/value style arguments that can influence what information is returned.
image_type( $filename )

Returns a hash with only one key, file_type. The value will be the type of the file. On error, sets the two keys error and Errno.
image_info( \$imgdata )

This function is a dramatically faster alternative to the image_info function for situations in which you only need to find the image type.

It uses only the internal file-type detection to do this, and thus does not need to load any of the image type-specific driver modules, and does not access to entire file. It also only needs access to the first 32 bytes of the file.

To maintain some level of compatibility with image_info, image_type returns in the same format, with the same error message style. That is, it returns a HASH reference, with the $type->{error} key set if there was an error.

On success, the HASH reference will contain the single key 'file_type', which represents the type of the file, expressed as the type code used for the various drivers ('GIF', 'JPEG', 'TIFF' and so on).

If there are multiple images within the file they will be ignored, as this function provides only the type of the overall file, not of the various images within it. This function will not return multiple hashes if the file contains multiple images.

Of course, in all (or at least effectively all) cases the type of the images inside the file is going to be the same as that of the file itself.
dim( $info_hash )

Takes an hash as returned from image_info() and returns the dimensions ($width, $height) of the image. In scalar context returns the dimensions as a string.
html_dim( $info_hash )

Returns the dimensions as a string suitable for embedding directly into HTML or SVG <img>-tags.

determine_file_format( $filedata )

Determines the file format from the passed file data (a normal Perl scalar containing the first bytes of the file), and returns either undef for an unknown file format, or a string describing the format, like "BMP" or "JPEG".
Reply With Quote