當前位置:首頁 » 編程語言 » php圖形驗證碼

php圖形驗證碼

發布時間: 2022-04-26 00:05:22

㈠ 如何用php生成驗證碼

php生成驗證碼,php驗證碼,php怎樣生成驗證碼?
工具/原料
這個驗證碼較實用,大家可以應用到項目中。
方法/步驟
1.
<?php
/*設置文件頭為圖片輸出*/
Header("Content-type:image/JPEG");

/*調用生成驗證碼函數*/
$checkcode=make_rand(4);

/**
*生成驗證碼字元
*@paramint$length驗證碼字元長度
*@returnstring
*/
functionmake_rand($length="32"){
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$result="";
for($i=0;$i<$length;$i++){
$num[$i]=rand(0,25);
$result.=$str[$num[$i]];
}
return$result;
}
2.
/*調用輸出驗證碼圖片函數*/
getAuthImage($checkcode,160,40);

/**
*生成驗證碼圖片
*@paramstring$text驗證碼字元
*/
functiongetAuthImage($text,$w,$y){
/*設置圖片的寬度和高度*/
$im_x=$w;
$im_y=$y;
/*創建圖片*/
$im=imagecreatetruecolor($im_x,$im_y);
$text_c=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$tmpC0=mt_rand(100,255);
$tmpC1=mt_rand(100,255);
$tmpC2=mt_rand(100,255);
$buttum_c=ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
imagefill($im,16,13,$buttum_c);
3.
/*字體文件*/
$font='t1.ttf';
for($i=0;$i<strlen($text);$i++)
{
$tmp=substr($text,$i,1);
$array=array(-1,1);
$p=array_rand($array);
$an=$array[$p]*mt_rand(1,10);//角度
$size=28;
imagettftext($im,$size,$an,15+$i*$size,35,$text_c,$font,$tmp);
}

/*將字元寫入文件中*/
$distortion_im=imagecreatetruecolor($im_x,$im_y);
imagefill($distortion_im,16,13,$buttum_c);
for($i=0;$i<$im_x;$i++){
for($j=0;$j<$im_y;$j++){
$rgb=imagecolorat($im,$i,$j);
if((int)($i+20+sin($j/$im_y*2*M_PI)*10)<=imagesx($distortion_im)&&(int)($i+20+sin($j/$im_y*2*M_PI)*10)>=0){
imagesetpixel($distortion_im,(int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4),$j,$rgb);
}
}
}
4.
/*干擾元素點的數量*/
$count=160;
/*創建干擾元素點*/
for($i=0;$i<$count;$i++){
$randcolor=ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($distortion_im,mt_rand()%$im_x,mt_rand()%$im_y,$randcolor);
}
/*創建干擾線條*/
$rand=mt_rand(5,30);
$rand1=mt_rand(15,25);
$rand2=mt_rand(5,10);
for($yy=$rand;$yy<=+$rand+2;$yy++){
for($px=-80;$px<=80;$px=$px+0.1)
{
$x=$px/$rand1;
if($x!=0)
{
$y=sin($x);
}
$py=$y*$rand2;
imagesetpixel($distortion_im,$px+80,$py+$yy,$text_c);
}
}
5.
/*以PNG格式將圖像輸出到瀏覽器*/
ImagePNG($distortion_im);

/*銷毀圖像*/
ImageDestroy($distortion_im);
ImageDestroy($im);

㈡ php圖片驗證碼問題,我把圖片驗證碼隱藏之後,驗證碼怎麼不能刷新了,而且每次刷新顯示的都是一樣的

這是瀏覽器的請求規則問題,當圖片處於隱藏狀態時,更改<img/>標簽的src屬性不會重新下載圖片。要解決這個問題只有在更改src屬性後顯示一次圖片,然後再隱藏;
如:
var _src = "imgCode.php?_="+Math.random();
img.src=_src;
img.style.display="block";
img.style.display="none";

㈢ PHP圖形驗證碼識別

1.驗證碼取出,轉到8位或者24位點陣圖
2.點陣圖處理,二值化,RGB設定閥值小於閥值時為1否則為0 1為黑神色,0為白色
3.點陣圖處理,去噪點干擾,利用二值化的點陣圖,八方向法,一個孤立的噪點周圍八個方向的點都是0白色。
4.干擾線,定義邊界均為白色的區域,若干像素,讓該區域在點陣圖移動,如果進入區域內的黑色點小於某值時認定為噪點或干擾線。
5.分割。簡單平均分布可以直接固定分割。復雜的有粘連的利用投影,求字元數+1個極小值或極大值。極小值之間最小距離<極小值到前一個極小值距離<極小值之間最大距離。最大距離和最小距離按照字元長度來目測,一點一點的對比得出適當的值。
雖然此法可以解決部分粘連驗證碼,但是對於一些變態變形的公共區域比較多的驗證碼是無效的。
如果想知道更多分割方法,請到網路文庫,搜索驗證碼分割。
6.識別。建立特徵庫,或者利用神經網路自動學習。
然後比對,位元組或者文本均可。相似度自己設定,一般在90%以上
這些理論知識都學習明白了,基本就可以去做識別驗證碼了。
驗證碼最最重要且最難的一點就是分割。
有些方法不需要分割也可以借鑒一下。

㈣ 求 PHP 圖片驗證碼類 給出詳細調用方法 謝謝!!!

[code.php]

<?php

/**

*驗證碼圖片

*/

session_start();

Header("Content-type:image/gif");

/*

*初始化

*/

$border=0;//是否要邊框1要:0不要

$how=4;//驗證碼位數

$w=$how*15;//圖片寬度

$h=20;//圖片高度

$fontsize=10;//字體大小

$alpha="abcdefghijkmnpqrstuvwxyz";//驗證碼內容1:字母

$number="23456789";//驗證碼內容2:數字

$randcode="";//驗證碼字元串初始化

srand((double)microtime()*1000000);//初始化隨機數種子

$im=ImageCreate($w,$h);//創建驗證圖片

/*

*繪制基本框架

*/

$bgcolor=ImageColorAllocate($im,255,255,255);//設置背景顏色

ImageFill($im,0,0,$bgcolor);//填充背景色

if($border)

{

$black=ImageColorAllocate($im,0,0,0);//設置邊框顏色

ImageRectangle($im,0,0,$w-1,$h-1,$black);//繪制邊框

}

/*

*逐位產生隨機字元

*/

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

{

$alpha_or_number=mt_rand(0,1);//字母還是數字

$str=$alpha_or_number?$alpha:$number;

$which=mt_rand(0,strlen($str)-1);//取哪個字元

$code=substr($str,$which,1);//取字元

$j=!$i?4:$j+15;//繪字元位置

$color3=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));//字元隨即顏色

