简体   繁体   English

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

[英]PHP GD, Transparent PNG ontop of Full Color JPG

I am trying to create a custom looking banner avatar for a website. 我正在尝试为网站创建自定义的横幅头像。 The image below shows what needs to happen: 下图显示了需要发生的情况:

要显示的图像

As you can see, I have a semi-transparent PNG, a user provided image and I would like to make the third image. 如您所见,我有一个半透明的PNG,一个用户提供的图像,我想制作第三个图像。

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: 它输出如下所示的内容:

形象不佳

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); 并将最后一行更改为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. ,透明png会保持透明!,但是一旦我尝试将两者放在一起,它将使透明成为完美的黑色。

Any suggestions? 有什么建议么?

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 . $imgAvatar复制到$imgBanner时,可以为化身的真实比例在宽度/高度上添加一些计算。 The code above will just resize the avatar to fit the overlays size. 上面的代码将只是调整头像的大小以适合叠加层的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM