PHP實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能
可自定義生成驗(yàn)證碼文字的大小、數(shù)量、干擾項(xiàng)等等,也可以自定義驗(yàn)證文字的字體。。。
廢話不多說,直接上代碼:
1、classgd.class.php
<?php Class Captcha{ private $_fontfile=''; private $_size=36; private $_width=200; private $_height=100; private $_length=4; private $_image=null; private $_snow=0; private $_pixel=0; private $_line=0; public function __construct($config=array()){ if(is_array($config)&&count($config)>0){ if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){ $this->_fontfile=$config['fontfile']; }else{ return false; } if(isset($config['size'])&&$config['size']>0){ $this->_size=(int)$config['size']; } if(isset($config['width'])&&$config['width']>0){ $this->_width=(int)$config['width']; } if(isset($config['height'])&&$config['height']>0){ $this->_height=(int)$config['height']; } if(isset($config['length'])&&$config['length']>0){ $this->_length=(int)$config['length']; } if(isset($config['snow'])&&$config['snow']>0){ $this->_snow=(int)$config['snow']; } if(isset($config['pixel'])&&$config['pixel']>0){ $this->_pixel=(int)$config['pixel']; } if(isset($config['line'])&&$config['line']>0){ $this->_line=(int)$config['line']; } $this->_image=imagecreatetruecolor($this->_width,$this->_height); return $this->_image; } else{ return false; } } public function getCaptcha(){ $white=imagecolorallocate($this->_image,255,255,255); imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white); $str=$this->_generateStr($this->_length); if(false===$str){ return false; } $fontfile=$this->_fontfile; for($i=0;$i<$this->_length;$i++){ $size=$this->_size; $angle=mt_rand(-30,30); $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10); $y=ceil($this->_height/1.5); $color=$this->_getRandColor(); //針對中文字符截取 //$text=mb_substr($str,$i,1,'utf-8'); $text=$str{$i}; imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text); } if($this->_snow){ $this->_getSnow(); }else{ if($this->_pixel){ $this->_getPixel(); } if($this->_line){ $this->_getLine(); } } header('content-type:image/png'); imagepng($this->_image); imagedestroy($this->_image); return strtolower($str); } private function _getSnow(){ for($i=1;$i<=$this->_snow;$i++){ imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor()); } } private function _getPixel(){ for($i=1;$i<=$this->_pixel;$i++){ imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor()); } } private function _getLine(){ for($i=1;$i<=$this->_line;$i++){ imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor()); } } private function _generateStr($length=4){ if($length<1 || $length>30){ return false; } $chars=array( 'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z', 'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z', 1,2,3,4,5,6,7,8,9 ); $str=join('',array_rand(array_flip($chars),$length)); return $str; } private function _getRandColor(){ return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); } } ?>
2、testCaptcha.php
<?php require_once 'classgd.class.php'; $config=array( 'fontfile'=>'fonts/simfang.ttf', //引入字體文件 //'snow'=>50, 'pixel'=>100, 'line'=>10 ); $captcha=new Captcha($config); $captcha->getCaptcha(); ?>
總結(jié)
以上所述是小編給大家介紹的PHP實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- PHP圖像處理技術(shù)實(shí)例總結(jié)【繪圖、水印、驗(yàn)證碼、圖像壓縮】
- php實(shí)現(xiàn)算術(shù)驗(yàn)證碼功能
- PHP封裝的驗(yàn)證碼工具類定義與用法示例
- php生成復(fù)雜驗(yàn)證碼(傾斜,正弦干擾線,黏貼,旋轉(zhuǎn))
- PHP驗(yàn)證碼類文件及調(diào)用方式代碼詳解
- PHP使用GD庫制作驗(yàn)證碼的方法(點(diǎn)擊驗(yàn)證碼或看不清會刷新驗(yàn)證碼)
- 一個漂亮的php驗(yàn)證碼類(分享)
- PHP生成圖片驗(yàn)證碼、點(diǎn)擊切換實(shí)例
- 如何打開php的gd2庫
- 基于GD2圖形庫的PHP生成圖片縮略圖類代碼分享
- PHP基于GD2函數(shù)庫實(shí)現(xiàn)驗(yàn)證碼功能示例
相關(guān)文章
php安裝xdebug/php安裝pear/phpunit詳解步驟(圖)
PHP環(huán)境中安裝pear、phpunit以及xdebug全攻略,大家參考使用吧2013-12-12基于HTTP長連接的"服務(wù)器推"技術(shù)的php 簡易聊天室
關(guān)于HTTP長連接的“服務(wù)器推”技術(shù)原理可以查看IBM的這篇文章,我簡單的做了個DEMO.2009-10-10基于linnux+phantomjs實(shí)現(xiàn)生成圖片格式的網(wǎng)頁快照
在代碼區(qū)看到一個生成站點(diǎn)快照的代碼,看了半天才發(fā)現(xiàn),作者僅僅貼出來業(yè)務(wù)代碼,最核心的生成快照圖片的代碼反而沒有給出來。 以前記得google搜索提供站點(diǎn)縮略圖,那時候覺得好神奇,但是沒有花時間去做深入的調(diào)研。昨天又遇到了,那就順便調(diào)研下吧。2015-04-04Thinkphp 框架擴(kuò)展之應(yīng)用模式實(shí)現(xiàn)方法分析
這篇文章主要介紹了Thinkphp 框架擴(kuò)展之應(yīng)用模式,結(jié)合實(shí)例形式分析了Thinkphp 擴(kuò)展應(yīng)用模式的具體原理、實(shí)現(xiàn)方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-04-04