Skip to content

Instantly share code, notes, and snippets.

@Neoglyph
Last active November 17, 2023 21:57
Show Gist options
  • Save Neoglyph/645d25d2b784a8fb77fd77e3bdfce006 to your computer and use it in GitHub Desktop.
Save Neoglyph/645d25d2b784a8fb77fd77e3bdfce006 to your computer and use it in GitHub Desktop.
Convert RGB to CMYK in PHP with Imagick
<?php
$iccProfile = '../path_to_icc/Profile.icc';
$image = new Imagick();
$image->clear();
$image->readImage($imagePath);
if ($image->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
return;
}
$iccCmyk = file_get_contents($iccProfile);
$image->profileImage('icc', $iccCmyk);
unset($iccCmyk);
$image->transformImageColorspace(Imagick::COLORSPACE_CMYK);
$image->writeImage($newImagePath);
@Neoglyph
Copy link
Author

Neoglyph commented Feb 6, 2020

Hey,

really glad it helped you!

As for the suggestions / questions:

  • what is the point of lines 9 and 11, and of the var $iccCmyk ?

You're right, I misstyped, the '../path_to_icc/Profile.icc' should be the $iccCmyk variable instead (will fix it in the code).

  • the profile name argument (first of method $image->profileImage($name, $pathToProfile) must be 'cmyk', not 'icc'. An error occurs if so...

Interesting, my code contains the icc profile parameter and does not throw an error.
Might be due to different Imagick versions, or maybe a difference in files, not too sure. For reference my Imagick is currently at:

"versionNumber" => 1673
"versionString" => "ImageMagick 6.8.9-9 Q16 x86_64 2019-11-12 http://www.imagemagick.org"
  • If this transformation concerns web images, the Profile.icc must be >this file< - provided by Adobe for Web images

I actually pass different ICC Profile files based on what i'm converting the CMYK for, currently one is similar to this one: CoatedFOGRA39.icc, although I must admit I am not that knowledgeable about color profiles in general.
So you're saying, if you're receiving web images and converting them for print purposes the USWebUncoated profile is generally the recommended one? If so I might try replacing the current one i'm using with that one in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment