Blog

Using PHP’s Imagick class to convert a CMYK jpeg to RGB

We’ve recently converted all of our image processing from using GD to using ImageMagick and one of the biggest stumbling blocks in doing that has been the lack of documentation for PHP’s Imagick class. I ran into a problem when I realized that I needed to convert a jpeg from CMYK to RGB. After some googling, I was only able to find a solution that used imagemagick on the command line. This just isn’t a viable solution for my current needs, I need a solution in PHP, preferably using the Imagick class. Back to google. I finally find what appears to be exactly what I’m looking for, but it’s on one of those question/answer sites where you have to pay to see the answer. Forget that! So I read through the little Imagick documentation that exists and trial-and-errored my way to a working solution.

Here’s a simple script that gives a run through of converting a CMYK jpeg to RGB using ImageMagick’s Imagic class:

<?php
 
$filePath = '/path/to/your/file.jpg';
$i = new Imagick($filePath);
 
$cs = $i->getImageColorspace();
 
if ($cs == Imagick::COLORSPACE_CMYK) {
 
    print "Image is CMYK<br/>\n";
 
    ?>
    CMYK Image:<br/>;
    <img src="<?php echo $filePath ?>"/>
    <br/><br/>
    <?php
 
    $i->setImageColorspace(Imagick::COLORSPACE_SRGB);
    $i->setImageFormat('jpeg');
 
    $cs = $i->getImageColorspace();
 
    if ($cs != Imagick::COLORSPACE_CMYK) {
 
        print "Image is no longer CMYK<br/>\n";
 
        // write it to a temp file
        $filePath = '/path/to/temp/file.jpg';
        $i->writeImage($filePath);
 
    }
 
} else {
    print "Image is not CMYK<br/>\n";
}
 
if ($cs == Imagick::COLORSPACE_SRGB || 
    $cs == Imagick::COLORSPACE_RGB){
    print "Image is RGB<br/>\n";
}
 
?>
RGB Image:<br/>
<img src="<?php echo $filePath ?>"/>;
<?php
 
$i->clear();
$i->destroy();
$i = null;

Hopefully this will help some of you out! I know there are a lot of people out there looking for the same (or at least a similar) solution.

10 Responses to Using PHP’s Imagick class to convert a CMYK jpeg to RGB

  1. Shaun says:

    Thanks,

    I’ve been looking for this solution for months.

    Great stuff

  2. Neela says:

    Could u please tell where to get the ‘Imagick’ class from ?

  3. batfastad says:

    It’s actually a PECL module available from http://pecl.php.net/package/imagick
    You get access to the class and its functions when you install that module.
    You also need Imagick installed on the server as well… otherwise the imagick module won’t do anything

  4. Elnur Abdurrakhimov says:

    batfastad, I should correct you: you also need Imagemagick installed on the server.

  5. Elnur Abdurrakhimov says:

    Chris Woodford, thank you very much for the information. It helped me to solve the problem of CMYK not showing in IE.

  6. Tom says:

    Awesome snippet. Cheers!

  7. KrisBerg says:

    Hey Chris,

    Thanks for trying to figure this out mate. I tried it out, but it seems that it just “invert” the CMYK-image. I checked to see if that was actually happend visually, and it seems like it did by invert it again in GIMP. It also seems like it adjusted the levels/brightness of it…

    This is a sample CMYK image I used:
    http://www.manheimcanadalogos.com/images/jpeg/Manheim_Canada_logo_clr-4c.jpg

    You can find the correct RGB image here as well:
    http://www.manheimcanadalogos.com/images/jpeg/Manheim_Canada_logo_clr.jpg

    So as you said, I’m back to Google :/

    PS: This failure of correct converting it MIGHT be something to do with my versions of come lib or installation of whatever component. Not making the version list too long, all I did was a clean install of Ubuntu 10.10 + lamp + php5 and GD libs etc.

    Cheers,
    Kris

  8. KrisBerg says:

    I just figured out that the image i tried to convert had some sort of a color profile, and since I don’t know much about that subject I can only give you guys the solution that seems to work for me. I downloaded the profiles here:
    http://www.color.org/srgbprofiles.xalter

    and read the last post here:
    http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=13578

    and tried this in my terminal for a test conversion:
    “convert CMYK.jpg -profile sRGB_v4_ICC_preference.icc -colorspace RGB test.jpg”

    It seems to be quite similar to the RGB test file I download in color comparison. Hope it helps for those who experienced the same challenge I stumbled upon :)

  9. Michael says:

    Great Info. Saved me some development time :)

  10. PrashantP says:

    Thanks Woodford, I also done tons of research on this issue and then came to know from your provided code. It worked superb as Imagick and other related packages was already installed on my server.

    Thanks a lot once again!

*
To prove that you're not a bot, enter this code
Anti-Spam Image