Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Creating PDF documents Wish List | Report Bug | About Me ]

11.4.4     Adding imagery: pdf_open_image_file() and pdf_place_image()

This is NOT the latest copy of this book; click here for the latest version.

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.





<< 11.4.3 Adding more pages and more style: pdf_setcolor()   11.4.5 PDF special effects: pdf_rotate() and pdf_skew() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow