Jump to content

PNG Overlapping Problem


Kryptix

Recommended Posts

I have 2 PNG images that are the same size and both have transparent backgrounds. The first image is a head which should fit perfectly on the second image (the body).

 

This code works fine on my friends Linux system, but on my system the body just overlaps the head making it white

 

<?php
header('Content-type: image/png');

$canvas = imagecreatetruecolor(64, 102);

$head = imagecreatefrompng('head.png');
$body = imagecreatefrompng('body');


imagecopy($canvas, $head, 0, 0, 0, 0, 64, 102);
imagecopy($canvas, $body, 0, 0, 0, 0, 64, 102);

imagecolortransparent($canvas, imagecolorallocate($canvas, 0, 0, 0)); // Remove black background

imagepng($canvas);
imagedestroy($canvas);
?>

 

Link to comment
Share on other sites

Copy pasting the official Example 1 on this page and changing it to fit your images, and the right x/y coordinates and opacity, I was able to get it to work as intended.

 

http://www.php.net/manual/en/function.imagecopymerge.php

 

<?php
$dest = imagecreatefrompng('body.png');
$src = imagecreatefrompng('head.png');

// Copy and merge
imagecopymerge($dest, $src, 0, 0, 0, 0, 64, 102, 100);

// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);

imagedestroy($dest);
imagedestroy($src);
?>

Link to comment
Share on other sites

Brilliant, that works perfect -- thanks!

 

One more problem, I have 2 types of PNG, one is for the character and the other type of the items they're wearing. It seems that the PNG's have been generated differently and although this PNG appears transparent it just overlaps the existing image. :(

 

93103735.png

 

When I load the first set of images into Photoshop they appear to have a black background even though they're transparent when viewing them in Windows and when you open the above image it opens as transparent, so there's clearly something different in how they're generated or saved.

 

Can anyone tell me how to overcome this?

 

Here's the code. It works fine until the last one (armour) and then it's overlapped and just shows the one image:

 

<?php
header('Content-type: image/png');
$canvas = imagecreatetruecolor(64, 102);
imagecolortransparent($canvas, imagecolorallocate($canvas, 0, 0, 0));

$body = imagecreatefrompng('body.png');
imagecopymerge($canvas, $body, 0, 0, 0, 0, 64, 102, 100);

$head = imagecreatefrompng('head.png');
imagecopymerge($canvas, $head, 0, 0, 0, 0, 64, 102, 100);

$armour = imagecreatefrompng('armour.png');
imagecolortransparent($armour, imagecolorallocate($armour, 255, 255, 255));
imagecopymerge($canvas, $armour, 0, 0, 0, 0, 64, 102, 100);		

imagepng($canvas);

imagecanvasroy($canvas);
imagecanvasroy($src);
?>

Link to comment
Share on other sites

This example might get you some of the way there, but I don't have time to read up on the GD library right now.  Hopefully this might get you a little closer to your answer.  This is probably not the best way; I'm just trying to get it to work in general.

<?php
$body 	= imagecreatefrompng('body.png');
$head 	= imagecreatefrompng('head.png');
$armour	= imagecreatefrompng('armour.png');

//innermost->outermost
imagecopymerge($head, $body, 0, 0, 0, 0, 64, 102, 100);
imagecopymerge($armour, $head, 0, 0, 0, 0, 64, 102, 100);

header('Content-Type: image/png');
imagepng($armour); //last image used

//cleanup
imagedestroy($body);
imagedestroy($head);
imagedestroy($armour);
?>

 

If there are a reasonably finite number of combinations, I would recommend you generate them ahead of time, and just access the based on a mask of some sort.

Link to comment
Share on other sites

It works but the thing is, I'm overlapping up to 20 PNG images in one go through a function called add_sprite() like so:

 

function add_sprite($loc) {
global $char_image;
   
$spr_add = imagecreatefrompng($loc);
imagecopymerge($char_image, $spr_add, 0, 0, 0, 0, 64, 102, 100);
imagedestroy($spr_add);
}

 

With your way I don't see how I can do it because it's a different set and amount of images each time. :( All you're doing is putting the non-transparent item at the bottom and then the head on top. The problem is the item isn't properly transparent.

 

Thanks so much for your help so far.

 

Is there anyway to see what the difference is between the two PNG's? As I said the first batch work fine, it's just the items that are causing a problem as they've been generated differently. :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.