tags:

views:

48

answers:

1

I am using this code:

    <?php
list($width, $height, $type, $attr) = getimagesize("terrain.png");
    $canvas = imagecreatetruecolor($width, $height);


$src = imagecreatefrompng("terrain.png");
if($_GET['glass'] == 1){
$src2 = imagecreatefrompng("rock.png");
}

imagecopymerge($canvas, $src, 0, 0, 0, 0, $width, $height, 100);
if($_GET['glass'] == 1){
imagecopy($canvas, $src2, 0, 0, 0, 0, 16, 16);

}
imagealphablending($canvas, true);
imagesavealpha($canvas, true);
    header("Content-type: image/png");
    imagepng($canvas);
    imagedestroy($dest);
imagedestroy($src);
?>

Terrain.png is at http://hogofwar.co.uk/experiment/mc/terrain.png (which is transparent)

How do I preserve the transparency when using GD?

A: 

You can try

Parameters:

$new_image = image resource identifier such as returned by imagecreatetruecolor(). must be passed by reference
$image_source = image resource identifier returned by imagecreatefromjpeg, imagecreatefromgif and imagecreatefrompng. must be passed by reference

<?php
function setTransparency($new_image,$image_source)
    {

            $transparencyIndex = imagecolortransparent($image_source);
            $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);

            if ($transparencyIndex >= 0) {
                $transparencyColor    = imagecolorsforindex($image_source, $transparencyIndex);   
            }

            $transparencyIndex    = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
            imagefill($new_image, 0, 0, $transparencyIndex);
             imagecolortransparent($new_image, $transparencyIndex);

    }
?> 

You can check for more details,

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

Yogesh
Does not work as far as I can work out.
Hogofwar
have you checked link to docs which Yogesh specified at the end of the answer?
mfolnovich