解決php gd函數 ImageCreatetruecolor 做背景顏色只顯示黑色的問(wèn)題
最近學(xué)php項目的開(kāi)發(fā),遇到不少問(wèn)題。用php做驗證碼,發(fā)現使用gd函數imagecolorallocate(X,X,X)時(shí),無(wú)論把x設成三原色的何值都顯示黑色背景,無(wú)法改變背景色,
如下代碼:
<?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里找了整整半天,終于找到答案
問(wèn)題的關(guān)鍵是ImageCreatetruecolor函數的局限性,換成另外一個(gè)函數就好了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);
?>
代碼沒(méi)有問(wèn)題,可以自行測試一下。
各位轉載的兄弟姐妹們,希望加資料來(lái)自:http://hi.baidu.com/binbin_1988/blog/item/f1cc5025daa02439c89559f7.html 給我博客加點(diǎn)人氣啊。