繁体   English   中英

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

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

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

要显示的图像

如您所见,我有一个半透明的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);

它输出如下所示的内容:

形象不佳

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

如果我取出:

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

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

有什么建议么?

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

<?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时,可以为化身的真实比例在宽度/高度上添加一些计算。 上面的代码将只是调整头像的大小以适合叠加层的大小。

暂无
暂无

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

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