ImageChar($im,$fontsize,$j,3,$code,$color3);//繪字元

$randcode.=$code;//逐位加入驗證碼字元串

}

/*

*如果需要添加干擾就將注釋去掉

*

*以下for()循環為繪背景干擾線代碼

*/

/*+-------------------------------繪背景干擾線開始--------------------------------------------+*/

for($i=0;$i<5;$i++)//繪背景干擾線

{

$color1=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干擾線顏色

ImageArc($im,mt_rand(-5,$w),mt_rand(-5,$h),mt_rand(20,300),mt_rand(20,200),55,44,$color1);//干擾線

}

/*+-------------------------------繪背景干擾線結束--------------------------------------+*/

/*

*如果需要添加干擾就將注釋去掉

*

*以下for()循環為繪背景干擾點代碼

*/

/*+--------------------------------繪背景干擾點開始------------------------------------------+*/

/*

for($i=0;$i<$how*40;$i++)//繪背景干擾點

{

$color2=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干擾點顏色

ImageSetPixel($im,mt_rand(0,$w),mt_rand(0,$h),$color2);//干擾點

}

*/

/*+--------------------------------繪背景干擾點結束------------------------------------------+*/

//把驗證碼字元串寫入session方便提交登錄信息時檢驗驗證碼是否正確例如:$_POST['randcode']=$_SESSION['randcode']

$_SESSION['randcode']=$randcode;

/*繪圖結束*/

Imagegif($im);

ImageDestroy($im);

/*繪圖結束*/

?>

[調用方法]

<SCRIPTLANGUAGE="javaScript">

<!--

functionreloadcode(){

vard=newDate();

document.getElementById('safecode').src="/code.php?t="+d.toTimeString()

}

//-->

</SCRIPT>

驗證碼:<inputname="chknumber"type="text"maxlength="4"class="chknumber_input"/><imgsrc='code.php'id="safecode"onclick="reloadcode()"title="看不清楚?點擊切換!"></img>

