如何在 PNG 上的 PHP GD 中保持透明度

发布于 2024-09-24 09:14:52 字数 795 浏览 3 评论 0原文

我正在使用此代码:

    <?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 位于 http://hogofwar.co.uk /experiment/mc/terrain.png(透明)

使用 GD 时如何保持透明度?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-10-01 09:14:52

您可以尝试

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);

    }
?> 

您可以检查更多详细信息,

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

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

清君侧 2024-10-01 09:14:52

试试这个:

$img = imagecreatefrompng("yourimage.png");
$width = imagesx($img);
$height = imagesy($img);
$new_width=500;//this is the new width of the output image
$newheight=($height/$width)*$new_width;
$target=imagecreatetruecolor($new_width,$newheight);
$transparent=imagecolorallocatealpha($target,0,0,0,127);
imagefill($target,0,0,$transparent);
imagecopyresampled($target,$img,0,0,0,0,$new_width,$newheight,$width,$height);
imagealphablending($target,false);
imagesavealpa($target,true);
imagepng($target,"your_target_filename.png");

完成...

Try this:

$img = imagecreatefrompng("yourimage.png");
$width = imagesx($img);
$height = imagesy($img);
$new_width=500;//this is the new width of the output image
$newheight=($height/$width)*$new_width;
$target=imagecreatetruecolor($new_width,$newheight);
$transparent=imagecolorallocatealpha($target,0,0,0,127);
imagefill($target,0,0,$transparent);
imagecopyresampled($target,$img,0,0,0,0,$new_width,$newheight,$width,$height);
imagealphablending($target,false);
imagesavealpa($target,true);
imagepng($target,"your_target_filename.png");

Done...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文