Name

newFromGif( ) — create a new image from a GIF file

Synopsis

GD::Image::newFromGif(filehandle)

The newFromGif( ) method will read data from a GIF file. This method requires that the filehandle parameter be a handle to a previously opened file. This gives you a bit of flexibility with the option of reading the GIF data from a file or (on Unix systems) from a previously opened pipe. The method will automatically read the data in binary mode but you must explicitly close the filehandle when you are through with it.

If the method succeeds, it will return a binary data stream representing the image data. If it fails (usually if the data is not actually a valid GIF file), it returns undef, in which case you should signal an error with die( ). For example:

open (GIFFILE, "burroughs.gif") || 
     die "Couldn't open file!";
my $image = newFromGif GD::Image(\*GIFFILE) || 
     die "Not a valid GIF file!";
close GIFFILE;

As a side note on Perl syntax, the generally approved method for passing a filehandle to a function is as a reference to a "typeglob,” as in:

somefunction(\*filehandle)

Passing the filehandle as a reference is preferable to passing it as a bare typeglob in that the former method will not complain if the use strict or use strict refs pragmas are in effect.[14]

[14] A typeglob is simply an abstract internal Perl type that was used to pass filehandles by reference before Perl had real references. The “strict” pragma is a compiler directive that affects how sloppily Perl will let you write ...

Get Programming Web Graphics with Perl and GNU Softwar now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.