Section 17.3. Adding Images


17.3. Adding Images

PHP provides us with two functions for using images in PDFs : pdf_open_image_file( ) and pdf_place_image( ). The former reads a specified image type (parameter two) of a specified file name (parameter three) and returns an image that can be used in subsequent functions.

The pdf_place_image( ) function then takes the returned image as its second parameter, and also allows you to specify the X coordinate (parameter three), Y coordinate (parameter four), and any scaling (parameter five) you wish to be applied to the image.

For this next example, you will need to find a JPEG, name it myimage.jpg, and place it in the same directory as the script before you run the script.

     $pdf = pdf_new( );     pdf_open_file($pdf, "/path/to/your.pdf");     pdf_begin_page($pdf, 595, 842);     $testimage = pdf_open_image_file($pdf, "jpeg", "myimage.jpg");     pdf_place_image($pdf, $testimage, 0, 0, 0.5);     pdf_end_page($pdf);     pdf_close($pdf);     pdf_delete($pdf); 

In the above example, we set the scale parameter of pdf_place_image( ) (parameter five) to 0.5, which will show our myimage.jpg picture at half its original size. Note that altering the scale value of pictures will not change the final file size of the PDF that you output, because the file is saved unscaled and then scaled at run-time.

Owing to its saving pictures unscaled, the PDF format allows you to reuse images without having to store multiple copies in the file. So, if we go back to our earlier for loop where we had 10 pages being generated, we get something like this:

     $pdf = pdf_new( );     pdf_open_file($pdf, "/path/to/your.pdf");     $times = pdf_findfont($pdf, "Times-Roman", "host");     $timesb = pdf_findfont($pdf, "Times-Bold", "host");     $timesi = pdf_findfont($pdf, "Times-Italic", "host");     $testimage = pdf_open_image_file($pdf, "jpeg", "myimage.jpg");     for ($i = 1; $i < 10; ++$i) {             pdf_begin_page($pdf, 595, 842);             pdf_setcolor($pdf, 0.0, 0.0, 0.0);             pdf_setfont($pdf, $times, 24);             $scaleval = $i * 10 . '%';             $smallscale = 0.1 * $i;             pdf_show_xy($pdf, "This is page $i - $scaleval scale", 50, 750);             pdf_place_image($pdf, $testimage, 0, 0, $smallscale);             df_end_page($pdf);     }     pdf_close($pdf);     pdf_delete($pdf); 

The PDF file generated by that script will be only slightly larger than the previous file.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net