繁体   English   中英

更改图片背景

[英]Change image background

现在我得到了带有白色背景的图像,但是想将其更改为其他图像...

我试过使用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);

似乎Google chart 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 ~]$ 

因此,使用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);

结果:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM