2
votes

I'm converting images from RGB to CMYK with IMagick in PHP.

During conversion some images get black grids on them.

Code:

$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage(SITE_ROOT . 'userfiles/image/products/' . $image); 
$IMagick->negateImage(false, Imagick::CHANNEL_ALL);
$IMagick->setImageColorspace(13);
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebCoatedSWOP.icc'); 
$IMagick->profileImage('icc', $icc_cmyk); 
unset($icc_cmyk); 
$IMagick->setImageColorspace(12);
$IMagick->writeImage (SITE_ROOT . 'userfiles/image/products/cmyk/' . $image);

Images:

Original

Converted

I'm converting around 80 images in a loop and most of them are OK. Any idea why it happens?

EDIT: Working code:

$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage(SITE_ROOT . 'userfiles/image/products/' . $image); 
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebCoatedSWOP.icc'); 
$IMagick->profileImage('icc', $icc_cmyk); 
unset($icc_cmyk); 
$IMagick->transformImageColorspace(12);
$IMagick->writeImage (SITE_ROOT . 'userfiles/image/products/cmyk/' . $image);
1
setImageColorspace "Sets the image colorspace. This method should be used when creating new images. To change the colorspace of an existing image, you should use Imagick::transformImageColorspace()." - Danack
Worked. Also fixed the problem with inverted colors. So no need to negateImage. Could you post it as an anwser so I can accept it. - Siim Somma

1 Answers

0
votes

setImageColorspace should only be used when creating new images, either through Imagick::newPseudoImage or rendering an ImagickDraw instance into an image.

For existing images, the correct method to change the colorspace of the image is Imagick::transformImageColorspace.