如何在php中检查给定图像是否为CMYK?

时间:2011-12-27 16:35:18

标签: php

如何在php中检查给定图像是否为CMYK?

你知道没有Imagick的任何解决方案吗?

感谢。

2 个答案:

答案 0 :(得分:10)

您可以使用PHP中的getimagesize($filename)函数来实现此目的。

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

答案 1 :(得分:0)

如果图像是jpg格式,您可以检查jpeg标题中的SOF(帧起始 - SOF0或SOF2)部分(参见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))