PDFlib Cookbook

cookbook

color/iccprofile_to_image updated

Assign an ICC profile to an image.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 *
 * 
 * ICC profile to image:
 * Assign an ICC profile to an image
 *
 * Load an RGB image without any ICC profile.
 * Apply the "sRGB" ICC profile to an RGB image.
 * Apply the "ISOcoated" ICC profile to a CMYK image.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: RGB image file, CMYK image file, ICC profile
 */

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

$x = 100;
$y = 0;

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);
       
    /* Start page 1 */
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    // Load and output an RGB image without any ICC profile.
    $imagefile = "nesrin.jpg";
    $y = 450;
    
    /*
     * Load the RGB image. In the first example we ignore any ICC profile
     * which might be embedded into the image and also prevent the
     * PDFlib default of "iccprofile=sRGB" for RGB images.
     */
    $image = $p->load_image("auto", $imagefile, "iccprofile=none honoriccprofile=false");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());
        
    /* Fit the image proportionally into a box */
    $p->fit_image($image, $x, $y, "boxsize={400 300} fitmethod=meet");
    $p->fit_textline("RGB image without any ICC profile assigned", $x, $y -= 30, 
                    "fontname=NotoSerif-Regular fontsize=14");
    
    $p->close_image($image);
    
    /*
     * Load and output an RGB image with the "sRGB" ICC profile assigned to it:
     */
    
    /* Load the RGB image and assign the sRGB profile to it. Since
     * the sRGB profile is available internally in PDFlib it doesn't
     * have to be loaded with p.load_iccprofile().
     */

    $image = $p->load_image("auto", $imagefile, "iccprofile=sRGB");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());
        
        /* Fit the image proportionally into a box */
    $p->fit_image($image, $x, $y -= 350, "boxsize={400 300} fitmethod=meet");
    $p->fit_textline("RGB image with the \"sRGB\" ICC profile assigned", $x, 
                    $y -= 30, 
                    "fontname=NotoSerif-Regular fontsize=14");
    
    $p->close_image($image);
    
    $p->end_page_ext("");
   

    /* Start page 2 */
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    /*
     * Load and output a CMYK image without any ICC profile assigned to it:
     */
    $imagefile = "nesrin_cmyk.jpg";
    $y = 450;
    
    /*
     * Load the CMYK image. Just for our sample, ignore any ICC profile
     * which might be embedded into the image.
     */
    $image = $p->load_image("auto", $imagefile, "honoriccprofile=false");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());
        
        /* Fit the image proportionally into a box */
    $p->fit_image($image, $x, $y, "boxsize={400 300} fitmethod=meet");
    $p->fit_textline("CMYK image without any ICC profile assigned", $x, $y -= 30, 
                    "fontname=NotoSerif-Regular fontsize=14");
    
    $p->close_image($image);
    
    /*
     * Load and output a CMYK image with the "ISOcoated" ICC profile assigned
     * to it:
     */
    
    /* Load the ISOcoated profile */
    $icchandle = $p->load_iccprofile("ISOcoated_v2_eci.icc", "usage=iccbased");
    if ($icchandle == 0)
        throw new Exception("Error: " . $p->get_errmsg());
        
    /*
     * Load the CMYK image, ignore any ICC profile which might be 
     * embedded in the image, and assign the ISOcoated profile to it.
     */
    $image = $p->load_image("auto", $imagefile, 
                    "iccprofile=" . $icchandle . " honoriccprofile=false");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());
        
    /* Fit the image proportionally into a box */
    $p->fit_image($image, $x, $y -= 350, "boxsize={400 300} fitmethod=meet");
    $p->fit_textline("CMYK image with the \"ISOcoated\" ICC profile assigned", 
                    $x, $y -= 30, 
                    "fontname=NotoSerif-Regular  fontsize=14");
    
    $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=iccprofile_to_image.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;
?>