程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php漢字驗證碼

php漢字驗證碼

編輯:關於PHP編程

最近因項目的要求,需要使用漢字驗證碼,於是研究了一個,在這裡貼出代碼來和大家分享一下。下面是使用php生成漢字驗證碼的具體用法和函數代碼。

用法如下:

<?php
create_excode(4);//生成四個漢字的驗證碼

漢字驗證碼圖片:

具體函數代碼如下:

<?php
/*
* $length 驗證碼漢字個數
*/
function create_excode($length){
	$randChar=array('浩','比','不','驚','靜','看','友','前','花',
	'開','龍','落','義','得',	'江','無','意','虎','望','天','外',
	'雲','卷','市','丁','中','程','人','產','名','僅','余','金',
	'國','美','幣','東','木','水','火','土','七','九','八','工',
	'碼','圖','員','電','大','秒','舒','仁');
	header('content-type: image/png');
	$charWidth=30;//每個字符占有的寬度
	$image_x=$length*$charWidth; //圖片寬度
	$image_y=40; //圖片高度
	$noise_num=100*$length; //雜點數量
	$line_num=13; //干擾線數量
	$image=imagecreate($image_x,$image_y);
	$w=$h=0;//圖片款高度初始化
	imagecolorallocate($image,250,250,250); //設定圖片背景顏色
	//imagecolorallocate($image,0xff,0xff,0xff);//白色背景
	$rectangle_color=imagecolorallocate($image,0xAA,0xAA,0xAA); //邊框顏色
	$noise_color=imagecolorallocate($image,0x00,0x00,0x00); //雜點顏色
	$font_size=18;//字體大小
	$font_y=29;//字符在Y軸上基線的位置
	$font_face='heiti.ttf'; //字體
	//加入雜點
	for($i=0;$i<$noise_num;$i++){
		imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
	}
	//生成驗證碼
	$x=2;
	$session_code='';
	for($i=0;$i<$length;$i++){
		$font_color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //字體顏色
		$code=$randChar[mt_rand(0,count($randChar)-1)];
		imagettftext($image,$font_size,mt_rand(-6,6),$x,$font_y,$font_color,$font_face,$code);
		$x+=30;
		$session_code.=$code;
	}
	//把驗證碼的值存放到session中
	@session_start();
	$_SESSION['checkCode']=$session_code;
	for($i=0;$i<$line_num;$i++){ 
		$fontcolor=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
		imagearc($image,mt_rand(-10,$w),mt_rand(-10,$h),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor); 
	}//www.phpernote.com/php-function/1010.html
	for($i=0;$i<255;$i++){ 
		$fontcolor=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
		imagesetpixel($image,mt_rand(0,$w),mt_rand(0,$h),$fontcolor); 
	}
	imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color);  //加個邊框
	imagepng($image);
	imagedestroy($image);
}

漢字驗證碼代碼下載(內含字體文件)

您可能感興趣的文章

  • php生成動態驗證碼圖片(gif)
  • php提取身份證號碼中的生日日期以及驗證是否為未成年人的函數
  • php生成驗證碼函數
  • php表單字段格式驗證類
  • js,php正則驗證是否為數字與字母的混合(6-15位)
  • php獲取漢字拼音首字母的函數(真正可以使用的)
  • PHP將簡體漢字轉為繁體的方法
  • php利用filter函數驗證郵箱、url和ip地址的方法

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved