How to check if an image has transparency using GD?

后端 未结 8 1390
耶瑟儿~
耶瑟儿~ 2020-12-09 18:48

How do I check if an image has transparent pixels with php\'s GD library?

8条回答
  •  借酒劲吻你
    2020-12-09 19:03

    This is how I detect 8-32 bit transparency. It only work with PNG's.

    function detect_transparency($file){
    
        if(!@getimagesize($file)) return false;
    
        if(ord(file_get_contents($file, false, null, 25, 1)) & 4) return true;
    
        $content = file_get_contents($file);
        if(stripos($content,'PLTE') !== false && stripos($content, 'tRNS') !== false) return true;
    
        return false;
    }
    

提交回复
热议问题