1

I'm having a rough time getting this transparent PNG (map marker) to be transparent being copied onto the larger PNG (radar image). I've tried a number of different things and all have failed. It must be something quirky because if I use a rectangle filled with a color and apply the map marker the transparency is respected. I'm stumped at the moment even though I'm still working the problem.

Below I've included dropbox links to the two images and the php code that I've been testing with. Once the transparency gets worked out I'm going to try and figure out how to resize the map marker, don't need it that big!

Thank you for your time and help!

<?php

//$im = @imagecreatetruecolor(1440,768) or die("Cannot Initialize new GD image stream");
$im = imagecreatefrompng('new_radar_image.png');

$color_red = imagecolorallocate ($im,255,0,0);
$color_white = imagecolorallocate ($im,255,255,255);
$radar_size_x = imagesx($im);
$radar_size_y = imagesy($im);
$mid_x = $radar_size_x / 2;
$mid_y = $radar_size_y / 2;

// imagefilledrectangle ( $im, 0 , 0 , 1439 , 767 , $color_red );

imageline ( $im, $mid_x , 0, $mid_x, $radar_size_y, $color_white);
imageline ( $im, 0, $mid_y, $radar_size_x, $mid_y , $color_white);

$src_im = imagecreatefrompng('map-marker-icon1.png');
imagecopy ( $im , $src_im , 0  , 0 , 0 , 0 , 128 , 128);

header('Content-Type: image/png');
imagealphablending( $im, false );
imagesavealpha( $im, true );
imagepng($im);
imagedestroy($im);

?>

map marker

radar image

miken32
  • 42,008
  • 16
  • 111
  • 154
Handler
  • 57
  • 7

2 Answers2

0

The documentation reads:

Transparency is copied only with imagecopymerge() and true color images, not with imagecopy() or pallete images.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • I tried that as well with imagecopymerge. I went the whole range from 0 to 100 stopping in the middle. All it did was fade out the merged image instead of overlaying it with it's transparency respected. – Handler Aug 10 '16 at 14:26
0

I got it! It dawned on my while on the treadmill! =)

<?php

$im = imagecreatefrompng('new_radar_image.png');
$om = imagecreatetruecolor(1440,768);
imagecopy($om,$im,0,0,0,0,1440,768);

$color_red = imagecolorallocate ($om,255,0,0);
$color_white = imagecolorallocate ($om,255,255,255);
$radar_size_x = imagesx($om);
$radar_size_y = imagesy($om);
$mid_x = $radar_size_x / 2;
$mid_y = $radar_size_y / 2;

imageline ( $om, $mid_x , 0, $mid_x, $radar_size_y, $color_white);
imageline ( $om, 0, $mid_y, $radar_size_x, $mid_y , $color_white);

$src_im = imagecreatefrompng('map-marker-icon.png');
imagecopy( $om , $src_im , 0  , 0 , 0 , 0 , 128 , 128);

header('Content-Type: image/png');
imagealphablending( $om, false );
imagesavealpha( $om, true );
imagepng($om);
imagedestroy($om);
imagedestroy($im);

?>

Just have to resize it which shouldn't be an issue!

Handler
  • 57
  • 7