PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a big problem making a php script that saves the transparency of a gif image (alpha channel). The documentation for this and code examples are some few that the script I tried modifying is very simple: it does save transparency but loses the colors in process.
function trans($path){
$colorTrans=0;
$dimX=0;
$dimY=0;
$im="";
$im1="";
$imtrans="";
//get the image from disk
$im=imagecreatefromgif($path);
//dimension of the file dimX= width dimY= height
$dimX=imagesx($im);
$dimY=imagesy($im);
//create new image which saves the transparency
$imtr=imagecreatetruecolor($dimX,$dimY);
//using image copy for testing reasons
imagecopy($im,$imtr,0,0,0,0,$dimX,$dimY);
//getting the 'transparent' color
$colorTrans=imagecolortransparent($im);
//filling the background with the transparent color and saving it
imagefill($imtr,0,0,$colorTrans);
imagecolortransparent($imtr,$colorTrans);
return imagegif($imtr);
imagedestroy($imtr);
}
I know it' s easier to save transparency for png and that the big mistake is filling with transparency the image but I didn' t found any other way. Can you help me?
Using imagecreatetruecolor() to create a gif is likely to do some pretty weird things. When PHP comes to output the GIF image it'll have to convert it to a palette based image, something PHP does very badly indeed. If you're making a gif you should stick with an 8bit palette based image throughout the process, so imagecreate() is a more logical choice.
If I use imagecreate() I get an error which states that the gif image couldn' t be created. I assume is given from imagecolortransparent (from what I understood it works only with true color images).
Alternatively, you could try PNG instead. The 8-bit version is a gif workalike and is compatible with all browsers. It's big brother supports a full alpha channel (as opposed to bitmask transparency like GIFs) but browser support is less ideal. A javascript/css solution exists for IE.
Same as before the code gives the same error when I put that function. And png isn' t needed because it gives major problems with another code (which I didn' t wrote and can' t modify).
From what I read palette images save the alpha channel on 1 bit, true color images save on 24 bit the colors and the alpha channel. Somehow the code I made overwrites the rest of the colors.
Solved it by using instead imagecopyresized() after using the functions you suggested. It' s weird that this only function works and all the other copying, merging that state they save transparency don' t work.
Now I have a bigger problem integrating the function (script) I made into the existing code.