PDFlib Cookbook

cookbook

images/image_mask

Place an image and apply a mask to it.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Image mask:
 * Place an image with a mask applied to it
 * 
 * Place an image and apply a grayscale mask on it. Depending on the values
 * contained in the mask, the image pixels will be displayed more or less 
 * transparent. Smaller (darker) mask values result in more transparent image 
 * pixels and larger (lighter) values in less transparency.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file, grayscale mask image file
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Image Mask";

$imagefile = "sky.jpg";
$maskfile = "image_mask.jpg";

try {
    $p = new pdflib();

    $p->set_option("searchpath={" . $searchpath . "}");

    /* This means we must check return values of load_font() etc. */
    $p->set_option("errorpolicy=return");

    if ($p->begin_document($outfile, "") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /* Load the original image */
    $image = $p->load_image("auto", $imagefile, "");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    /*
     * Load the grayscale image to be used as soft mask.
     * It must not have an ICC profile, as an image with an ICCBased
     * colorspace cannot be used as a mask.
     */
    $mask = $p->load_image("auto", $maskfile, "honoriccprofile=false");
    if ($mask == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    /* Load the image with the mask assigned to it */
    $masked_image = $p->load_image("auto", $imagefile, "masked " . $mask);
    if ($masked_image == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    /* Load font */
    $font = $p->load_font("NotoSerif-Regular", "unicode", "");
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    /* Start a page with a size similar to the image dimensions. */
    $p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
    
    /* Place the original image */
    $p->fit_image($image, 50, 150, "boxsize {200 500} fitmethod=meet");
    $p->fit_textline("Original image", 50, 130, "font=" . $font .
        " fontsize=10");
    
    /* Place the image without the clipping path applied to it */
    $p->fit_image($mask, 300, 150, "boxsize {200 500} fitmethod=meet");
    $p->fit_textline("Soft mask", 300, 130, "font=" . $font .
        " fontsize=10");
    
    /* Place the image with the clipping path applied to it */
    $p->fit_image($masked_image, 550, 150, "boxsize {200 500} fitmethod=meet");
    $p->fit_textline("Image with soft mask applied: darker mask", 550, 130,
        "font=" . $font . " fontsize=10");
    $p->fit_textline("values result in more transparent image " .
        "values.", 550, 110, "font=" . $font . " fontsize=10");
            
    /* Close the images */
    $p->close_image($image);
    $p->close_image($mask);
    $p->close_image($masked_image);
 
    $p->end_page_ext("");
    $p->end_document("");
    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=image_mask.pdf");
    print $buf;

} catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n".
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Throwable $e) {
    echo("PHP exception occurred: " . $e->getMessage() . "\n");
    exit(1);
}

$p = 0;

?>