Convert images for printing with MagickWand for PHP
Takeaway: Phillip Perkins shows you how to use MagickWand for PHP to convert individual page images into bitmaps before printing. He also explains what you need to do to customize his solution for your own project.
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 server. (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.)
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 file.
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, PRINTER_RESOLUTION_Y, PRINTER_PAPER_WIDTH, and PRINTER_PAPER_LENGTH.
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 up.
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
- PHP Printer functions
- MagickWand for PHP's help file: There's a CHM and a compressed ZIP available for download. Within that download, you'll find the various MagickWand function calls with the appropriate parameters. You may want to read up on digital imaging before tackling anything complicated.
- MSDN's information on printing in Windows' environments: You'll get details on printing using Windows printing API and Windows GDI.
Miss a column?
Check out the Web Development Zone archive, and catch up on the most recent editions of Phillip Perkins' column.
SponsoredWhite Papers, Webcasts, and Downloads
- Your organization's data is at risk--Protect it with Undelete 5! Diskeeper
- Enterprise IP PBX Buyers Guide - Features and Services That Matter VoIP-News
- The Shortcut Guide to Managing Disk Fragmentation - Chapter 2 Diskeeper
- Finance Accounting Solutions Buyer's Guide Inside Business Finance
- Is Real-Time Defragmentation Needed in Today's Environment? Diskeeper
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


