PDFlib Cookbook

cookbook

images/image_color_manipulation

Manipulate image colors with the 'decode' and 'chromakey' options.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Image color manipulation:
 * Manipulate image colors with the "decode" and "chromakey" options.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.2
 * Required data: image file
 */

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

$exitcode = 0;
$imagefile = "zebra.tif";
$x = 20; $y1 = 20; $y2 = 800;

try {
    $p = new PDFlib();

    $testcases= array(
        /* image option list                     effect achieved */
        array( "",                                   "original image" ),
        array("decode={1 0 1 0 1 0}",               "invert image colors (same as 'invert')" ),
        array("decode={-0.3 1.3 -0.3 1.3 -0.3 1.3}","increase contrast" ),
        array("decode={0.1 0.9 0.1 0.9 0.1 0.9}",   "reduce contrast" ),
        array("decode={0.2 1.2 0.2 1.2 0.2 1.2}",   "lighten all colors by 20%" ),
        array("decode={-0.1 0.9 -0.1 0.9 -0.1 0.9}","darken all colors by 10%" ),

        array("chromakey={0 77 0 77 0 77}",         "treat colors darker than 30% as transparent" ),
        array("chromakey={153 255 153 255 153 255}","treat colors lighter than 60% as transparent" ),                    
    );

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

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

    /* all strings are expected as utf8 */
    $p->set_option("stringformat=utf8");

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

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);

    foreach ($testcases as $testcase)
    {
        $textopt = "fontname=NotoSerif-Regular encoding=unicode fontsize=12";

        $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
        
        /* Output option list and description */
        if ($testcase[0] == "")
                $p->fit_textline("No image option: " . $testcase[1], $x, $y2, $textopt);
        else
                $p->fit_textline("Image option '" . $testcase[0] . "': " . $testcase[1],
                $x, $y2, $textopt);
        
        /* Load the image with option */
        $image = $p->load_image("auto", $imagefile, $testcase[0]);
        if ($image == 0)
                throw new Exception("Error: " . $p->get_errmsg());
                
        $p->fit_image($image, $x, $y1, "boxsize={540 800} fitmethod=meet");
        
        $p->close_image($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_color_manipulation.pdf");
    print $buf;

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

$p = 0;