0

In PHP GD, is it possible to make an image it-self transparent? I'm not talking about the background, etc. Is it possible to take a PNG image, and make the image transparent?

For example:

$c = imagecreatefrompng($urlToImage);
imagefill($c, 0, 0, imagecolorallocatealpha($c, 255, 255, 255, 50)); // 50 alpha.

imagepng($c);

This, however, doesn't do anything. Is there something I am missing? Thank you.

Alex
  • 1,398
  • 4
  • 15
  • 19

1 Answers1

1

You forgot to use imagecolortransparent() function. more at : http://www.php.net/manual/en/function.imagecolortransparent.php

Taken from comments there here is an example of adding transparency

if($transparency) {
        if($ext=="png") {
            imagealphablending($new_img, false);
            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
            imagefill($new_img, 0, 0, $colorTransparent);
            imagesavealpha($new_img, true);
        } elseif($ext=="gif") {
            $trnprt_indx = imagecolortransparent($img);
            if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }
Vit Kos
  • 5,535
  • 5
  • 44
  • 58
  • Hey there. After looking at the code and reading said comment, it looks like all it does is change every black pixel to a transparent pixel. What I'm trying to do is take every pixel in the image, and make it transparent. How do you recommend I do so? – Alex Dec 03 '13 at 09:32
  • 1
    if you need to convert any single pixel to a transparent one, just create a new image with black color and use the imagecolortransparent() fx. – Anas Bouhtouch Dec 03 '13 at 09:50
  • I apologize but that was not my question. My question was if it was possible to take a PNG image, and take every pixel and make it half-transparent (an alpha of ~65) with PHP GD. – Alex Dec 03 '13 at 21:59