㈤ PHP寫的圖形驗證碼亂碼

我吧我的給你用吧

class wxhVerify {

static function imageVerify($length=4, $mode=1, $width=48, $height=22, $verifyName='verify') {
$randval = randomString($length, $mode);
$_SESSION[$verifyName] = md5($randval);
$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
$im = @imagecreatetruecolor($width, $height);
$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);
//隨機背景色
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
//邊框色
$borderColor = imagecolorallocate($im, 100, 100, 100);
//點顏色
$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
//干擾
for ($i = 0; $i < 10; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 25; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
for ($i = 0; $i < $length; $i++) {
imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
exit;
}

}

//生成隨機字元串
function randomString($len=6, $type=1, $addChars='') {
switch ($type) {
case 0:
$chars = '' . $addChars;
break;
case 1:
$chars = str_repeat('0123456789', 3);
break;
case 2:
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
break;
case 3:
$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
break;
default :
//默認去掉了容易混淆的字元oOLl和數字01,要增加請使用addChars參數
$chars = '' . $addChars;
break;
}
if ($len > 10) {
//位數過長重復字元串一定次數
$chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
}
$chars = str_shuffle($chars);
return substr($chars, 0, $len);
}

調用 wxhVerify::imageVerify();

㈥ php的圖片驗證碼代碼

這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用
<?php

session_start();

$enablegd = 1;
//判斷圖像處理函數是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','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)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.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/png\r\n");
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/jpeg\r\n");
include $checkcodefile;
}
?>

㈦ php圖片驗證碼,為什麼非要加上ob_clean();這句話才能正常顯示呢

ob_clean這個函數的作用就是用來丟棄輸出緩沖區中的內容,如果你的網站有許多生成的圖片類文件,那麼想要訪問正確,就要經常清除緩沖區。

㈧ php中驗證碼為什麼驗證不成功

建議按照我代碼的注釋 //數字 一步步來。最簡單的方法,還是把整個代碼復制走了。
新建一個captcha.php:
php
//10>設置session,必須處於腳本最頂部
session_start();

$image = imagecreatetruecolor(100, 30); //1>設置驗證碼圖片大小的函數
//5>設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);
//10>設置變數
$captcha_code = "";
//7>生成隨機數字
for($i=0;$i<4;$i++){
//設置字體大小
$fontsize = 6;
//設置字體顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//設置數字
$fontcontent = rand(0,9);
//10>.=連續定義變數
$captcha_code .= $fontcontent;
//設置坐標
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//10>存到session
$_SESSION['authcode'] = $captcha_code;
//8>增加干擾元素,設置雪花點
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);
}
//9>增加干擾元素,設置橫線
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);
}

//2>設置頭部,image/png
header('Content-Type: image/png');
//3>imagepng() 建立png圖形函數
imagepng($image);
//4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

接著就是靜態頁的代碼了:index.html
<doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>確認驗證碼title>
head>
<body>
<form method="post" action="./form.php">
<p>驗證碼: <img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?>' style="width:100px; height:30px" />
<a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">換一個?a>
p>
<P>請輸入驗證碼:<input type="text" name='authcode' value=''/>p>
<p><input type='submit' value='提交' style='padding:6px 5px;'/>p>
</body>
</html>
從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:
php
header("Content-Type:text/html;charset=utf-8"); //設置頭部信息
//isset()檢測變數是否設置
if(isset($_REQUEST['authcode'])){
session_start();
//strtolower()小寫函數
if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){
//跳轉頁面
echo "<script language=\"javascript\">";
echo "document.location=\"./form.php\"";
echo "</script>";
}else{
//提示以及跳轉頁面
echo "<script language=\"javascript\">";
echo "alert('輸入錯誤!');";
echo "document.location=\"./form.php\"";
echo "</script>";
}
exit();
}
顯示頁面如下:

那麼,純數字的實現了,數字加英文的也應該不難了。要修改的代碼 只是在 captcha.php 將 //7>生成隨機數字 修改成 //7>生成隨機的字母和數字,如果你真的很可愛的就修改這幾個字就認為可以實現的話,那麼祝賀你,你永遠保持快樂。腦殘兒童歡樂多。
廢話不多說了,拉代碼吧。
php
//10>設置session,必須處於腳本最頂部
session_start();

