純6位數字驗證碼(圖片)


<?php

//中文字符集
header('content-type:text/html;charset=utf-8');

//設置session,必須處於腳本最頂部
session_start();

//設置驗證碼圖片大小的函數
$image = imagecreatetruecolor(600, 90);

//設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255);

//區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域着色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);

//設置變量
$captcha_code = "";

//生成隨機數字6個
for($i=0;$i<6;$i++){

//設置字體大小
$fontsize = 10;

//設置字體顏色,隨機顏色。0-120深顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));

//設置數字
$fontcontent = rand(0,9);

//=連續定義變量
$captcha_code .= $fontcontent;

//設置坐標
$x = ($i*100/6)+rand(5,15);
$y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}

//存到session
$_SESSION['authcode'] = $captcha_code;

//增加干擾元素,設置雪花點
for($i=0;$i<200;$i++){

//設置點的顏色,50-200顏色比數字淺,不干擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}

//增加干擾元素,設置橫線
for($i=0;$i<4;$i++){

//設置線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));

//設置線,兩點一線
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
}

//設置頭部,image/png
header('Content-Type: image/png');

//imagepng() 建立png圖形函數
imagepng($image);

//imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM