Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用GD检查GIF是否具有透明度?_Php_Image Processing_Gd_Php Gd - Fatal编程技术网

Php 如何使用GD检查GIF是否具有透明度?

Php 如何使用GD检查GIF是否具有透明度?,php,image-processing,gd,php-gd,Php,Image Processing,Gd,Php Gd,我看到了,解决方案非常有效,但仅适用于PNG。在PHP-GD中是否有检查GIF图像是否具有透明度的解决方案?此代码为GIF创建预览并检查透明度 $width=64; $height=64; $src='original.gif'; $dst='preview.gif'; list($width_orig, $height_orig) = getimagesize($src); $image_p = imagecreatetruecolor($width, $height); $image =

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

此代码为GIF创建预览并检查透明度

$width=64;
$height=64;
$src='original.gif';
$dst='preview.gif';
list($width_orig, $height_orig) = getimagesize($src);

$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromgif($src);

$transparent_index = imagecolortransparent($image);
$palette_colors_cnt = imagecolorstotal($image);
if ($transparent_index >= 0) {
    imagepalettecopy($image, $image_p);
    imagefill($image_p, 0, 0, $transparent_index);
    imagecolortransparent($image_p, $transparent_index);
    imagetruecolortopalette($image_p, true, $palette_colors_cnt);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagegif($image_p, $dst);

此代码为GIF创建预览并检查透明度

$width=64;
$height=64;
$src='original.gif';
$dst='preview.gif';
list($width_orig, $height_orig) = getimagesize($src);

$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromgif($src);

$transparent_index = imagecolortransparent($image);
$palette_colors_cnt = imagecolorstotal($image);
if ($transparent_index >= 0) {
    imagepalettecopy($image, $image_p);
    imagefill($image_p, 0, 0, $transparent_index);
    imagecolortransparent($image_p, $transparent_index);
    imagetruecolortopalette($image_p, true, $palette_colors_cnt);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagegif($image_p, $dst);

与其他格式相比,我对
GIF
s不太熟悉,因此我的假设可能不正确。请让我知道,如果我错了-一个简单的评论,而不是反对票,将不胜感激

我假设:

  • 所有的
    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";
   }
?>

与其他格式相比,我对
GIF
s不太熟悉,因此我的假设可能不正确。请让我知道,如果我错了-一个简单的评论,而不是反对票,将不胜感激

我假设:

  • 所有的
    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";
   }
?>


请参阅手册-其中有很多有用的信息。请参阅手册-其中有很多有用的信息。这对于8位PNG图像来说是个不错的主意,但GIF不支持alpha通道透明度。单个调色板条目被定义为透明颜色,然后使用该条目的任何像素都将被渲染为透明。@timclutton感谢您的输入。我不明白你在说什么-我创建了一些透明的GIF(使用ImageMagick),每次我发现一个调色板条目在alpha通道中的值为127。我不是在争论或暗示你错了,正如我说的,我对GIF不太熟悉,想了解自己,那么你能给我指一个任意像素都透明但调色板中没有透明像素的GIF吗?或者用另一种方式解释,好让我掌握窍门吗?你提到的“阿尔法频道”引起了我的注意。GIF不支持alpha通道,所以我不明白如何检查它。不过,我还没有测试你的代码,GD经常让我感到困惑!如果有机会,我会自己运行代码。很高兴被证明是错的:-)@timclutton哦,我明白了。我只需检查多达256个调色板条目,调色板中的每个条目都有一个红色、绿色、蓝色和一个Alpha部分的空格。我认为channel是一个糟糕的名字,因为它只是调色板条目的一个组件。我将更新我的帖子。这里使用“alpha”让我感到困惑;但是运行了代码后,我可以看出您是非常正确的。显然,GD比我想象的更聪明,智能地将
alpha
索引中的透明度设置返回为
0
127
+这是一个非常好的主意,适合8位PNG图像,但GIF不支持alpha通道透明度。单个调色板条目被定义为透明颜色,然后使用该条目的任何像素都将被渲染为透明。@timclutton感谢您的输入。我不明白你在说什么-我创建了一些透明的GIF(使用ImageMagick),每次我发现一个调色板条目在alpha通道中的值为127。我不是在争论或暗示你错了,正如我说的,我对GIF不太熟悉,想了解自己,那么你能给我指一个任意像素都透明但调色板中没有透明像素的GIF吗?或者用另一种方式解释,好让我掌握窍门吗?你提到的“阿尔法频道”引起了我的注意。GIF不支持alpha通道,所以我不明白如何检查它。不过,我还没有测试你的代码,GD经常让我感到困惑!如果有机会,我会自己运行代码。很高兴被证明是错的:-)@timclutton哦,我明白了。我只需检查多达256个调色板条目,调色板中的每个条目都有一个红色、绿色、蓝色和一个Alpha部分的空格。我认为channel是一个糟糕的名字,因为它只是调色板条目的一个组件。我将更新我的帖子。这里使用“alpha”让我感到困惑;但是运行了代码后,我可以看出您是非常正确的。显然,GD比我想象的更聪明,智能地将
alpha
索引中的透明度设置返回为
0
127
+我给了你一个及时的提醒,提醒你要经常测试你的假设:-)