$image = imagecreatetruecolor(100, 30); //1>設置驗證碼圖片大小的函數
//5>設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);
//10>設置變數
$captcha_code = "";
//7>生成隨機的字母和數字
for($i=0;$i<4;$i++){
//設置字體大小
$fontsize = 8;
//設置字體顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//設置需要隨機取的值,去掉容易出錯的值如0和o
$data ='';
//取出值,字元串截取方法 strlen獲取字元串長度
$fontcontent = substr($data, rand(0,strlen($data)),1);
//10>.=連續定義變數
$captcha_code .= $fontcontent;
//設置坐標
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//10>存到session
$_SESSION['authcode'] = $captcha_code;
//8>增加干擾元素,設置雪花點
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);
}
//9>增加干擾元素,設置橫線
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);
}

//2>設置頭部,image/png
header('Content-Type: image/png');
//3>imagepng() 建立png圖形函數
imagepng($image);
//4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

其他的兩個頁面,不許要修改。

一般而言,現在就已經夠用了。但是就像動漫一樣,總會有番外。
那麼,我們來個漢字的番外吧。其實我也准備將漢字的驗證碼放到我的畢業設計裡面,雖然現在很流行滑動驗證碼,但是本人畢竟不是專門學習js的。
而且,還可以和答辯的老師說,我們驗證碼不需要素材,連圖片也是生成的,用自己的知識裝13,也沒有設么的。
php
//11>設置session,必須處於腳本最頂部
session_start();

//1>設置驗證碼圖片大小的函數
$image = imagecreatetruecolor(200, 60);
//5>設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);
//7>設置ttf字體
$fontface = 'FZYTK.TTF';
//7>設置字型檔,實現簡單的數字儲備
$str='天地不仁以萬物為芻狗聖人不仁以百姓為芻狗這句經常出現在控訴暴君暴政上地殘暴不仁把萬物都當成低賤的豬狗來看待而那些高高在上的所謂聖人們也沒兩樣還不是把我們老百姓也當成豬狗不如的東西但實在正取的解讀是地不情感用事對萬物一視同仁聖人不情感用事對百姓一視同仁執子之手與子偕老當男女主人公含情脈脈看著對方說了句執子之手與子偕老女方淚眼朦朧含羞地回一句討厭啦這樣的情節我們是不是見過很多但是我們來看看這句的原句死生契闊與子成說執子之手與子偕老於嗟闊兮不我活兮於嗟洵兮不我信兮意思是說戰士之間的約定說要一起死現在和我約定的人都走了我怎麼活啊赤裸裸的兄弟江湖戰友友誼啊形容好基友的基情比男女之間的愛情要合適很多吧';
//str_split()切割字元串為一個數組,一個中文在utf_8為3個字元
$strdb = str_split($str,3);
//>11
$captcha_code = '';
//8>生成隨機的漢子
for($i=0;$i<4;$i++){
//設置字體顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//隨機選取中文
$in = rand(0,count($strdb));
$cn = $strdb[$in];
//將中文記錄到將保存到session的字元串中
$captcha_code .= $cn;
/*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,
string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐標,顏色,字體路徑,文本字元串
mt_rand()生成更好的隨機數,比rand()快四倍*/
imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);
}
//11>存到session
$_SESSION['authcode'] = $captcha_code;
//9>增加干擾元素,設置點
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,199), rand(1,59), $pointcolor);
}
//10>增加干擾元素,設置線
for($i=0;$i<4;$i++){
//設置線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
//設置線,兩點一線
imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);
}

//2>設置頭部,image/png
header('Content-Type: image/png');
//3>imagepng() 建立png圖形函數
imagepng($image);
//4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

㈨ PHP 繪制網站登錄首頁圖片驗證碼

幾乎所有的網站登錄頁都會有驗證碼,驗證碼是一種安全保護機制,在注冊時要求必須有人工操作進行驗證,用於防止垃圾注冊機大量注冊用戶賬號佔用伺服器內存從而使伺服器癱瘓。
圖片驗證碼的實現十分簡單。首先從指定字元集合中隨機抽取固定數目的字元,以一種不規則的方法畫在畫布上,再適當添加一些干擾點和干擾元素,最後將圖片輸出,一張嶄新的驗證碼就完成了。
先給大家展示下生成的驗證碼:

點擊刷新:

