要使用jpgraph類庫
代碼
<?php
session_start();
header("content-type:image/png"); //設(shè)置創(chuàng)建圖像的格式
$image_width=70; //設(shè)置圖像寬度
$image_height=18; //設(shè)置圖像高度
srand(microtime()*100000); //設(shè)置隨機(jī)數(shù)的種子
for($i=0;$i<4;$i++){ //循環(huán)輸出一個4位的隨機(jī)數(shù)
$new_number.=dechex(rand(0,15));
}
$_SESSION[check_checks]=$new_number; //將獲取的隨機(jī)數(shù)驗證碼寫入到SESSION變量中
$num_image=imagecreate($image_width,$image_height); //創(chuàng)建一個畫布
imagecolorallocate($num_image,255,255,255); //設(shè)置畫布的顏色
for($i=0;$i<strlen($_SESSION[check_checks]);$i++){ //循環(huán)讀取SESSION變量中的驗證碼
$font=mt_rand(3,5); //設(shè)置隨機(jī)的字體
$x=mt_rand(1,8)+$image_width*$i/4; //設(shè)置隨機(jī)字符所在位置的X坐標(biāo)
$y=mt_rand(1,$image_height/4); //設(shè)置隨機(jī)字符所在位置的Y坐標(biāo)
$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //設(shè)置字符的顏色
imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color); //水平輸出字符
}
imagepng($num_image); //生成PNG格式的圖像
imagedestroy($num_image); //釋放圖像資源
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者