gpt4 book ai didi

php - 向 PNG 的非透明部分添加颜色叠加层

转载 作者:搜寻专家 更新时间:2023-10-31 21:19:24 24 4
gpt4 key购买 nike

我正在努力实现色轮,它会在 PNG 图像上添加一个叠加层,不透明度为 50%,这样您仍然可以看到后面图像的轮廓。

我尝试过使用干预填充方法并在每个像素的基础上进行更改。

private function colorImage($url, $r = null, $g = null, $b = null) {

$im = imagecreatefrompng($url);
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);

if (imageistruecolor($im)) {
$sx = imagesx($im);
$sy = imagesy($im);
for ($x = 0; $x < $sx; $x++) {
for ($y = 0; $y < $sy; $y++) {
$c = imagecolorat($im, $x, $y);
$a = $c & 0xFF000000;
$newColor = $a | $r << 16 | $g << 8 | $b;
imagesetpixel($im, $x, $y, $newColor);
}
}
}

ob_start();

imagepng($im);
imagedestroy($im);
$image_data = ob_get_contents();

ob_end_clean();

$image_data_base64 = "data:image/png;base64," . base64_encode($image_data);

return $image_data_base64;
}

之前:https://imgur.com/v1xRixQ

当前:https://imgur.com/67M9iCg

目标:https://imgur.com/RxzaxTP

最佳答案

您可以使用 Intervention Image图片库来执行此操作。

创建一个实例,该实例是用半透明 RGB 颜色填充的图像,使用源图像对其进行 mask ,并将 mask 作为叠加层放置在原始图像之上。

use Intervention\Image\ImageManager;

// setup
$red = 255;
$green = 0;
$blue = 0;
$opacity = 0.25;

$manager = new ImageManager(['driver' => 'gd']);

// the source image
$image = $manager->make('./board.png');

// the fill color
$fill = $manager->canvas(
$image->width(),
$image->height(),
[$red, $green, $blue, $opacity]
);

// use the source image to mask off only the portion of the fill that will be
// used as the overlay
$fill->mask($image, true);

// apply the semi-transparent, masked fill to the source image
$image->insert($fill);

// render as a png
header('Content-Type: image/png');
echo $image->encode('png');

输出: https://imgur.com/oIRP8Ir

关于php - 向 PNG 的非透明部分添加颜色叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com