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:

 
<?

$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

    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().

      It's the call to imagecopy with negative values for src_x and src_y. You copy from outside the image which yields the colour you see top and left in the resulting image.

      Try something like

      imagecopy($image, $rotate, $dest_x, $dest_y, 0, 0, imagesx($rotate), imagesy($rotate));

        Thanks alot for the reply, using that takes away the colour, but the image is overlaid on the top left corner of the background. How can I now move it into position without the borders coming back?

        You can see it here:
        http://tsunet.co.uk/php/gd/water.php

          In the code you provided $dest_x and $dest_y were never set. See if they actually contain any values in your code. And you ought to increase those values by 11 and 28 respectively.

            Thanks alot, I've got it working now. I'd been going round in circles for the past couple of days so my code had gotten a little bit messy. All working now though.

              Write a Reply...