看不清?點擊圖片即可切換驗證碼<\/span>
\r\n \r\n <\/form>\r\n <\/body>\r\n<\/html><\/pre>2、createcode.class.php中的代碼:<\/p>
gdcheck()){\r\n $this->img_width = $img_width;\r\n $this->img_height = $img_height;\r\n $this->str_content = $str_content;\r\n $this->code_content_color = $code_content_color;\r\n $this->get_code();\r\n $this->session_code();\r\n }\r\n }\r\n \/\/生成畫布\r\n public function get_img(){\r\n \/\/定義畫布\r\n $this->img = imagecreatetruecolor($this->img_width, $this->img_height);\r\n \/\/畫布背景色\r\n $this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));\r\n \/\/給畫圖填充背景色\r\n imagefill($this->img, 0, 0, $this->img_bgcolor);\r\n \/\/取得畫布的寬高\r\n $img_width = imagesx($this->img);\r\n $img_height = imagesy($this->img);\r\n \/\/畫布中插入驗證碼\r\n 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))));\r\n \/\/畫布中插入像素點\r\n $this->get_pix();\r\n \/\/畫布中插入直線\r\n $this->get_line();\r\n \/\/畫布顯示\r\n header('Content-type:image\/png');\r\n imagepng($this->img);\r\n }\r\n \/\/生成驗證碼\r\n private function get_code(){\r\n $str_content_len = strlen($this->str_content);\r\n for($i=0;$i<4;$i++){\r\n $this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1);\r\n }\r\n }\r\n \/\/生成像素點\r\n private function get_pix(){\r\n for($j=0;$j<300;$j++){\r\n $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)));\r\n }\r\n return $image_pix;\r\n }\r\n \/\/生成直線\r\n private function get_line(){\r\n for($l=0;$l<2;$l++){\r\n $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)));\r\n }\r\n return $img_line;\r\n }\r\n \/\/session存儲驗證碼\r\n private function session_code(){\r\n session_start();\r\n $_SESSION['code'] = $this->code_content;\r\n }\r\n \/\/判斷程序是否支持GD庫\r\n private function gdcheck(){\r\n if(extension_loaded('gd')){\r\n return true;\r\n }else{\r\n return false;\r\n exit();\r\n }\r\n }\r\n }<\/pre>3、checkcode.php中的代碼:<\/p>
Home
類庫下載
PHP類庫
PHP generates image verification code demo [OOP object-oriented version]
PHP generates image verification code demo [OOP object-oriented version]
Oct 09, 2016 am 09:19 AM
PHP生成圖片驗證碼demo【OOP面向?qū)ο蟀姹尽?/p>
下面是我今天下午用PHP寫的一個生成圖片驗證碼demo,僅供參考。
這個demo總共分為4個文件,具體代碼如下:
1、code.html中的代碼:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>登錄、注冊驗證碼生成</title>
</head>
<body>
<!--
* @Description 網(wǎng)站登錄/注冊驗證碼生成類
* @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="/static/imghw/default1.png" data-src="showcode.php" class="lazy" onclick="this.setAttribute('src','showcode.php?'+Math.random())" / alt="PHP generates image verification code demo [OOP object-oriented version]" >
<span>看不清?點擊圖片即可切換驗證碼</span><br/>
<input type="submit" name="sub" value="登錄/注冊" />
</form>
</body>
</html>
2、createcode.class.php中的代碼:
<?php
/**
* @Description 網(wǎng)站登錄/注冊驗證碼生成類
* @Author 趙一鳴
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
*/
class Createcode{
//畫布資源
public $img;
//畫布寬度
private $img_width;
//畫布高度
private $img_height;
//畫布顏色
private $img_bgcolor;
//驗證碼文字內(nèi)容
private $str_content;
//生成的驗證碼內(nèi)容
private $code_content;
//驗證碼顏色
private $code_content_color;
//構(gòu)造函數(shù)
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
/**
* @Description 網(wǎng)站登錄/注冊驗證碼生成類
* @Author 趙一鳴
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
*/
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
/**
* @Description 網(wǎng)站登錄/注冊驗證碼生成類
* @Author 趙一鳴
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
*/
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();
原文地址:http://www.zymseo.com/php/334.html
轉(zhuǎn)載請注明出處!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn