我找到了问题如何使用GD检查图像是否具有透明度? https://stackoverflow.com/q/5495275但答案都是针对 PNG 文件的。是否有解决方案可以使用 GD 扩展在 PHP 中检查 GIF 图像是否具有透明度?
我假设:
- all
GIF
s 已调色,
- 对于任何透明的调色板条目,alpha 分量将不为零(可能是 127),
- 编码器不会添加不必要的透明调色板条目。
在此基础上,以下代码将加载GIF
并检查调色板条目是否包含透明度 - 而不是在图像的高度和宽度上非常缓慢的双循环中检查每个像素:
<?php
function GIFcontainstransparency($fname){
// Load up the image
$src=imagecreatefromgif($fname);
// Check image is palettised
if(imageistruecolor($src)){
fwrite(STDERR,"ERROR: Unexpectedly got a truecolour (non-palettised) GIF!");
}
// Get number of colours - i.e. number of entries in palette
$ncolours=imagecolorstotal($src);
// Check palette for any transparent colours rather than all pixels - to speed it up
for($index=0;$index<$ncolours;$index++){
$rgba = imagecolorsforindex($src,$index);
if($rgba['alpha']>0){
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////////////////////////
if(GIFcontainstransparency("image.gif")){
echo "Contains transparency";
} else {
echo "Is fully opaque";
}
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)