In last week’s article, I set the
stage for creating a printing solution for the Web using PHP. The Printer
functions available for PHP are Windows API wrappers, so only Windows 98/Me/NT/2000/XP
operating systems are supported. In my solution, I am interested in printing
image files, particularly, multi-page TIFF images. However, the only printer
function that supports my needs is the printer_draw_bmp()
function. This is where ImageMagick
and MagickWand for PHP come into play. I’ll show you how to use MagickWand for
PHP to convert the individual page images into bitmaps before printing.

ImageMagick is a free imaging software suite, and MagickWand
for PHP is a PHP extension that works in conjunction with ImageMagick. Using ImageMagick
and MagickWand for PHP, I can convert images from various formats to BMP. Also, ImageMagick has support
for multi-page TIFFs.

To begin the process, first you need to install the
ImageMagick suite because the MagickWand for PHP extension uses the suite’s
libraries to perform the imaging tasks. If, like me, you run into problems
installing the software, I recommend downloading
installation instructions on ImageMagick’s discourse
. (A quick tip of my hat to ImageMagick: When I still encountered
installation issues, I went on their message board, and I got an updated DLL
that provided the functionality I needed. The guys at ImageMagick were able to
resolve my issue in a matter of days—a feat a third-party imaging suite vendor
I use at my full-time job can’t accomplish.)

Weekly development tips in your inbox

Keep your developer skills sharp by signing up for TechRepublic’s free Web Development Zone newsletter, delivered each Tuesday.

Automatically sign up today!

After you’ve installed the suite, you’ll find that using
MagickWand is a snap. (Be sure to add the extension to your PHP.ini file.) In
order to use MagickWand, you need to create an instance of MagickWand, which
you’ll do through the NewMagickWand() function. Load an image using the MagickReadImage() function. This function accepts two
parameters: the MagickWand resource—returned by the NewMagickWand() function—and the file path to the image.

Once the image is loaded, there are numerous functions that
you can use to manipulate or get information about the image. My interest lies
only in converting the individual images to BMPs. For
multi-page images, you can use both the MagickGetNumberImages()
function, which takes the Magick resource as a
parameter, to get the total number of images and the MagickSetImageIndex() function, which takes the Magick
resource parameter and the image index parameter, to set the active image. Once
you set the active image, set the save format with the MagickSetImageFormat() function. This function takes the Magick resource and the format as a string value. For BMP,
the string format is “BMP”. Last is the MagickWriteImage()
function to save the image to the converted format. It takes two parameters:
the Magick resource and the pathname for the new

When you draw the image file to the output device context
during the printing operations, you’ll want to scale the image to fit the paper
size. You can get the width and height of the output image with the Printer
functions. You’ll also need the resolution to calculate the pixel width and
height. One function gets you all this information: printer_get_option(). This function takes the printer handle as a
parameter and one of several pre-defined constants: PRINTER_RESOLUTION_X,

For the sake of brevity, the best thing to do at this point
is to throw all this functionality together into a clear, concise solution. See
Listing A.

The first thing I do is set the timeout of the script to 5
minutes; this is necessary on large image files. Next, I open the printer
resource to the specified printer. I then initialize MagickWand, read in the
uploaded image file, and store the count of images. I use the Printer functions
to store device context dimensions to local variables. As each image in the
list of images is iterated, the image is converted and then stored to a
temporary file. This temporary file along with the device context dimensions
are then used to draw the image to the printer device context. Finally, after
all the images have been printed, the printer is closed and the resources are cleaned

During the course of printing, I write status information to
the HTTP response output buffer and flush the response. This provides a sort of
“real-time” update for the user.

My original intention for this solution was to print scanned
document images to a printer connected to a Web server. The image size
calculations work well enough because the scanned page and the printed page
should be the same size: 8.5″ X 11″. However, if you decide you want to try
your hand at this solution and wish to print normal image files, you’ll need to
get the resolution difference between the printer device context and the
original image and scale accordingly.

Resource links

Miss a column?

Check out the Web Development Zone archive, and catch up on the most recent editions of Phillip Perkins’ column.