Php图像转换没有达到我期望的效果
下面的php代码块应该作为输入snd PRODUCT作为输出(将黑色转换为黄色,将浅蓝色转换为黑色): 然而,我得到的是输出 有人能看到我的代码有问题吗Php图像转换没有达到我期望的效果,php,image,syntax,gd,rgb,Php,Image,Syntax,Gd,Rgb,下面的php代码块应该作为输入snd PRODUCT作为输出(将黑色转换为黄色,将浅蓝色转换为黑色): 然而,我得到的是输出 有人能看到我的代码有问题吗 $im = imagecreatefrompng("./input.png"); $width = imagesx($im); $height = imagesy($im); $new = imagecreate($width, $height); imagecopy($new, $im, 0, 0, 0, 0, $width, $height
$im = imagecreatefrompng("./input.png");
$width = imagesx($im);
$height = imagesy($im);
$new = imagecreate($width, $height);
imagecopy($new, $im, 0, 0, 0, 0, $width, $height);
imagecolorset($new, imagecolorexact($new, 0, 0, 0), 255, 255, 0);
for($i = 0; $i < $width; $i++) {
for($j = 0; $j < $height; $j++) {
$index = imagecolorat($new, $i, $j);
$rgb = imagecolorsforindex($new, $index);
if($rgb['red'] != 255 && $rgb['green'] != 255 && $rgb['blue'] != 0) {
echo '(' . $i . ', ' . $j . ')' . 'color => (' . (255 - $rgb['blue']) . ', ' . (255 - $rgb['blue']) . ', 0)<br />';
$color = imagecolorallocate($new, 255 - $rgb['blue'], 255 - $rgb['blue'], 0);
imagesetpixel($new, $i, $j, $color);
}
unset($index);
unset($rgb);
}
}
imagepng($new, 'tesst.png');
imagedestroy($im);
imagedestroy($new);
$im=imagecreatefrompng(“./input.png”);
$width=imagesx($im);
$height=imagesy($im);
$new=imagecreate($width,$height);
imagecopy($new,$im,0,0,0,$width,$height);
imagecolorset($new,imagecolorexact($new,0,0),255,255,0);
对于($i=0;$i<$width;$i++){
对于($j=0;$j<$height;$j++){
$index=imagecolorat($new,$i,$j);
$rgb=imagecolorsforindex($new,$index);
如果($rgb[‘红色’!=255&&$rgb[‘绿色’!=255&&$rgb[‘蓝色’!=0){
回音“(“$i.”、“$j.”)、“颜色=>”(”(255-$rgb['blue']),“((255-$rgb['blue'])”,0)
;
$color=imagecolorallocate($new,255-$rgb['blue'],255-$rgb['blue'],0);
imagesetpixel($new、$i、$j、$color);
}
未结算(指数);
未结算($rgb);
}
}
imagepng($new,'tesst.png');
图像处理(港币);;
(新的);
我认为问题的根源在于,当使用基于调色板的图像时,例如通过调用imagecreate()
创建的图像,可以在调色板中的多个索引处声明相同的颜色
这意味着,由于在每次迭代中调用imagecoloralocate()
,调色板最终会变满,imagecoloralocate()
开始返回false
(如果在imagesetpixel()
调用之前调用var\u dump($color);
,可以看到这一点)false
转换为整数时计算结果为零-因此,当调色板填满时,所有剩余像素都有效地转换为背景色
对此,您可以做两件事,第一件也是最简单的一件事就是使用真彩色图像,这只是更改imagecreate($width,$height)的一个简单例子代码>到ImageCreateTureColor($width,$height)代码>
如果要坚持使用基于调色板的图像(例如,出于输出图像数据大小的原因-图像包含的颜色太少,基于调色板的图像会小得多),则需要手动缓存分配的颜色,以便可以重复使用,如下所示:
// ...
$colors = array();
for ($x = 0; $x < $width; $x++) { // iterate x axis
for ($y = 0; $y < $height; $y++) { // iterate y axis
// Get the color at this index
$index = imagecolorat($new, $x, $y);
// Only allocate a new color if not already done
if (!isset($colors[$index])) {
$rgb = imagecolorsforindex($new, $index);
if ($rgb['red'] != 255 || $rgb['green'] != 255 || $rgb['blue'] != 0) {
// If it's not the background color allocate a new color
$r = $g = 255 - $rgb['blue'];
$b = 0;
$colors[$index] = imagecolorallocate($new, $r, $g, $b);
} else {
// Otherwise set the index to false, we can ignore it
$colors[$index] = false;
}
}
// If there's something to do, do it
if ($colors[$index] !== false) {
imagesetpixel($new, $x, $y, $colors[$index]);
}
}
}
// ...
/。。。
$colors=array();
对于($x=0;$x<$width;$x++){//迭代x轴
对于($y=0;$y<$height;$y++){//迭代y轴
//获取此索引处的颜色
$index=imagecolorat($new,$x,$y);
//如果尚未分配新颜色,则仅分配新颜色
如果(!isset($colors[$index])){
$rgb=imagecolorsforindex($new,$index);
如果($rgb[‘红色’!=255 | |$rgb[‘绿色’!=255 | |$rgb[‘蓝色’!=0){
//如果不是背景色,请分配新颜色
$r=$g=255-$rgb[‘蓝色’];
$b=0;
$colors[$index]=IMAGECOLORDALLOCATE($new、$r、$g、$b);
}否则{
//否则将索引设置为false,我们可以忽略它
$colors[$index]=假;
}
}
//如果有什么事要做,就去做
如果($colors[$index]!==false){
imagesetpixel($new,$x,$y,$colors[$index]);
}
}
}
// ...
您可能还希望跟踪图像中正在使用的颜色,以便以后可以“清理调色板”(即,取消分配图像中不再使用的任何颜色,这将进一步帮助减少数据大小)。虽然可以说,在这种情况下,最好从干净的调色板开始,检查旧图像资源以获取像素细节,而不是将原始图像复制到新的图像资源。我认为问题的根源在于,当使用基于调色板的图像时,例如通过调用imagecreate()创建的图像
,可以在调色板中的多个索引处声明相同的颜色
这意味着,由于在每次迭代中调用imagecoloralocate()
,调色板最终会变满,imagecoloralocate()
开始返回false
(如果在imagesetpixel()
调用之前调用var\u dump($color);
,可以看到这一点)false
转换为整数时计算结果为零-因此,当调色板填满时,所有剩余像素都有效地转换为背景色
对此,您可以做两件事,第一件也是最简单的一件事就是使用真彩色图像,这只是更改imagecreate($width,$height)的一个简单例子代码>到ImageCreateTureColor($width,$height)代码>
如果要坚持使用基于调色板的图像(例如,出于输出图像数据大小的原因-图像包含的颜色太少,基于调色板的图像会小得多),则需要手动缓存分配的颜色,以便可以重复使用,如下所示:
// ...
$colors = array();
for ($x = 0; $x < $width; $x++) { // iterate x axis
for ($y = 0; $y < $height; $y++) { // iterate y axis
// Get the color at this index
$index = imagecolorat($new, $x, $y);
// Only allocate a new color if not already done
if (!isset($colors[$index])) {
$rgb = imagecolorsforindex($new, $index);
if ($rgb['red'] != 255 || $rgb['green'] != 255 || $rgb['blue'] != 0) {
// If it's not the background color allocate a new color
$r = $g = 255 - $rgb['blue'];
$b = 0;
$colors[$index] = imagecolorallocate($new, $r, $g, $b);
} else {
// Otherwise set the index to false, we can ignore it
$colors[$index] = false;
}
}
// If there's something to do, do it
if ($colors[$index] !== false) {
imagesetpixel($new, $x, $y, $colors[$index]);
}
}
}
// ...
/。。。
$colors=array();
对于($x=0;$x<$width;$x++){//迭代x轴
对于($y=0;$y<$height;$y++){//迭代y轴
//获取此索引处的颜色
$index=imagecolorat($new,$x,$y);
//如果尚未分配新颜色,则仅分配新颜色
如果(!isset($colors[$index])){
$rgb=imagecolorsforindex($new,$index);
如果($rgb[‘红色’!=255 | |$rgb[‘绿色’!=255 | |$rgb[‘蓝色’!=0){
//如果不是背景色,请分配新颜色
$r=$g=255-$rgb[‘蓝色’];
$b=0;
$colors[$index]=IMAGECOLORDALLOCATE($new、$r、$g、$b);
}否则{
//否则将索引设置为false,我们可以忽略它
$colors[$in
$color = imagecolorallocate($new, 35, 35, 0); //got from debugging