Adding imagery

int pdf_open_image_file ( resource pdfdoc, string imagetype, string filename [, string masktype [, int mask]])

int pdf_place_image ( resource pdfdoc, int image, float x, float y, float scale)

Let's face it - there is only so far you can go with plain text, no matter how many font faces and colours you use. Indeed, generally you should try to limit type face diversity if possible, because very often adding more words will not improve the presentation of your work, which is perhaps why people say a picture says a thousand words. Even if the words are multicoloured.

To this end, PHP provides us with two functions: 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.

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

Save this next example as pdf3.php. You will need to find a JPEG and place it in the same directory as the script as myimage.jpg before you run the script.

<?php
    $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 50% of its usual 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 then scaled at run-time.

This might seem odd, but it makes good sense when you realise the reason behind it. Owing to its saving pictures unscaled, the PDF format allows you to re-use images without having to store multiple copies in the file. So, if we go back to our earlier for loop where we had ten pages being generated, we get something like this:

<?php
    $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);
?>

Save that as pdf4.php and run it again - this time the your.pdf file should be only slightly larger than that generated by the previous script that had just one image. As you can see, it is actually quite helpful that PDF stores its graphics separately from the display, particularly when you re-use pictures a great deal.

 

Next chapter: PDF special effects >>

Previous chapter: Adding more pages and more style

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar