Page 1 of 1

Mixing color with imagecopymerge

Posted: Tue Dec 04, 2007 3:51 am
by Rovas
I have to a couple gif that I merge after I colored the first one (or the second one). The colors of the third and fourth gif image change ("blend") with the colors of the first or second gif. I tried using imagecopy the same result.
The script is quite long so I cut some parts from it for a better reading

Code: Select all

//creating the image resources the simple imagegif($path);

/*this condition checks that the resource is transparent and has a color. Applies the selected color to the image using a function defined lower and the merges with the first one */
if($transparent2==1 && $rgb2!=""){$im2=ImageColorWithTransparency($im2, $rgb2); imagecopymerge($im1,$im2,  $x2, $y2, 0,0, imagesx($im2), imagesy($im2), 100);}
if($transparent3==1 && $rgb3!=""){$im3=ImageColorWithTransparency($im3, $rgb3); imagecopymerge($im1,$im3,  $x3, $y3, 0,0, imagesx($im3), imagesy($im3), 100);}
//and so on ...

//for saving the transparent gifs the function trans must used
$transparentImage=trans($im1);
imagegif(transparentImage);

//this function colores a transparent gif and saves it' s transparency
	function ImageColorWithTransparency($im, $rgb){
		//extracting values for each RGB channel
		$red=hexdec(substr($rgb,0,2));
		$green=hexdec(substr($rgb,2,2));
		$blue=hexdec(substr($rgb,4,2));
		$t = imagecolorstotal($im);
		
		//obtaining the new colors using the formula
		for($i=0;$i<=$t-1;$i++){
			$col=ImageColorsForIndex($im,$i); 
			$red_set=$red/255*$col['red'];
			$green_set=$green/255*$col['green'];
			$blue_set=$blue/255*$col['blue'];
			if($red_set>255)$red_set=255;
			if($green_set>255)$green_set=255;
			if($blue_set>255)$blue_set=255;
			if($red_set<0)$red_set=0;
			if($green_set<0)$green_set=0;
			if($blue_set<0)$blue_set=0;
			
			//coloring the image with the new color   
			imagecolorset($im,$i,$red_set,$green_set,$blue_set); 
		}
		return $im;
	}

        //this function saves the transparency of gif
	function trans($im){

		$culoareTrans=0;
		$dimX=0;
		$dimY=0;
		$imtrans="";
		
		$dimX=imagesx($im);
		$dimY=imagesy($im);
		//creating true color image to save transparent color
		$imtr=imagecreatetruecolor($dimX,$dimY);
		//copying the color from palette image
		imagepalettecopy($im, $imtr);
		//saving the transparent color
		$culoareTrans=imagecolortransparent($im);
		//filing and setting the transparent color to the new image
		imagefill($imtr,0,0,$culoareTrans);
		imagecolortransparent($imtr,$culoareTrans);
		//copying the image 
		imagecopyresized($imtr, $im,0,0,0,0, $dimX, $dimY,$dimX,$dimY);
		//transforming into palette
		imagetruecolortopalette($imtr,true,256);
		//imagecolortransparent($imtr,$culoareTrans);
		return $imtr;
	}
Any ideas besides replacing the functions that deal with copying (imagetruecolortopalette, imagepalettcopy) or imagecopy which don' t work.

Re: Mixing color with imagecopymerge

Posted: Wed Jan 16, 2008 3:48 am
by Rovas
I found out there is a problem if the images are gifs, if there is a jpg image the leakage will stop.