Convert image from RGB to CMYK with Imagick

10,718

Solution 1

Take a look here:

<?php 
// don't use this (it inverts the image) 
//    $img->setImageColorspace (imagick::COLORSPACE_RGB); 

if ($img->getImageColorspace() == Imagick::COLORSPACE_CMYK) { 
   $profiles = $img->getImageProfiles('*', false); 
   // we're only interested if ICC profile(s) exist 
   $has_icc_profile = (array_search('icc', $profiles) !== false); 
   // if it doesnt have a CMYK ICC profile, we add one 
   if ($has_icc_profile === false) { 
       $icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebUncoated.icc'); 
       $img->profileImage('icc', $icc_cmyk); 
       unset($icc_cmyk); 
   } 
   // then we add an RGB profile 
   $icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc'); 
   $img->profileImage('icc', $icc_rgb); 
   unset($icc_rgb); 
} 

$img->stripImage (); // this will drop down the size of the image dramatically (removes all profiles) 
?>

Solution 2

Okay, so this is a tricky issue. I had the same problem and it took me days to solve this one. You need to negateImage(), please see my example and make sure you only do it for php 5.3.x as this issue is unique to that php vs.

$range = $jpeg->getQuantumRange();
$php_vs_arr = preg_split("/\./", phpversion());
$php_vs = $php_vs_arr[0] . '.' . $php_vs_arr[1];
if ($jpeg->getImageColorspace() == Imagick::COLORSPACE_CMYK) {

    //make sure cmyk color-space is set correctly
$jpeg->setImageColorspace(12);

// then we add an RGB profile
$icc_rgb = file_get_contents(FRAMEWORK_PATH . DS . 'color' . DS . 'AdobeRGB1998.icc');
$jpeg->profileImage('icc', $icc_rgb);
unset($icc_rgb);

//set color space to rgb
$jpeg->setImageColorspace(13);

//fix gamma, hue, saturation, brightness
if($php_vs < 5.3) {
    //ADJUST GAMMA BY 2.0 for 5.2.x
    $jpeg->levelImage(0, 2.0, $range['quantumRangeString']);
} else {
    //php 5.3 hack FOR INVERTED COLORS
    $jpeg->negateImage(false, Imagick::CHANNEL_ALL);
}

}
$jpeg->stripImage();

Note: my Imagick object is obviously $jpeg

Also, you will need to download the AdobeRGB1998.icc profile from the Adobe website, just do a google search for it.

I hope this helps, please mark it as the correct answer as a lot of folks have trouble with this.

Share:
10,718
Laurens Schuitemaker
Author by

Laurens Schuitemaker

Updated on June 08, 2022

Comments

  • Laurens Schuitemaker
    Laurens Schuitemaker almost 2 years

    I'm trying to convert RGB images to CMYK, because they need to be printed. I'm using this code:

    <?php
    $filePath = 'rgb.jpg';
    
    // First save image as png
    $image = new Imagick($filePath);
    $image->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
    $image->setImageCompressionQuality(0); 
    $image->setImageFormat("png");
    $filePath = 'rgb.png';
    $image->writeImage($filePath);
    $image->clear();
    $image->destroy();
    $image = null;
    
    // Convert colors
    $image = new Imagick($filePath);
    $image->stripImage();
    $image->setImageColorspace(Imagick::COLORSPACE_CMYK);
    $image->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
    $image->setImageCompressionQuality(0); 
    $image->setImageFormat("png");
    $filePath = 'cmyk.png';
    $image->writeImage($filePath);
    
    $image->clear();
    $image->destroy();
    $image = null;
    
    
    $fileUrl = 'http://www.product-designer.nl/rgb2cmyk/cmyk.png';
    ?>
    CMYK Image:<br/>
    <img src="<?php echo $fileUrl; ?>" width="400" /><br /><br />
    <?php
    $fileUrl = 'http://www.product-designer.nl/rgb2cmyk/rgb.png';
    ?>
    RGB Image:<br/>
    <img src="<?php echo $fileUrl ?>" width="400" />
    

    You can see the result on http://product-designer.nl/rgb2cmyk I don't know how, but somehow the colors on the image become inverted. I need to convert the image but the colors need to be as close to the RGB colors as possible.

    Does anyone know how to do this?

    Thanks