国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

一步一步教你做php生成驗證碼程序代碼

2024-04-25 20:37:11
字體:
來源:轉載
供稿:網友

一步一步教你做php生成驗證碼程序代碼

代碼如下:

<?php

session_start();

$enablegd = 1;

//判斷圖像處理函數是否存在

$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellips教程e','imagepng');

foreach($funcs as $func)

{

if(!function_exists($func))

{

$enablegd = 0;

break;

}

}

ob_clean(); //清理緩沖

if($enablegd)

{

//create captcha

$consts = 'cdfgkmnpqrstwxyz23456';

$vowels = 'aek23456789';

for ($x = 0; $x < 6; $x++)

{

$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數

$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數

}

$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];

$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //顯示4個字符

$imageX = strlen($radomstring)*8; //圖像的寬

$imageY = 20; //圖像的高

$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像

//creates two variables to store color

$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色

$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),

imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))

);

$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配顏色并說明透明度

$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中間背景

$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中間背景2

//與左上角的顏色相同的都會被填充

imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));

//往圖像上寫入文字

imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf', $string[0]);

imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf', $string[1]);

imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)],'C:WindowsFontsArial.ttf', $string[2]);

imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)],'C:WindowsFontsArial.ttf', $string[3]);

//畫邊框

$border = imagecolorallocate($im, 133, 153, 193);

imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);

//畫一些隨機出現的點

$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

for ($i=0;$i<80;$i++)

{

imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);

}

//畫隨機出現的線

for ($x=0; $x<9;$x++)

{

if(mt_rand(0,$x)%2==0)

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //畫線

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //畫橢圓

}

else

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);

}

}

//output to browser

header("content-type:image/pngrn");

imagepng($im);

imagedestroy($im);

}

else

{

$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('請檢查文件目錄完整性:/images/checkcode/');

$checkcodefile = $files[rand(0, count($files)-1)]; //隨機其中一個文件

$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //獲得文件名

header("content-type:image/jpegrn");

include $checkcodefile;

}

?>

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 分宜县| 新河县| 富顺县| 阿巴嘎旗| 仲巴县| 确山县| 武冈市| 滁州市| 德阳市| 顺义区| 东平县| 正安县| 荣昌县| 那曲县| 峨边| 卓资县| 夏津县| 河源市| 宁晋县| 鹤壁市| 姜堰市| 木里| 大石桥市| 武乡县| 资兴市| 东城区| 鄢陵县| 嘉鱼县| 两当县| 石家庄市| 肥城市| 衡阳市| 南充市| 上犹县| 阳原县| 凤翔县| 巴彦县| 开封市| 长岛县| 崇阳县| 津市市|