Search code examples
phpgd

Imagecopy merge Issue


$image_1 = imagecreatefrompng('GreenAppleMerge80.png');
$image_2 = $image_1;
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 40, 0, 0, 0, 100, 100);
imagepng($image_1, 'final.png');

The above code is written to merge two GreenAppleMerge80.png into final.png

Green.png

final.png

However i am not satisfied with final.png and want the right apple to visible fully and 50% of the left apple, where right apple wil be 50% on top of left apple.

Please suggest.


Solution

  • First you need a GD resource that can hold both apples.

    $imgBig = imagecreate(120 , 80);
    

    You get the PNG exactly as shown. You don't need image_2. It is the same resource.

    $image_1 = imagecreatefrompng('GreenAppleMerge80.png');
    imagealphablending($image_1, true);
    imagesavealpha($image_1, true);
    

    Then you copy the right half of the apple into the big picture.

    imagecopy($imgBig, $image_1, 80, 0, 40, 0, 40, 80);
    

    Then you create your 2nd picture and copy it into the "imgBig".

    imagecopy($image_1, $image_1, 40, 0, 0, 0, 80, 80);
    
    imagecopy($imgBig, $image_1, 0, 0, 0, 0, 80, 80);
    
    imagepng($imgBig, 'final.png');
    

    The solution is not particularly nice. But works for me with your apple picture.