如果大家對實現效果非常滿意,請繼續往下看。
前端代碼如下:
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="content-type"
content="text/html;charset=utf-8">
<title>This
is
a
test!</title>
<link
rel="stylesheet"
type="text/css"
href="css/bootstrap.min.css">
</head>
<body>
<form
name="form">
<input
type="text"
placeholder="賬號"/><br/>
<input
type="password"
placeholder="密碼"/><br/>
<input
type="text"
placeholder="驗證碼"/>
<img
id="verImg"
src="libs/verification.php"/>
<a
href="#"
class="change"
onclick="changeVer()">點擊刷新</a><br/>
<input
type="submit"
value="登錄"/>
</form>
<script
type="text/javascript">
//刷新驗證碼
function
changeVer(){
document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
}
</script>
</body>
</html>
php腳本文件驗證碼的代碼如下:
<?php
session_start();
//開啟session記錄驗證碼數據
vCode(4,
15);//設置驗證碼的字元個數和圖片基礎寬度
//vCode
字元數目,字體大小,圖片寬度、高度
function
vCode($num
=
4,
$size
=
20,
$width
=
0,
$height
=
0)
{
!$width
&&
$width
=
$num
*
$size
*
4
/
5
+
15;
!$height
&&
$height
=
$size
+
10;
//設置驗證碼字元集合
$str
=
"";
//保存獲取的驗證碼
$code
=
''
//隨機選取字元
for
($i
=
0;
$i
<
$num;
$i++)
{
$code
.=
$str[mt_rand(0,
strlen($str)-1)];
}
//創建驗證碼畫布
$im
=
imagecreatetruecolor($width,
$height);
//背景色
$back_color
=
imagecolorallocate($im,
mt_rand(0,100),mt_rand(0,100),
mt_rand(0,100));
//文本色
$text_color
=
imagecolorallocate($im,
mt_rand(100,
255),
mt_rand(100,
255),
mt_rand(100,
255));
imagefilledrectangle($im,
0,
0,
$width,
$height,
$back_color);
//
畫干擾線
for($i
=
0;$i
<
5;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagearc($im,
mt_rand(-
$width,
$width),
mt_rand(-
$height,
$height),
mt_rand(30,
$width
*
2),
mt_rand(20,
$height
*
2),
mt_rand(0,
360),
mt_rand(0,
360),
$font_color);
}
//
畫干擾點
for($i
=
0;$i
<
50;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagesetpixel($im,
mt_rand(0,
$width),
mt_rand(0,
$height),
$font_color);
}
//隨機旋轉角度數組
$array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
//
輸出驗證碼
//
imagefttext(image,
size,
angle,
x,
y,
color,
fontfile,
text)
@imagefttext($im,
$size
,
array_rand($array),
12,
$size
+
6,
$text_color,
'c:WINDOWSFontssimsun.ttc',
$code);
$_SESSION["VerifyCode"]=$code;
//no-cache在每次請求時都會訪問伺服器
//max-age在請求1s後再次請求會再次訪問伺服器,must-revalidate則第一發送請求會訪問伺服器,之後不會再訪問伺服器
//
header("Cache-Control:
max-age=1,
s-maxage=1,
no-cache,
must-revalidate");
header("Cache-Control:
no-cache");
header("Content-type:
image/png;charset=gb2312");
//將圖片轉化為png格式
imagepng($im);
imagedestroy($im);
}
?>
好了,關於小編給大家介紹的php繪制圖片驗證就給大家介紹這么多,希望對大家有所幫助!

熱點內容
易語言灰鴿子源碼 發布:2025-07-15 17:37:08 瀏覽:868
編程代碼怎麼寫 發布:2025-07-15 17:30:45 瀏覽:120
光遇安卓九游服怎麼切換賬號 發布:2025-07-15 17:29:07 瀏覽:916
sqlserverce 發布:2025-07-15 17:20:27 瀏覽:712
海量數據的存儲管理 發布:2025-07-15 17:16:51 瀏覽:658
安卓手機哪個版本適合玩游戲 發布:2025-07-15 17:12:31 瀏覽:57
安裝flexlinux 發布:2025-07-15 16:35:47 瀏覽:372
網站模板php 發布:2025-07-15 16:22:47 瀏覽:248
編譯簡譜 發布:2025-07-15 16:16:37 瀏覽:813
編程口是什麼 發布:2025-07-15 16:11:28 瀏覽:497