更改图像背景
问题描述:
现在,我得到这个图像与白色背景,但希望将其更改为别的东西......更改图像背景
我使用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);
答
看来,谷歌图表API是创建真彩图像。注:
[[email protected] ~]$ 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";
[[email protected] ~]$ php imgtest1.php
Colours before: 0
Colours after: 2
[[email protected] ~]$
所以转换成调色板图像与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);
而结果:
+0
真棒谢谢:D – Mobilpadde
+0
+1你的爱与命令行PHP很有趣 – Baba
尝试使用'imagecreatefromstring',而不是'imagecreatefrompng' –
没有工作怎么样?没有图像输出?错误的图像?错误的背景? –
@DamienPirsy'imagecreatefromstring'将不起作用... @MarcB没有任何改变 – Mobilpadde