解决php gd函数 ImageCreatetruecolor 做背景颜色只显示黑色的问题
最近学php项目的开发,遇到不少问题。用php做验证码,发现使用gd函数imagecolorallocate(X,X,X)时,无论把x设成三原色的何值都显示黑色背景,无法改变背景色,
如下代码:
<?php
/*
* 验证码代码
*/
session_start();
for ($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));
$_SESSION[check_pic]=$rand;
}
//echo $rand;
$im = ImageCreatetruecolor(60,20);
$background = imagecolorallocate($im, 125, 255, 255);
$fontcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im,6,rand(1,25),rand(1,5),$rand, $fontcolor);
header("Content-type: image/jpeg");
imagejpeg($im);
?>
在谷歌,baidu里找了整整半天,终于找到答案
问题的关键是ImageCreatetruecolor函数的局限性,换成另外一个函数就好了ImageCreate ,ImageCreate 的用法与ImageCreatetruecolor一样。
下面是我更改后的代码:
<?php
/*
* 验证码代码
*/
session_start();
for ($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));
$_SESSION[check_pic]=$rand;
}
//echo $rand;
$im = ImageCreate (60,20);
$background = imagecolorallocate($im, 125, 255, 255);
$fontcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im,6,rand(1,25),rand(1,5),$rand, $fontcolor);
header("Content-type: image/jpeg");
imagejpeg($im);
?>
代码没有问题,可以自行测试一下。
各位转载的兄弟姐妹们,希望加资料来自:http://hi.baidu.com/binbin_1988/blog/item/f1cc5025daa02439c89559f7.html 给我博客加点人气啊。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。