如何使用GD检查GIF是否具有透明度? [英] How to check if a GIF has transparency using GD?

查看:83
本文介绍了如何使用GD检查GIF是否具有透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了此线程,该解决方案非常有效,但仅适用于PNG.是否有解决方案来检查GIF图像在PHP-GD中是否具有透明度?

I saw this thread and the solutions perfectly works but for PNG only. Is there a solution for checking if a GIF image has transparency in PHP-GD?

推荐答案

与其他格式相比,我对 GIF 不太熟悉,因此我的假设可能不正确.如果我错了,请告诉我-简单的评论,而不是不赞成投票的方式,将不胜感激.

I am less familiar with GIFs than other formats, so my assumptions may be incorrect. Please let me know if I am wrong - a simple comment, rather than a down-vote, would be appreciated.

我假设:

  • 所有 GIF 都灰化了,
  • 对于任何透明的调色板条目,alpha分量将为非零值(可能为127)
  • 编码器不会不必要地添加透明调色板条目.

在此基础上,以下代码将加载 GIF 并检查是否没有调色板项包含透明度-而不是检查图像高度和宽度上非常慢的双循环中的每个像素:

On that basis, the following code will load a GIF and check that no palette entry contains transparency - rather than checking every single pixel in a very slow double loop over height and width of an image:

<?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";
   }
?>

这篇关于如何使用GD检查GIF是否具有透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