PHP
·
发表于 5年以前
·
阅读量:8297
Ч¹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