PHP GD,全彩 JPG 之上的透明 PNG

发布于 2024-12-12 11:03:58 字数 1613 浏览 0 评论 0原文

我正在尝试为网站创建一个自定义外观的横幅头像。下图显示了需要发生的情况:

要显示的图像

正如你所看到的,我有一个半透明的 PNG,一个用户提供的图像,我想制作第三张图像。

到目前为止我编写的代码是:

$user_id = 1;   
$name_qry = mysql_query("SELECT a.*, b.* FROM mbr_user_name a, mbr_user_information b WHERE a.user_id = '$user_id' AND b.user_id = '$user_id'");
    while($row = mysql_fetch_array($name_qry)){

    $user_name = $row['user_name'];
    $user_email = $row['user_email'];
    $user_avatar = $row['user_avatar'];
    }

    $height = "208";
    $width = "199";
    $top_image = "../images/bannerShadow_cccccb.png";
    $image = imagecreatefrompng("." . $user_avatar);
    $banner = imagecreatefrompng($top_image);

        //Keeping the Banner Trasnparent
        $transBanner = imagecreate($width, $height);
        $color = imagecolorallocatealpha($transBanner, 0, 0, 0, 127);
        imagefill($transBanner, 0, 0, $color);
        imagecopyresampled($transBanner, $banner, 0, 0, 0, 0, $width, $height, $width, $height);


    imagealphablending($transBanner, true);
    imagecopymerge($image, $transBanner, 0, 0, 0, 0, 199, 208, 100);



    imagepng($image);

它输出如下所示的内容:

Bad Image

我显然仍然必须使用户提供的图像具有正确的尺寸,这是一个简单的数学问题 - 现在,我必须使透明度保持透明!

如果我取出:

imagealphablending($transBanner, true);
imagecopymerge($image, $transBanner, 0, 0, 0, 0, 199, 208, 100);

并将最后一行更改为 imagepng($transBanner);,透明 png 将保持透明!,但是一旦我尝试将两者放在一起,它会使透明度成为完美的黑色颜色。

有什么建议吗?

I am trying to create a custom looking banner avatar for a website. The image below shows what needs to happen:

Image to display

As you can see, I have a semi-transparent PNG, a user provided image and I would like to make the third image.

The code that I have written so far is:

$user_id = 1;   
$name_qry = mysql_query("SELECT a.*, b.* FROM mbr_user_name a, mbr_user_information b WHERE a.user_id = '$user_id' AND b.user_id = '$user_id'");
    while($row = mysql_fetch_array($name_qry)){

    $user_name = $row['user_name'];
    $user_email = $row['user_email'];
    $user_avatar = $row['user_avatar'];
    }

    $height = "208";
    $width = "199";
    $top_image = "../images/bannerShadow_cccccb.png";
    $image = imagecreatefrompng("." . $user_avatar);
    $banner = imagecreatefrompng($top_image);

        //Keeping the Banner Trasnparent
        $transBanner = imagecreate($width, $height);
        $color = imagecolorallocatealpha($transBanner, 0, 0, 0, 127);
        imagefill($transBanner, 0, 0, $color);
        imagecopyresampled($transBanner, $banner, 0, 0, 0, 0, $width, $height, $width, $height);


    imagealphablending($transBanner, true);
    imagecopymerge($image, $transBanner, 0, 0, 0, 0, 199, 208, 100);



    imagepng($image);

It outputs something like what is shown below:

Bad Image

I obviously still have to make the user provided image the right size, That is a simple math problem - Right now, I have to make the transparency stay transparent!

If I take out:

imagealphablending($transBanner, true);
imagecopymerge($image, $transBanner, 0, 0, 0, 0, 199, 208, 100);

and change the last line to imagepng($transBanner);, the transparent png will stay transparent!, but once I try to put the two together, It makes the transparency a perfect black color.

Any suggestions?

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

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

发布评论

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

评论(1

很糊涂小朋友 2024-12-19 11:03:58

我前段时间做了类似的事情。我在那里使用了真彩色图像。只需创建一个新图像并将每个图像(头像和叠加层)的调整大小版本复制到其中:

<?php
$imgOverlay = imagecreatefrompng('overlay.png');
$imgAvatar = imagecreatefrompng('avatar.png');

$width = imagesx($imgOverlay);
$height = imagesy($imgOverlay);

$imgBanner = imagecreatetruecolor($width, $height);
imagecopyresampled($imgBanner, $imgAvatar, 0, 0, 0, 0, $width, $height, imagesx($imgAvatar), imagesy($imgAvatar));
imagecopyresampled($imgBanner, $imgOverlay, 0, 0, 0, 0, $width, $height, $width, $height);

header('Content-type: image/png');
imagepng($imgBanner);

$imgAvatar 复制到 <代码>$imgBanner。上面的代码只会调整头像的大小以适应叠加层的大小。

I did something similar some time ago. There I used a true color image. Just create a new image and copy a resized version of each (avatar and overlay) into it:

<?php
$imgOverlay = imagecreatefrompng('overlay.png');
$imgAvatar = imagecreatefrompng('avatar.png');

$width = imagesx($imgOverlay);
$height = imagesy($imgOverlay);

$imgBanner = imagecreatetruecolor($width, $height);
imagecopyresampled($imgBanner, $imgAvatar, 0, 0, 0, 0, $width, $height, imagesx($imgAvatar), imagesy($imgAvatar));
imagecopyresampled($imgBanner, $imgOverlay, 0, 0, 0, 0, $width, $height, $width, $height);

header('Content-type: image/png');
imagepng($imgBanner);

You may add some calculations to the width/height for a real scale of your avatar when copying $imgAvatar into $imgBanner. The code above will just resize the avatar to fit the overlays size.

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