PDFlib Cookbook

cookbook

color/recombine_color_channels

Colorize images and place all colorized images on top of each other.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 *
 * 
 * Recombine split color channels
 *
 * This sample code expects N grayscale images as input, colorizes each image
 * with Cyan, Magenta, Yellow, and Black, respectively, and places all
 * colorized images on top of each other with blendmode=Darken. As a
 * result, the full recombined CMYK image is visible on the page.
 *
 * Using the parameters at the start of the code you can even recombine
 * more than four channels, or color channels other than C, M, Y, K.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: TIFF image file, CMYK image file, ICC profile
 */
$title = "Channel Recombination";
$basename = "zebra"; /* zebra_C.tif etc. */
$suffix = "tif";
$channelsuffix = array( "_c", "_m", "_y", "_k" );
$MAXCHANNEL = count($channelsuffix);

$channelnames = array( "Cyan", "Magenta", "Yellow", "Black" );

/* CMYK "alternate" values for the process color channels */
$alt = array(
    array( 1, 0, 0, 0 ),
    array( 0, 1, 0, 0 ),
    array( 0, 0, 1, 0 ),
    array( 0, 0, 0, 1 )
);

/* This is where font/image/PDF input files live. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";

try {
    $p = new pdflib();

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

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

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

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

    /* Load split channel images and colorize with a suitable spot color */
    for ($channel = 0; $channel < $MAXCHANNEL; $channel++) {
        $p->setcolor("fill", "cmyk", $alt[$channel][0], $alt[$channel][1],
            $alt[$channel][2], $alt[$channel][3]);
        $spot[$channel] = $p->makespotcolor($channelnames[$channel]);

        $filename = $basename . $channelsuffix[$channel] . "."
            . $suffix;
        $optlist = "colorize=" . $spot[$channel];
        $image[$channel] = $p->load_image("auto", $filename, $optlist);

        if ($image[$channel] == 0) {
            throw new Exception("Error: " . $p->get_errmsg());
        }
    }

    /* Set suitable blend mode for overlaying the images */
    $gs = $p->create_gstate("blendmode=Darken");

    /* Page size may be adjusted by PDF_fit_image() */
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height transparencygroup={colorspace=DeviceCMYK}");

    $p->save();
    $p->set_gstate($gs);

    for ($channel = 0; $channel < $MAXCHANNEL; $channel++) {
        $p->fit_image($image[$channel], 0.0, 0.0, "adjustpage");
        $p->close_image($image[$channel]);
    }
    $p->restore();

    $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=recombine_color_channels.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;
?>