下面是我今天下午用PHP寫的一個生成圖片驗證碼demo,僅供參考。文章底部提供demo在線預覽地址。
這個demo總共分為4個文件,具體代碼如下:
1、code.html中的代碼:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>登錄、注冊驗證碼生成</title>
</head>
<body>
<!--
* @Description 網站登錄/注冊驗證碼生成類
* @Author 趙一鳴
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
-->
<form action="checkcode.php" method="post">
<input type="text" name="code" /><br/>
<img src="showcode.php" onclick="this.setAttribute('src','showcode.php?'+Math.random())" />
<span>看不清?點擊圖片即可切換驗證碼</span><br/>
<input type="submit" name="sub" value="登錄/注冊" />
</form>
</body>
</html>
2、createcode.class.php中的代碼:
<?php
class Createcode{
//畫布資源
public $img;
//畫布寬度
private $img_width;
//畫布高度
private $img_height;
//畫布顏色
private $img_bgcolor;
//驗證碼文字內容
private $str_content;
//生成的驗證碼內容
private $code_content;
//驗證碼顏色
private $code_content_color;
//構造函數
public function __construct($img_width,$img_height,$str_content,$code_content_color){
if($this->gdcheck()){
$this->img_width = $img_width;
$this->img_height = $img_height;
$this->str_content = $str_content;
$this->code_content_color = $code_content_color;
$this->get_code();
$this->session_code();
}
}
//生成畫布
public function get_img(){
//定義畫布
$this->img = imagecreatetruecolor($this->img_width, $this->img_height);
//畫布背景色
$this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
//給畫圖填充背景色
imagefill($this->img, 0, 0, $this->img_bgcolor);
//取得畫布的寬高
$img_width = imagesx($this->img);
$img_height = imagesy($this->img);
//畫布中插入驗證碼
imagestring($this->img, 5, ($this->img_width/3), ($this->img_height/2.5), $this->code_content, imagecolorallocate($this->img, hexdec(substr($this->code_content_color, 1,2)), hexdec(substr($this->code_content_color, 3,2)), hexdec(substr($this->code_content_color, 5,2))));
//畫布中插入像素點
$this->get_pix();
//畫布中插入直線
$this->get_line();
//畫布顯示
header('Content-type:image/png');
imagepng($this->img);
}
//生成驗證碼
private function get_code(){
$str_content_len = strlen($this->str_content);
for($i=0;$i<4;$i++){
$this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1);
}
}
//生成像素點
private function get_pix(){
for($j=0;$j<300;$j++){
$image_pix .= imagesetpixel($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
}
return $image_pix;
}
//生成直線
private function get_line(){
for($l=0;$l<2;$l++){
$img_line .= imageline($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
}
return $img_line;
}
//session存儲驗證碼
private function session_code(){
session_start();
$_SESSION['code'] = $this->code_content;
}
//判斷程序是否支持GD庫
private function gdcheck(){
if(extension_loaded('gd')){
return true;
}else{
return false;
exit();
}
}
}
3、checkcode.php中的代碼:
<?php
header('Content-type:text/html;charset="utf-8"');
session_start();
if($_POST['code']!=''){
if($_SESSION['code']==$_POST['code']){
echo '<script type="text/javascript">
alert("驗證碼填寫成功");
history.go(-1);
</script>';
}else{
echo '<script type="text/javascript">
alert("驗證碼填寫失敗");
history.go(-1);
</script>';
}
}
4、showcode.php中的代碼:
<?php
function __autoload($classname){
include strtolower($classname).'.class.php';
}
//定義驗證碼的取值范圍
$str_content = 'abcdefghijklmnopqrstuvwxyz0123456789';
//驗證碼文字顏色
$code_content_color = '#ffffff';
//初始化對象
$code = new Createcode(100,30,$str_content,$code_content_color);
$code->get_img();
- 0、php安裝
- 1、選擇PHP作為首選后端語言的原因
- 2、PHP基本語法
- 3、PHP中變量和常量的區別
- 4、PHP中單引號和雙引號的區別
- 5、PHP檢測數據類型的幾種方式
- 6、PHP數據類型轉換
- 7、return、break、continue的區別
- 8、PHP代碼重用
- 9、字符串移除或添加函數
- 10、PHP中字符串大小寫切換以及翻轉和加密
- 11、PHP字符串截取和截取函數
- 12、PHP字符串替換和比較函數
- 13、PHP字符串url解析和實體轉換
- 14、addslashes在預定義字符串前添加反斜杠
- 15、PHP中的數組基本概念
- 16、數組函數implode、explode、in_array、each、list
- 17、PHP數組函數(count、reset、end、next、current、key)
- 18、ZendStudio軟件破解安裝包免費下載
- 19、【圖文】ZendStudio漢化方法
- 20、數組函數in_array、array_search、array_change_key_case、array_chu
- 21、關閉PHP提示的方法(運行出現Notice : Use of undefined constant 的完美解決方案
- 22、PHP數組函數(array_diff_ukey、array_diff_uassoc、array_intersect
- 23、PHP數組函數(array_fill、array_filter、array_flip、array_key_exis
- 24、PHP數組去重及向前向后刪除元素的函數
- 25、PHP數組函數(array_map、array_walk、array_walk_recursive)
- 26、PHP數組函數(compact、extract)
- 27、PHP數組函數(array_merge、array_merge_recursive)
- 28、PHP數組函數(range、array_count_values、array_product)
- 29、PHP數組函數(array_reduce、array_slice、array_splice、array_sum)
- 30、PHP數組排序函數總結
- 31、PHP中面向對象的基本概念及定義對象的方法
- 32、PHP創建對象與構造函數
- 33、PHP對象的釋放
- 34、PHP面向對象的特性(抽象、封裝、繼承、多態)
- 35、PHP面向對象的public、private、protected之間的區別
- 36、PHP面向對象中的final和const的用法
- 37、PHP面向對象的static關鍵字
- 38、PHP中的單例模式
- 39、$this、self、parent詳解
- 40、PHP中面向對象的抽象類和抽象方法
- 41、PHP面向對象中的接口interface
- 42、PHP面向對象中的魔術方法
- 43、面向對象方法get_object_vars、is_subclass_of、interface_exists
- 44、PHP中的數學函數方法總結
- 45、PHP文件處理disk_total_space、disk_free_space、dirname、file_exis
- 46、PHP目錄操作rename和scandir
- 47、PHP操作目錄opendir、readdir、glob
- 48、PHP打開fopen、讀取fread、寫入文件fwrite
- 49、PHP文件處理file_get_contents、file_put_contents、fgetc、fgets、fg
- 50、PHP截取字符串出現亂碼的解決方法(UTF8和GBK)
- 51、PHP文件內容分頁操作,避免亂碼
- 52、PHP文件操作函數file、set_include_path、copy
- 53、PHP文件操作函數filemtime、filectime、fileatime、touch
- 54、PHP文件操作指針函數feof、ftell、fseek、rewind、fpassthru
- 55、PHP文件操作pathinfo、realpath、flock、tempnam、tmpfile
- 56、設置php.ini配置實現表單文件上傳流程
- 57、PHP將上傳到臨時文件夾的文件移動到服務器指定文件夾內
- 58、PHP+input表單實現多文件上傳
- 59、PHP通過超鏈接實現文件下載
- 60、發送header文件頭信息實現文件下載
- 61、PHP中的日期時間_時區時間戳函數使用
- 62、PHP操作COOKIE緩沖區影響COOKIE傳遞方式
- 63、PHP中SESSION定義、建立、刪除方式
- 64、PHP.INI配置文件中關于session的設置
- 65、HP中Session緩存的概念與用法
- 66、PHP自定義Session處理機制
- 67、通過MYSQL數據庫操作Session會話完整源碼類
- 68、PHP連接數據庫實現分頁代碼
- 69、HTTP緩存實現原理詳解
- 70、PHP連接數據庫實現搜索+分頁功能
- 71、PHP圖像處理之建立畫布填充顏色、打開不同圖像類型處理
- 72、PHP顏色或圖像填充及不同填充方式
- 73、PHP圖像處理之畫線&定義線樣式&寬度&風格&筆刷
- 74、PHP圖像處理:繪制色素及矩形圖
- 75、PHP圖像處理之畫圓、弧線、網站餅狀統計圖繪制
- 76、PHP圖像處理之繪制多邊行及文字繪制
- 77、PHP 圖像處理之獲得文字寬高屬性&圖片水印函數功能
- 78、PHP生成圖片驗證碼demo【OOP面向對象版本】
- 79、PHP圖像處理 圖像處理之處理文字及獲得文字尺寸屬性,imagecopymerge imagecop
- 80、替換用戶輸入的QQ表情
- 81、PHP封裝MYSQL數據庫操作類源碼
- 82、PHP與XML技術結合使用解析