Page 1 of 1

Saving other colors besides transparency from a gif

Posted: Mon Oct 22, 2007 8:04 am
by Rovas
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.

Code: Select all

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?

Posted: Mon Oct 22, 2007 8:56 am
by onion2k
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.

Posted: Mon Oct 22, 2007 1:24 pm
by Rovas
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).

Posted: Mon Oct 22, 2007 11:44 pm
by Kieran Huggins
Try http://ca3.php.net/manual/en/function.i ... alette.php before saving the GIF.

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.

Posted: Tue Oct 23, 2007 3:45 am
by Rovas
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.