Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmyk to rgb using php

Tags:

php

image

rgb

cmyk

I'm using the following script to save images on a folder, but there is one image that shows this message "the image cannot be displayed because it contains errors".

I think the problem is something called cmyk and rgb on the image, but when searching on the web for a way so convert cmyk to rgb using php I can't find an example of how to do this.

Here is one of example: http://offshootinc.com/blog/2008/10/24/using-the-imagick-class-to-convert-a-cmyk-jpeg-to-rgb/ but I don't undestand how to use that in my case.

The link for the image is something like: www.example.com/attachment?id=2290

The script is as follow:

<?php 
$image = 'http://www.dealrush.ie/attachment?id=2290';
$name = 'somename';
$alt = 'somealt';
$saveimage = file_get_contents($image);
file_put_contents("/usr/local/pem/vhosts/155030/webspace/httpdocs/img/$name.jpg", $saveimage);?>

Later in some pages I will use something like this to show the image. <img src="http://www.example.com/img/<?php echo $name?>.jpg" alt="<?php echo $alt?>" height="127px" width="190px"/>

Any help with converting these images will be appreciated Thanks Daniel

like image 383
user983248 Avatar asked Dec 30 '11 01:12

user983248


2 Answers

I doubt that the colorspace (CMYK or RGB) is your problem. Although everyone should be using RGB images on the Net, the browsers will still display a CMYK image without complaint.

To convert the image from CMYK to RGB, you need to have an imagine manipulation program installed, such as ImageMagick, GraphicsMagick, or ExactImage. Any of these can do what you want, but have to be installed by the server admin. If you're luckly, ImageMagick might already be installed, in which case you could do this:

$image= '/path/to/your/file.jpg';
$i = new Imagick($image);
$i->setImageColorspace(Imagick::COLORSPACE_SRGB);
$i->writeImage($image);
$i->destroy();

Note that ImageMagick is the most powerful, ExactImage is the fastest, and GraphicsMagick is a folk of ImageMagick, which is faster but a little less powerful and has some bugs. Only ImageMagick can be used from PHP, the others have to be executed with the exec function, although that is not necessarily a bad thing as they probably handle their own memory and cleanup much better than PHP would.

like image 72
Alasdair Avatar answered Nov 13 '22 08:11

Alasdair


CMYK is a method of printing images using subtractive light mixing rather than additive. As you probably know, RGB colors are composed of red, green, and blue, and the pixels in your computer monitor or TV emit those colors. In printing, generally white paper is used. It reflects (close to) 100% of red, green, and blue light. When you put ink on the paper, the ink absorbs one or more of those colors. So red ink is actually absorbing the green and blue light that would normally be reflected off the paper. In printing they use the complements of red, green, and blue. Those are cyan, magenta, and yellow. CMY = Cyan, Magenta, Yellow. The K stands for black. (They didn't use B because the blue in RGB is already B.) It turns out that it's hard to get a nice deep black using cyan, magenta, and yellow inks (plus expensive - you have to use all 3!), so they add it a little pure black (which reflects close to no light) to make it look better.

So what this means for you is that you have to convert from CMYK to RGB. Unfortunately, there's not a single way to do that. If you look at something like Photoshop, it requires you to first set the ink and paper types you want to emulate before converting.

If you had pure CMY with no black component, you could use r = 1.0 - c, g = 1.0 - m, b = 1.0 - y and get a reasonably good approximation of the image. The first response in this question is a reasonable response.

You can also figure out a particular conversion you want to use in Photoshop or some other app and make a table from that. I've done this before by creating an image that is, say, 1024x1024. Each 32x32 square has a gradient with cyan in the x direction going from 0 to 1 and magenta in the y direction going from 0 to 1. Then from left to right, each square has increasing amounts of yellow, and going from top to bottom, they have increasing amounts of black in them. I load the image into Photoshop, convert it to RGB and save it again in a lossless format (like TIFF) without tiles, strips or layers (for ease of use). Then I load it into my app and use it as a look-up table to do the same conversion to RGB. It's a bit cumbersome the first time, but once you have the conversion table made, it's pretty easy to use. I just use a bilinear interpolation to generate the RGB value.

like image 23
user1118321 Avatar answered Nov 13 '22 08:11

user1118321