The 5th Annual China PHP Conference


(PECL imagick 2.0.0)

Imagick::setImageColorspaceConfigure l'espace de couleurs de l'image


bool Imagick::setImageColorspace ( int $colorspace )

Configure l'espace de couleurs de l'image. Cette méthode doit être utilisée lors de la création de nouvelles images. Pour changer l'espace de couleurs d'une image existante, vous devez utiliser la méthode Imagick::transformImageColorspace().

Liste de paramètres


Une des constantes COLORSPACE

Valeurs de retour

Retourne TRUE en cas de succès.

Erreurs / Exceptions

Lance une exception ImagickException si une erreur survient.

add a note add a note

User Contributed Notes 6 notes

pilot114 at bk dot ru
2 years ago
Simlest way converting from CMYK to RGB:

if ($jpeg->getImageColorspace() == \Imagick::COLORSPACE_CMYK) {

It is pretty work in current stable Image Magick (6.9.0-4).
eth at ethaniel dot com
5 years ago
When converting from CMYK to RGB using this function, the image can become inverted. To fix this, use a workaround (don't forget to download the .icc files online):

// 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);
// then we add an RGB profile
$icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc');
$img->profileImage('icc', $icc_rgb);

$img->stripImage (); // this will drop down the size of the image dramatically (removes all profiles)
jdstraughan dot com at gmail dot com
8 years ago
FYI, here is the breakdown for (int $colorspace):

0 - UndefinedColorspace   
1 - RGBColorspace   
2 - GRAYColorspace   
3 - TransparentColorspace   
4 - OHTAColorspace   
5 - LABColorspace   
6 - XYZColorspace   
7 - YCbCrColorspace   
8 - YCCColorspace   
9 - YIQColorspace   
10 - YPbPrColorspace   
11 - YUVColorspace   
12 - CMYKColorspace   
13 - sRGBColorspace   
14 - HSBColorspace   
15 - HSLColorspace   
16 - HWBColorspace
3 years ago
I have a better solution for solving inverted colors on php 5.3.x than posted.  All the other solutions I found darkens the image or messes with the colors.

See below (note: my imagick object is $jpeg)

$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
                // then we add an RGB profile
                $icc_rgb = file_get_contents(FRAMEWORK_PATH . DS . 'color' . DS . 'AdobeRGB1998.icc');
                $jpeg->profileImage('icc', $icc_rgb);
                //set color space to rgb
                //fix gamma, hue, saturation, brightness
                if($php_vs < 5.3) {
                    //ADJUST GAMMA BY 20% 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);
            //end convert to RGB=========================|
charlie at midsouthhost dot com
7 years ago
If your getting strange/bad color rendering from a PDF, after trying the colorspace constants noted by jdstraughan, try other values outside that range.

In one case for me only $image->setImageColorSpace(22) provided useful color. I have found posts elsewhere using values up to 255.
mettedraq at gmail dot com
6 years ago
This is how to Monochrome a jpg [on Windows].. since I couldn't find it anywhere else.

("Content-type: image/jpeg");

$IMagick = new IMagick('c:\\testing\\fruit.jpg');

To Top