2

I'm having a slight problem merging multiple transparent pngs on top of each other. I'm trying to create a tool that designs a belt on the screen for the user allowing them to select a strap, buckle, and design crease.

The tool will merge three different images into one to create one preview image. When I run the code below it creates the strap image, adds the buckle correctly but a black box shows up on the right side of the strap image looking about the same size as the buckle image. I can't figure what is the problem.

This is my first working with images using php so i may be missing the obvious. if anyone can help me i would be greatly appreciative. thanks in advance!

header('Content-type: image/png');
$strap = imagecreatefrompng("images/straps/DBR.png");
$w = imagesx($strap);
$h = imagesy($strap);

imagealphablending($strap,true);

$buckle = imagecreatefrompng("images/buckles/" . $buckle . ".png");
imagealphablending($buckle,true);

$crease = imagecreatefrompng("images/skull.png");
imagealphablending($crease,true);


imagecopy($strap,$buckle,200,0,0,0,$w,$h);
imagecopy($strap,$crease,0,0,0,0,$w,$h);

 //imagecopy($photo2,$crease,200,0,0,0,$w,$h);
// fill the image background with white

imagepng($strap); 

imagedestroy($strap);
imagedestroy($buckle);

UPDATE: This is my current source code

$strap = imagecreatefrompng("images/straps/DBR.png");
$w = imagesx($strap);
$h = imagesy($strap);

imagealphablending($strap,true);
imagesavealpha($strap, true);


$buckle = imagecreatefrompng("images/buckles/" . $buckle . ".png");

imagealphablending($buckle,false);
imagesavealpha($buckle, true);

$crease = imagecreatefrompng("images/skull.png");

imagealphablending($crease,false);
imagesavealpha($crease, true);

imagecopy($strap,$buckle,200,0,0,0,$w,$h);
imagecopy($strap,$crease,0,0,0,0,$w,$h);

imagepng($strap); 

imagedestroy($strap);
imagedestroy($buckle);
Brad
  • 159,648
  • 54
  • 349
  • 530
Jesse
  • 239
  • 6
  • 20
  • Possible duplicate of http://stackoverflow.com/questions/1394061/how-to-merge-transparent-png-with-image-using-php. – Jon Jan 03 '11 at 14:57
  • the other question doesn't address the same problem i'm having that i could see. – Jesse Jan 04 '11 at 00:29

2 Answers2

2

Try out this code, and see if it works:

//call path of all images 
    //example: $peinado="/images/path/peinado.png";
    $image_1 = imagecreatefrompng($peinado);
    $image_2 = imagecreatefrompng($cejas);
    $image_3 = imagecreatefrompng($ojos);
    $image_4 = imagecreatefrompng($nariz);
    $image_5 = imagecreatefrompng($boca);

    //the frame of the original image
    $imgFinal = imagecreatefrompng($src);

    //alpha & transparency
    imagealphablending($imgFinal, true);
    imagesavealpha($imgFinal, true);

    //merge all images 
    imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 259, 429);
    imagecopy($imgFinal, $image_2, 0, 0, 0, 0, 259, 429);
    imagecopy($imgFinal, $image_3, 0, 0, 0, 0, 259, 429);
    imagecopy($imgFinal, $image_4, 0, 0, 0, 0, 259, 429);
    imagecopy($imgFinal, $image_5, 0, 0, 0, 0, 259, 429);

    //save the png image
    imagepng($imgFinal, 'avatars/prueba.png');
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
napraga
  • 21
  • 3
0

I think you have to use imagesavealpha(): http://www.php.net/manual/en/function.imagesavealpha.php

Info: "You have to unset alphablending (imagealphablending($im, false)), to use it."

Floern
  • 33,559
  • 24
  • 104
  • 119
  • I have added imagealphablending($buckle,false); imagesavealpha($buckle, false); but still not working as i had hoped. – Jesse Jan 04 '11 at 00:28
  • Actually you have to set imagesavealpha($buckle, true); same to $crease and $strap – Floern Jan 04 '11 at 00:53
  • I have set imagesavealpha($buckle, true); and imagealphablending($buckle,false); to crease also. For strap I have imagealphablending($strap,true);. If I change to false the strap doesnt show at all.I still have this weird black box showing up though. http://www.clarityproductions.com/bd/image2.php?buckle=buckle2. Any ideas? – Jesse Jan 04 '11 at 16:44