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

问题描述 投票:0回答:1

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

这是图像:Image

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

我快速了解了它应该如何看待最后。请记住,图像应该仍然是透明的enter image description here

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

$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);
php image colors overlay gd
1个回答
0
投票

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

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

结果:

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.