0

I'll explain a bit of my situation.

We got an island with cities. These could be:

  • Owned by my alliance
  • Owned by an enemy
  • Owned by nobody (free)

So we got 3 "cities"-images and an island image, looks like this:

Island

No owner

Alliance

Enemy

Now we want to put these cities images on the island image. For example, we put a city owned by nobody on the island like this:

<?php
// Get image
$im = imagecreatefrompng('island.png');
imagealphablending($im,true);

// Get our "Free-city-position" image
$stamp = imagecreatefrompng('free.png');

$pos_x = 190 - 15; // Position X = 190 - the half of the free.png image = 30 / 2 = 15
$pos_y = 225 - 15;// Position Y = 225 - the half of the free.png image = 30 / 2 = 15

imagealphablending($stamp,true);
imagecopy($im, $stamp, $pos_x, $pos_y, 0, 0, imagesx($stamp), imagesy($stamp));

// Output image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

And now there is our problem: The city image is not transparent on the island image! It looks like this: Island with city

I thought imagealphablending should do the trick, but unfortunately, it doesn't.

How can we get the transparant city image on the island?

Frank M
  • 170
  • 3
  • 14

2 Answers2

0

Taken from PHP's comment on GD functions :

If you are trying to copy a transparant image on to another image, you might assume that you should apply the ImageAlphaBlending function to the image that has the transparancy, the source image. In reality, you must apply the ImageAlphaBlending function to the destination image. Basically it's saying, "make the specified image respect transparancy".

Eric
  • 9,870
  • 14
  • 66
  • 102
  • That means I've to add the `imagealphablending` function above the `header(...)` to `$im`? Sadly, that is not working... – Frank M Jan 05 '17 at 18:55
  • No, it means, only set imagealphablending to the background image and not the foreground image. See http://php.net/manual/en/function.imagealphablending.php - 2nd comment – Eric Jan 05 '17 at 18:56
  • But that is not the solution. When I change the image to a JPG (JPEG) image it works. Is there any solution for PNG images? – Frank M Jan 05 '17 at 19:13
  • Maybe check this thread http://stackoverflow.com/questions/18281395/merging-multiple-overlapping-transparent-png-images-with-php – Eric Jan 05 '17 at 19:17
0

Try this way:

#!/usr/bin/php -f
<?php
// Read island image and get dimensions
$island=imagecreatefrompng("island.png");
$w_island=imagesx($island);
$h_island=imagesy($island);

// Read city image and get dimensions
$city= imagecreatefrompng("city.png");
$w_city=imagesx($city);
$h_city=imagesy($city);

// Create output image
$result=imagecreatetruecolor($w_island,$h_island);
imagesavealpha($result,true);
$transparent=imagecolorallocatealpha($result, 0, 0, 0, 127);
imagefill($result, 0, 0, $transparent);

// Splat island onto transparent background
imagecopy($result, $island, 0, 0, 0, 0, $w_island, $h_island);
// Splat city ontop
imagecopy($result, $city, 100, 280, 0, 0, $w_city, $h_city);
imagepng($result,"result.png");
?>

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432