duanhongqiong9460
2011-10-24 09:28
浏览 276
已采纳

如何在PHP中识别CMYK图像

Can anyone tell me how to identify an image if it is in CMYK or RGB using PHP ?

图片转代码服务由CSDN问答提供 功能建议

任何人都可以告诉我如何使用PHP在CMYK或RGB中识别图像吗? \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • douhu3424 2011-10-24 09:40
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhui0975 2012-03-01 05:24
        $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";    
            }
    
    评论
    解决 无用
    打赏 举报
  • dptiq46022 2013-08-22 16:07

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

    评论
    解决 无用
    打赏 举报
  • douzi9744 2015-01-23 11:35

    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.

    评论
    解决 无用
    打赏 举报
  • duan3019 2017-09-02 07:25

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题