`
//PHP.ini文件error_reporting的默認配置會導致在使用$_SESSION輸出顯示亂碼 //加上error_reporting(E_ALL & ~E_NOTICE); 來屏蔽錯誤 error_reporting(E_ALL & ~E_NOTICE); $_width=75; $_height=25; $_rnd_code=4; //創建隨機碼 for ($i=0;$i<$_rnd_code;$i++){ $_nmsg .=dechex(mt_rand(0,15)); //dechex 十進制轉換爲十六進制 } //保存session $_SESSION['code'] = $_nmsg; //創建一張圖片 $_img=p_w_picpathcreatetruecolor($_width, $_height); //創建一個顏色 $_white=p_w_picpathcolorallocate($_img, 255, 255, 255); $_black=p_w_picpathcolorallocate($_img, 6, 6, 6); //填充 p_w_picpathfill() 在 p_w_picpath 圖像的座標 x,y(圖像左上角爲 0, 0)處用 color 顏色執行區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。 p_w_picpathfill($_img, 0, 0, $_white); //隨機畫出6個線條 for($i=0;$i<6;$i++){ $_rnd_color=p_w_picpathcolorallocate($_img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); p_w_picpathline($_img, mt_rand(0, $_width), mt_rand(0, $_height), mt_rand(0, $_width), mt_rand(0, $_height), $_rnd_color); } //隨機雪花 for ($i=0;$i<100;$i++){ $_rnd_color=p_w_picpathcolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); p_w_picpathstring($_img, 1, mt_rand(1, $_width), mt_rand(1, $_height), '*', $_rnd_color); } //輸出驗證碼 for ($i=0;$i<4;$i++){ p_w_picpathstring($_img,5,$i*$_width/$_rnd_code+mt_rand(1, 10),mt_rand(1, $_height/2), $_SESSION['code'][$i],$_black); } //輸出圖像 header('Content-Type:p_w_picpath/png'); p_w_picpathpng($_img);
`