现在我得到了这张白色背景的图像,但想把它改成别的东西......
我试过使用imagecolorset,索引为 16777215(白色),但它不起作用 D:
还有其他人遇到过这个问题吗?更好的是,知道如何解决它?
代码:
$img = imagecreatefrompng("http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=hellow");
$index = imagecolorat($img, 0, 0);
imagecolorset($img, $index, 0, 0, 255);
header("content-type:image/png");
imagepng($img);
imagedestroy($img);
Run Code Online (Sandbox Code Playgroud)
似乎 Google 图表 API 正在创建 Truecolor 图像。笔记:
[ghoti@pc ~]$ cat imgtest1.php
<?php
$url = "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=hello";
$img = imagecreatefrompng($url);
print "Colours before: " . imagecolorstotal($img) . "\n";
imagetruecolortopalette($img, FALSE, 2);
print "Colours after: " . imagecolorstotal($img) . "\n";
[ghoti@pc ~]$ php imgtest1.php
Colours before: 0
Colours after: 2
[ghoti@pc ~]$
Run Code Online (Sandbox Code Playgroud)
因此,转换为调色板图像imagetruecolortopalette()似乎可以解决您的问题。
<?php
$url = "http://chart.apis.google.com/chart?cht=qr&chs=100x100&chl=hello";
$img = imagecreatefrompng($url); # fetch the image
imagetruecolortopalette($img, FALSE, 2); # convert image from TC to palette
$bg = imagecolorat($img, 0, 0); # get the bg colour's index in palette
imagecolorset($img, $bg, 0, 0, 255); # make it blue
header("content-type:image/png");
imagepng($img);
imagedestroy($img);
Run Code Online (Sandbox Code Playgroud)
结果:
