混合颜色 GD - PHP
好的,我有一个 64x64 像素的图像,有些像素是白色的,有些是灰色的,有些较暗,所以我有另一个 64x64 像素的图像,其中一些黄色像素将确定必须更改第一个图像的哪些像素。 到目前为止,我可以使用以下代码更改第一张图像上的颜色,但问题是我不知道如何将给定颜色与第一张图像上已有的颜色“混合”。
例如,如果像素为白色 (255,255,255),新颜色为红色 (255,0,0),则结果将为 (255,0,0),但如果像素稍暗,则新红色也应为更暗。有什么想法吗?
$image = 'o1.png';
$overlay = 'o2.png';
$background = imagecreatefrompng($image);
imagealphablending($background, true);
// Create overlay image
$overlay = imagecreatefrompng($overlay);
// get size
$size = getimagesize("o2.png");
$L=$size[0];
$H=$size[1];
for($j=0;$j<$H;$j++){
for($i=0;$i<$L;$i++){
$rgb = imagecolorat($overlay, $i, $j);
$red = (isset($_GET['r']) ? $_GET['r'] : 0);
$green = (isset($_GET['g']) ? $_GET['g'] : 0);
$blue = (isset($_GET['b']) ? $_GET['b'] : 0);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if(($r==255)&&($g==255)&&($b==0)) {
$color = imagecolorallocate($background, $red, $green, $blue);
imagesetpixel($background, $i, $j, $color);
}
}
}
header("Content-type: image/png");
header("Content-Disposition: filename=" . $image);
imagepng($background);
// Destroy the images
imagedestroy($background);
imagedestroy($overlay);
Ok, I have a 64x64 pixels image, some pixels are white, some are grey, and some darker, so I have another 64x64 pixel image with some yellow pixels which will determine which pixels of the first image must be changed.
So far I could change the colors on the first image with the following code, but the thing is I have no idea how to "blend" the given color with the colors that were already on the first image.
For example, if a pixel is white (255,255,255) and the new color is red (255,0,0) the result will be (255,0,0) but if the pixel is a bit darker, the new red should also be darker. Any ideas?
$image = 'o1.png';
$overlay = 'o2.png';
$background = imagecreatefrompng($image);
imagealphablending($background, true);
// Create overlay image
$overlay = imagecreatefrompng($overlay);
// get size
$size = getimagesize("o2.png");
$L=$size[0];
$H=$size[1];
for($j=0;$j<$H;$j++){
for($i=0;$i<$L;$i++){
$rgb = imagecolorat($overlay, $i, $j);
$red = (isset($_GET['r']) ? $_GET['r'] : 0);
$green = (isset($_GET['g']) ? $_GET['g'] : 0);
$blue = (isset($_GET['b']) ? $_GET['b'] : 0);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if(($r==255)&&($g==255)&&($b==0)) {
$color = imagecolorallocate($background, $red, $green, $blue);
imagesetpixel($background, $i, $j, $color);
}
}
}
header("Content-type: image/png");
header("Content-Disposition: filename=" . $image);
imagepng($background);
// Destroy the images
imagedestroy($background);
imagedestroy($overlay);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你正在谈论多重混合模式。根据维基百科,其公式为:
使用此公式,背景颜色较暗的情况下生成的图像会较暗。
I think you are talking about multiply blend mode. The formula for this according to Wikipedia is:
Using this formula the resulting image will be darker where the background color is darker.