imagecopy() with PNG alpha transparency?

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

imagecopy() with PNG alpha transparency?

Post by Josh1billion »

I am trying to generate an image out of two images. I have a background image, and then an object image. The object image is drawn onto the background image. The object image's background should be transparent so that the "background image" can show through after the drawing.

Is there a way to do this with PNG's built-in transparency, rather than using the set transparency function?
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Re: imagecopy() with PNG alpha transparency?

Post by Josh1billion »

Nevermind, got it! A good hour or two of frustration is finally over!

Solution:

I changed my imagecopymerge() function to imagecopy() instead (and removed the last argument which isn't used in imagecopy()).
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: imagecopy() with PNG alpha transparency?

Post by onion2k »

It can be done with imagecopy() too.. you just need to make sure the imagesavealpha() and imagealphablending() values are correct for both images.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Re: imagecopy() with PNG alpha transparency?

Post by Josh1billion »

What I meant was that I was originally trying to use imagecopymerge(), and the solution was to change that to imagecopy() instead.
sneha1234
Forum Newbie
Posts: 5
Joined: Mon Sep 29, 2008 2:36 am

Re: imagecopy() with PNG alpha transparency?

Post by sneha1234 »

hi!
i m facing the same Problem i want transparent image of some width n height and text on that image...but when i m using imageSaveAlpha($image, true);
ImageAlphaBlending($image, false); its not working...

<?php

Header('Content-type: image/png');

//$size = 300;
//$Img=imagecreatetruecolor(100,30);

$Img = imagecreate(110,30);

$Gray = imagecolorallocatealpha($Img, 0, 0, 255, 10);

$text = 'Testing..';

imageFilledRectangle($Img,0,0,100,100,$Gray);
imagecolortransparent($Img,$Gray);

$font = 'Shelley_AllegroScript.ttf';
imagettftext($Img, 20, 0, 10, 20, $Gray, $font, $text);
//imagestring($Img,4,0,0,$text,$Gold);
//imagecolortransparent($Img,$Gray);

imagepng($Img);

ImageDestroy($Img);
?>
then i m getting diminished text....
Post Reply