CMYK to RGB

9 posts by 3 authors in: Forums > CMS Builder
Last Post: October 4, 2017   (RSS)

Hey Guys,

Is there a way that we can automatically convert CMYK images to RGB, as we do thumbnails etc?

Cheers,

Tim (toledoh.com.au)

By Dave - September 5, 2017

Hey Tim, 

I'm sure it's possible.  Where are you running into that? Can you give a bit more background?  

Dave Edis - Senior Developer
interactivetools.com

I got a number of shopping centre websites, and each of these have a "store directory".  Each store needs to supply a logo to the site admin who then uploads it, however the stores are often pretty low tech - take-away food, discount shops, ma and pa outfits etc who have had their logos created for signage and the designers supply them in print quality... CMYK.  They don't know how to change to RGB.  So some times it takes weeks of emails back and forth.

If the upload function converted to RGB rather than throwing an error - that would be great.

Cheers,

Tim (toledoh.com.au)

By Dave - September 6, 2017

Ok, thanks Tim.  I'll put that on the todo list!

Dave Edis - Senior Developer
interactivetools.com

Hi, Dave.

I've often experienced the same situation as Tim with clients or their staff uploading CMYK images and would like to add my "vote" to finding a solution, if one can be found.

~ Deborah

By Dave - October 3, 2017 - edited: October 4, 2017

Hi Tim and Deborah, 

Can you try this patch when you have a moment? 

With the latest version (v3.10): 

In /lib/upload_functions.php, search for "valid content" and add the code in red: 

if ($errors) { return $errors; }

// Convert CMYK to RGB if needed
image_convertCMYKToRGBIfNeeded($mainUploadWorkingPath);

// check for valid content in image and swf/swc files

In /lib/image_functions.php, add this function to the bottom of the file.

// Check if image file is CMYK and overwrite it with an RGB image if it is
// NOTE: We do this because some browsers don't support CMYK images.
function image_convertCMYKToRGBIfNeeded($filePath) { 
  
  // Check if file is a CMYK image
  $isCMYK = true;
  $imageData = getimagesize($filePath);
  if (!array_key_exists('channels', $imageData) || $imageData['channels'] != 4)    { $isCMYK = false; } // not CMYK
  if (!array_key_exists('mime', $imageData) || $imageData['mime'] != 'image/jpeg') { $isCMYK = false; } // not a jpeg (gif and png don't support CMYK)
  if (!$isCMYK) { return; } // leave original image unchanged

  // Convert CMYK image to RGB
  if (extension_loaded('imagick')) {
    $image = new Imagick($filePath);
    $image->transformImageColorspace(Imagick::COLORSPACE_RGB);
    $success = $image->writeImage($filePath);
    if (!$success) { dieAsCaller(__FUNCTION__. ": Error rewriting CMYK image as RGB with Imagick:  $php_errormsg"); }
  }
  else {
    $imageRes = imagecreatefromjpeg($filePath);
    $success = imagejpeg($imageRes, $filePath);
    if (!$success) { dieAsCaller(__FUNCTION__. ": Error rewriting CMYK image as RGB with GD:  $php_errormsg"); }
    imagedestroy($imageRes);
  }
}

Now, when you upload a CMYK image it should automatically convert it.  One thing to check for is if it changes the colors at all.  I've seen some reports online that that can happen, but I think that was with older PHP versions.  

Let me know if this works for you and we'll include it in the next version.

Dave Edis - Senior Developer
interactivetools.com

By Dave - October 4, 2017

Hi Deborah, 

Great, thanks for your feedback and testing!

>p.s. There's some duplicated lines in your original example code.

I've updated this, thanks.

>2) CMYK saved as PNG

Currently, the above code only works with jpegs.  I wasn't able to find any indication of PNG's being able to store or used to store 4 channel colors such as CMYK.  Apparently, they're all RGB as it's intended to be a digital image format?  Let me know if you know any different.  Commenting out this line would have it process all image types: 

//if (!array_key_exists('mime', $imageData) || $imageData['mime'] != 'image/jpeg') 

If, for some reason, the CMYK to RGB conversion process fails or isn't performed the original CMSB error about CMYK images will still be displayed.  So you should know if a file gets skipped that shouldn't have been skipped.

>I see ImageMagick referred to in the code. I don't think that's installed by default with PHP,
>right? Should I install that and see if the resulting image for #1 above is better?

Yea, it might do a better job.  If you go to: Admin > General > phpinfo (at the bottom)  and then search for "imagick" you can see if it's installed (and/or confirm it's installed once you've done that).

Thanks!

Dave Edis - Senior Developer
interactivetools.com

By Deborah - October 4, 2017

Dave,

You are so right about the PNGs. I retract my earlier statement, "There was seemingly no color shift when uploading/converting the PNG image." So disregard my PNG comments anyone who reads this post. I just realized that Adobe Photoshop appears to export PNG files as CMYK, but instead quietly converts to RGB. PNG format is for screen only, therefore, a PNG would have to be RGB. Yep.

My server does not have the ImageMagick plugin installed. I'm looking into that and will post back with new test results if I decide to install it.

Still glad to know the uploads will process, which is the most important aspect.

Thanks!
Deborah