PHP: How to make a green area (background) in an image transparent?

前端 未结 2 1308
清酒与你
清酒与你 2021-01-27 03:40

I\'m new here on Stackoverflow.

I would very much like figure out if it\'s possible to make a specific color of an image transparent using a PHP script. And if it is, ho

相关标签:
2条回答
  • 2021-01-27 04:18

    imagecolortransparent() will help you:

    ...
    $yourColor = imagecolorallocate($im, 0, 0, 0);
    imagecolortransparent($im, $yourColor);
    ...
    
    0 讨论(0)
  • 2021-01-27 04:32
    • demo : http://so.devilmaycode.it/php-how-to-make-a-green-area-background-in-an-image-transparent

    first of all the image should be png cause jpeg don't support transparency then the code is like this:

    <?php
       $image = 'test.png';
       $im = imagecreatefrompng($image); 
       //if you exactly know the RGB color indexes
       //$rgb = imagecolorexact($im, 0, 0, 0);
       //or keep the rgb color by position so at top 0 left 0
       $rgb = imagecolorat($im, 0, 0);
       imagecolortransparent($im, $rgb);
       header("Content-type: image/png");
       //display the image directly
       imagepng($im);
       // or save it
       // imagepng($im, 'test-to-transparent.png');
       imagedestroy($im); 
    ?>
    
    0 讨论(0)
提交回复
热议问题