Can anyone tell me how to identify an image if it is in CMYK or RGB using PHP ?
Take a good look at getimagesize.
Example:
<?php
$size = getimagesize($filename);
$fp = fopen($filename, "rb");
if ($size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp);
exit;
} else {
// error
}
?>
It returns an array with 7 elements.
Index 0 and 1 contains respectively the width and the height of the image.
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
mime is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header: channels will be 3 for RGB pictures and 4 for CMYK pictures.
bits is the number of bits for each color.
For some image types, the presence of channels and bits values can be a bit confusing. As an example, GIF always uses 3 channels per pixel, but the number of bits per pixel cannot be calculated for an animated GIF with a global color table.
On failure, FALSE is returned.
$miImagen = array_values(getimagesize('imagenCMYK.jpg'));
list($width, $height, $type, $attr, $bits, $canales) = $miImagen;
if ($canales = 4){
echo "Imagen: CMYK";
}
else{
echo "Tu imagen no es CYMK";
}
If the image is in jpg format, you can check SOF (Start Of Frame - SOF0 or SOF2) section in jpeg header (see http://en.wikipedia.org/wiki/JPEG)
function isCMYK($img_data) {
// Search for SOF (Start Of Frame - SOF0 or SOF2) section in header
// http://en.wikipedia.org/wiki/JPEG
if (($sof = strpos($img_data, "\xFF\xC0")) === false) {
// FF C2 is progressive encoding while FF C0 is standard encoding
$sof = strpos($img_data, "\xFF\xC2");
}
return $sof? ($img_data[($sof + 9)] == "\x04") : false;
}
$img_data
variable is the raw file contents (e.g. $img_data = file_get_contents($filename)
)
Here are two implementations. This version uses GD:
/**
* Check if a JPEG image file uses the CMYK colour space.
* @param string $path The path to the file.
* @return bool
*/
function imageIsCMYK($path) {
$t = getimagesize($path);
if (array_key_exists('mime', $t) and 'image/jpeg' == $t['mime']) {
if (array_key_exists('channels', $t) and 4 == $t['channels']) {
return true;
}
}
return false;
}
This version uses ImageMagick:
/**
* Check if an image file uses the CMYK colour space.
* @param string $path The path to the file.
* @return bool
*/
function imageIsCMYK($path)
{
$im = new Imagick($path);
return ($im->getimagecolorspace() == Imagick::COLORSPACE_CMYK);
}
The GD version is about 18 times faster for me. The imagemagick version will also spot CMYK in other formats, such as TIFF.
None of the answers are accurate enough for me. Use Imagemagick.
To get color space (ie. 'RGB', 'CMYK', etc):
exec('identify -format "%[colorspace] " '.$imagePath);
To get color profile:
exec('identify -format "%[profile:icc] " '.$imagePath);