繁体   English   中英

PHP GD - 在图像上添加颜色层

[英]PHP GD - Add a color layer over a image

我想使用 gd 在 php 中的图像上添加颜色层。

这是图像: 图片

我想用这种颜色覆盖它:#ABD0D2

我做了一个快速的图像,它应该如何看待最后。 请记住,图像仍然应该是透明的在此处输入图像描述

到目前为止,我有这个代码:

$img = imagecreatefrompng('image.png');

imagesavealpha($img, true);
imagefill($img, 0, 0, imagecolorallocatealpha($img, 0, 0, 0, 127));

// make overlay with new color???

imagepng($img, 'new.png');
imagedestroy($img);

您可以创建一个新图像,用您的目标颜色填充,然后将两者合并:

$img = imagecreatefrompng('image.png');
$w = imagesx($img);
$h = imagesy($img);
imagesavealpha($img, true);

$img2 = imagecreatetruecolor($w, $h);
imagefill($img2, 0, 0, imagecolorallocatealpha($img, 0xAB, 0xD0, 0xD2, 64));

imagecopy($img, $img2, 0, 0, 0, 0, $w, $h);

imagepng($img, 'new.png');
imagedestroy($img);
imagedestroy($img2);

结果:

在此处输入图像描述

我不完全清楚你想如何保持透明度(因为你的预期结果图像不透明)所以在上面的代码中我将“遮罩”颜色设置为 50% 的不透明度。

这对我有用:

    $width = 400;
    $height = 400;
    $image = imagecreatefrompng('img.png');

    $blueOverlay = imagecreatetruecolor($width, $height);
    imagesavealpha($image, true);
    imagealphablending($image, false);
    imagesavealpha($blueOverlay, true);
    $blue = imagecolorallocatealpha($blueOverlay, 0, 0, 255, ceil(0.22 * 127));
    imagefill($blueOverlay, 0, 0, $blue);
    imagecopymerge($blueOverlay, $image, 0, 0, 0, 0, $width, $height, 70);

    imagepng($blueOverlay, 'imgWithOverlay.png');
    imagedestroy($image);
    imagedestroy($blueOverlay);

暂无
暂无

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

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