Issue
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?
Solution
I am less familiar with GIF
s 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.
I am assuming that:
- all
GIF
s are palettised, - the alpha component will be non-zero (probably 127) for any palette entry which is transparent,
- encoders do not add transparent palette entries unnecessarily.
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";
}
?>
Answered By - Mark Setchell Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.