Php 逐像素更改图像的颜色
我试图改变png图像的颜色,使透明区域仍然保持透明,并为图像的其余部分提供一种颜色。这就是我所尝试的Php 逐像素更改图像的颜色,php,image-processing,gd,Php,Image Processing,Gd,我试图改变png图像的颜色,使透明区域仍然保持透明,并为图像的其余部分提供一种颜色。这就是我所尝试的 <?php $im = imagecreatefrompng('2.png'); $w = imagesx($im); $h = imagesy($im); $om = imagecreatetruecolor($w,$h); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $
<?php
$im = imagecreatefrompng('2.png');
$w = imagesx($im);
$h = imagesy($im);
$om = imagecreatetruecolor($w,$h);
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgb = imagecolorat($im, $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$orgb = imagecolorallocate($om,$colors['alpha'],$colors['alpha'],$colors['alpha']);
imagesetpixel($om,$x,$y,$orgb);
}
}
header('Content-Type: image/png');
imagepng($om);
imagedestroy($om);
imagedestroy($im);
?>
它产生如下图像:
之前
之后
我仍然不太清楚如何在不失去透明度的情况下获得png图像的高亮区域并为其提供黄色或粉色等颜色,以便只有非透明区域保持透明在这种情况下,您应该能够使用调色板,而不是遍历每个像素:
<?PHP
$myRed = 255;
$myGreen = 0;
$myBlue = 0;
$im = imagecreatefrompng('http://s25.postimg.org/ll0dzblan/image.png');
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 | $myRed << 16 | $myGreen << 8 | $myBlue;
imagesetpixel($im, $x, $y, $newColor );
}
}
} else {
$numColors = imagecolorstotal($im);
$transparent = imagecolortransparent($im);
for ($i=0; $i < $numColors; $i++)
{
if ($i != $transparent)
imagecolorset($im, $i, $myRed, $myGreen, $myBlue, $myAlpha);
}
}
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
在这种情况下,您应该能够使用调色板,而不是遍历每个像素:
<?PHP
$myRed = 255;
$myGreen = 0;
$myBlue = 0;
$im = imagecreatefrompng('http://s25.postimg.org/ll0dzblan/image.png');
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 | $myRed << 16 | $myGreen << 8 | $myBlue;
imagesetpixel($im, $x, $y, $newColor );
}
}
} else {
$numColors = imagecolorstotal($im);
$transparent = imagecolortransparent($im);
for ($i=0; $i < $numColors; $i++)
{
if ($i != $transparent)
imagecolorset($im, $i, $myRed, $myGreen, $myBlue, $myAlpha);
}
}
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
只需使用imagecolorset
即可更改特定颜色,无需触摸调色板的其余部分。只需找到绿色(例如)的索引,并将其替换为黄色。这只是一个示例图像,我不知道用户提供的图像是什么,因此不知道图像中是什么颜色这也可能是用户提供的图像中使用了多种颜色,在这种情况下,您想用指定的颜色替换所有颜色吗?那么基本上是创建一个遮罩吗?是的,我想用我的颜色替换所有颜色,而不失去透明度。您只需使用imagecolorset
即可更改特定颜色,而无需触摸调色板的其余部分。只需找到绿色(例如)的索引,并将其替换为黄色。这只是一个示例图像,我不知道用户提供的图像是什么,因此不知道图像中是什么颜色这也可能是用户提供的图像中使用了多种颜色,在这种情况下,您想用指定的颜色替换所有颜色吗?那么基本上是创建一个遮罩吗?是的,我想用我的颜色替换所有颜色,而不失去透明度编辑:修复循环中的if条件。我尝试了你的代码,但仍然没有任何效果。你能用这个代码分享你的输出图像吗?我编辑了答案,这应该很有希望与真彩色图像现在。谢谢这是伟大的,它为我工作,但请你解释或修改你的代码,为什么它不是所有的png图像,如这是由你的codeEdit:Fixed如果条件在循环内。我试过你的代码,但它仍然没有做任何事。请您使用此代码与change color共享您的输出图像好吗?我编辑了答案,希望现在可以与truecolor图像一起使用。谢谢,这很好,对我很有用,但请您解释或修改您的代码,为什么它不能对所有png图像起作用,例如,这是由您的代码生成的