Now I got this image with a white background, but want to change it to something else...
I've tried using imagecolorset, with a index of 16777215 (which is white), but it won't work D:
Anyone else who had this problem? And even better, knows how to fix it?
Code:
$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);
It seems that the Google chart API is creating Truecolor images. Note:
[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 ~]$
So converting to a palette image with imagetruecolortopalette() seems to fix your problem.
<?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);
And the result:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With