谷歌图表 API 似乎正在创建真彩色图像。笔记:
[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 ~]$
因此,转换为调色板图像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);
结果: