PNG Transparency Problems

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
tezmc
Forum Newbie
Posts: 2
Joined: Tue Sep 08, 2009 7:32 am

PNG Transparency Problems

Post by tezmc »

Hi,

I'm trying to write a script which will rotate an image, and overlay it on another image. It is working, except there are artifacts in the background, where the image has been resized.

An example is here:

http://tsunet.co.uk/php/gd/water.php

My code is:

Code: Select all

 
<?
 
$name               = "Liz Barton";
$title              = "V Festival Aug 09";
$line1              = ""I discovered Sol,";
$line2              = "I've never drunk";
$line3              = "beer before but";
$line4              = "I love this"";
$file               = 'blank3.png';
$dim                = getimagesize($file);
$imagewidth         = $dim[0];
$imageheight            = $dim[1];
$image              = imagecreatefrompng($file);
$black              = imagecolorallocate($image, 0, 0, 0);
$grey               = imagecolorallocate($image, 128, 128, 128);
$font               = 'ArdleysHand.ttf';
 
$filename = 'front1.png';
$degrees = 7.5;
$source = imagecreatefrompng($filename);
$rotate = imagerotate($source, $degrees, -1);
 
imagesavealpha($rotate, true);
imagealphablending($rotate, true);
 
imagesavealpha($image, true);
imagealphablending($image, true);
 
imagettftext($image, 12, -10, 160, 310, $black, $font, $name);
imagettftext($image, 12, -10, 155, 330, $black, $font, $title);
imagettftext($image, 12, -10, 150, 360, $black, $font, $line1);
imagettftext($image, 12, -10, 145, 380, $black, $font, $line2);
imagettftext($image, 12, -10, 140, 400, $black, $font, $line3);
imagettftext($image, 12, -10, 140, 420, $black, $font, $line4);
imagecopy($image, $rotate, $dest_x, $dest_y, -11, -28, 350, 275);
 
 
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
 
Any ideas on how I can fix this? I've been trying different things for a day now. Any help would be much appreciated.

Thanks

Terry
tezmc
Forum Newbie
Posts: 2
Joined: Tue Sep 08, 2009 7:32 am

Re: PNG Transparency Problems

Post by tezmc »

I've worked out that the black bars are coming from imagecopy(). They are filling the area left behind when I offset the position of my rotated image.

As far as I can see, imagecolortransparent() won't work with imagecopy(), but I'm struggling to get a transparent area behind the postcard with imagecopymerge().
Post Reply