user669677
user669677

Reputation:

How can I check if a given image is CMYK in php?

How can I check if a given image is CMYK in php?

Do you know any solution without Imagick?

thanks.

Upvotes: 4

Views: 2894

Answers (2)

Emanuele
Emanuele

Reputation: 617

If the image is in jpg format, you can check SOF (Start Of Frame - SOF0 or SOF2) section in jpeg header (see here).

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))

Upvotes: 0

Mikko
Mikko

Reputation: 602

You may use the getimagesize($filename) function in PHP to achieve this.

<?php
    $blah = getimagesize($filename);
    if($blah['channels']==4){
        // it is cmyk
    }
?>

Upvotes: 12

Related Questions