PHPÖÆ×÷ͼÐÎÑéÖ¤Âë´úÂë·ÖÏí

5年以前  |  阅读数:780 次  |  编程语言:PHP 

Ч¹u£º

myvcode.class.php£º*a×°´´½¨ÑeÖ¤ÂeµÄÀa

<?php  
/*  
* file:myvcode.class.php  
* ÑeÖ¤ÂeÀa,ÀaÃuVcode  
*/  
class Vcode  
{  
private $width;              /*ÑeÖ¤Âe¿i¶È*/  
private $height;             /*ÑeÖ¤Âe¸ß¶È*/  
private $codeNum;            /*ÑeÖ¤Âe×Ö*u¸oÊý*/  
private $checkCode;            /*ÑeÖ¤Âe×Ö*u*/  
private $image;                /*ÑeÖ¤Âe×ÊÔ´*/  
private $pixNum;            /*»aeÖƸÉÈŵaµÄ¸oÊý*/  
private $lineNum;            /*»aeÖƸÉÈÅÏßµÄÌoÊý*/



/*  
*¹¹Ôi*½*¨ÊµÀý»¯ÑeÖ¤Âe¶ÔÏo£¬²¢³oʼ»¯Êý¾Ý  
*@param int $width         ÉeÖÃĬÈÏ¿i¶È  
*@param int $height     ÉeÖÃĬÈÏ¸ß¶È  
*@param int $codeNum    ÉeÖÃÑeÖ¤ÂeÖеÄ×Ö*u¸oÊý  
*@param int $pixNum        ÉeÖøÉÈŵaµÄ¸oÊý  
*@param int $lineNum    ÉeÖøÉÈÅÏßµÄÊýÁ¿  
*/  
function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)  
{  
$this->width = $width;  
$this->height = $height;  
$this->codeNum = $codeNum;  
$this->pixNum = $pixNum;  
$this->lineNum = $lineNum;  
}  
/*ÄÚ²¿Ë½ÓÐ*½*¨,´´½¨Í¼Ïñ×ÊÔ´*/  
private function getCreateImage()  
{  
$this->image = imagecreatetruecolor($this->width, $this->height);  
$white = imagecolorallocate($this->image,0xff,0xff,0xff);  
imagefill($this->image, 0, 0, $white);  
$black = imagecolorallocate($this->image,0,0,0);  
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);  
}  
/*ÄÚ²¿Ë½ÓÐ*½*¨,»aeÖÆ×Ö*u£¬È¥µoo0LlzºÍ012*/  
private function createCheckCode()  
{  
$code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';  
$this->checkCode = "";  
for($i=0; $i<$this->codeNum;$i++)  
{  
$char = $code{rand(0,strlen($code) - 1)};  
$this->checkCode .= $char;  
$fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));  
$fontSize = rand(3,5);  
$x = rand(0,$this->width-imagefontwidth($fontSize));  
$y = rand(0,$this->height-imagefontheight($fontSize));  
imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);  
}  
}  
/*ÄÚ²¿Ë½ÓÐ*½*¨ÉeÖøÉÈÅÔªËØ*/  
private function setDisturbColor()  
{  
/*»aeÖƸÉÈŵa*/  
for($i=0; $i<$this->pixNum; $i++)  
{  
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));  
imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);  
}  
/*»aeÖƸÉÈÅÏß*/  
for($i=0; $i<$this->lineNum; $i++)  
{  
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));  
imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),  
rand($this->width / 2,$this->width ¨C 2), rand($this->height / 2,$this->height ¨C 2), $color);



}  
}  
/*¿ªÆosession±£´ae ÀuÓÃecho Êa³oͼÏñ*/  
function __toString()  
{  
$_SESSION['code'] = strtoupper($this->checkCode);  
$this->getCreateImage();  
$this->createCheckCode();  
$this->setDisturbColor();  
$this->outputImg();  
}  
/*ÄÚ²¿Ë½ÓÐ*½*¨Êa³oͼÏñ*/  
private function outputImg()  
{  
header("content-type:image/png");  
imagepng($this->image);  
}  
/*Îo¹¹*½*¨£¬ÊÍ*ŶÔÏo*/  
function __destruct()  
{  
imagedestroy($this->image);  
}  
}  
?>

imgcode.phpÊa³oͼÏñ


    <?php
    session_start();
    require_once('myvcode.class.php');
    echo new Vcode();
    ?>

test.html:ͬ¹ýimg±eÇ(C)ÒýÓÃ


    <img src="imgcode.php">

¿ÉÒÔ¼ÓÒ»¸oa±eÇ(C)£¬ÓÃjsʵÏÖ»»Ò»ÕÅЧ¹u£º

/¾Ö²¿Ë¢Ð»»ÑeÖ¤Âe/
function changeCode()
{
var imgcode = document.getElementById(¡®code');
var change = document.getElementById(¡®change');
change.onclick = function()
{
/±ØÐe¼ÓºoÃaeµÄ²ÎÊý²ÅÄÜË¢ÐÂ/
imgcode.src='code.php?tm'+Math.random();
}
}

codeºÍchange*Ö±ðÊÇimgºÍaµÄid

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例