PHPが作成した画像検証コード類ファイルの共有
11942 ワード
カスタム認証コードクラスに適用!
コードの例を見てみましょう.
画像タイプの検証コードを生成し、検証コードには数字と大文字が含まれ、sessionにはmd 5暗号化後の検証コードが格納されている.
例3:
次のように使用します.
テスト、検証コードクラスcodeを呼び出す.php
code_len;$i++ ){
$code .= $this->code_str[mt_rand(0, strlen($this->code_str)-1)];
}
return $this->code = $code;
}
//
public function getImage(){
$w = $this->width;
$h = $this->height;
$bg_color = $this->bg_color;
$img = imagecreatetruecolor($w, $h);
$bg_color = imagecolorallocate($img,
hexdec(substr($bg_color, 1,2)), hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2)));
imagefill($img, 0, 0, $bg_color);
$this->img = $img;
$this->create_font();
$this->create_pix();
$this->show_code();
}
//
public function create_font(){
$this->create_code();
$color = $this->font_color;
$font_color = imagecolorallocate($this->img, hexdec(substr($color,1,2)), hexdec(substr($color, 3,2)), hexdec(substr($color,5,2)));
$x = $this->width/$this->code_len;
for( $i=0;$icode_len;$i++ ){
$txt_color = imagecolorallocate($this->img, mt_rand(0,100), mt_rand(0, 150), mt_rand(0, 200));
imagettftext($this->img, $this->font_size, mt_rand(-30, 30), $x*$i+mt_rand(3, 6), mt_rand($this->height/1.2, $this->height), $txt_color, $this->font , $this->code[$i]);
//imagestring($this->img, $this->font_size, $x*$i+mt_rand(3, 6),mt_rand(0, $this->height/4) , $this->code[$i], $font_color);
}
$this->font_color = $font_color;
}
//
public function create_pix(){
$pix_color= $this->font_color;
for($i=0;$i<100;$i++){
imagesetpixel($this->img, mt_rand(0, $this->width),mt_rand(0, $this->height), $pix_color);
}
for($j=0;$j<4;$j++){
imagesetthickness($this->img, mt_rand(1, 2));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
}
}
//
public function getCode(){
return strtoupper($this->code);
}
//
private function show_code(){
header("Content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}
}
コードの例を見てみましょう.
画像タイプの検証コードを生成し、検証コードには数字と大文字が含まれ、sessionにはmd 5暗号化後の検証コードが格納されている.
buildAndExportImage();
*
* : luojing
* : 2013-3-27 11:42:12
*/
class Captcha {
private $width;//
private $height; //
private $codeNum;//
private $image;//
private $sessionKey;//session
private $captcha;//
const charWidth = 10;// ,
/**
* ,
* @param $width
* @param $height
* @param $codeNum
* @param $sessionKey session
*/
function __construct($width = 50, $height = 20, $codeNum = 4, $sessionKey = 'captcha') {
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->sessionKey = $sessionKey;
//
if($height < 20) {
$this->height = 20;
}
if($width < ($codeNum * self::charWidth + 10)) {// 5
$this->width = $codeNum * self::charWidth + 10;
}
}
/**
*
*/
public function buildAndExportImage() {
$this->createImage();
$this->setDisturb();
$this->setCaptcha();
$this->exportImage();
}
/**
* ,
*/
private function createImage() {
//
$this->image = imagecreatetruecolor($this->width, $this->height);
//
$bg = imagecolorallocate($this->image, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
//
imagefilledrectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $bg);
}
/**
*
*/
private function setDisturb() {
//
for($i = 0; $i < 150; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
imagesetpixel($this->image, mt_rand(5, $this->width - 10), mt_rand(5, $this->height - 3), $color);
}
//
for($i = 0; $i < 10; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 220), mt_rand(150, 220), mt_rand(150, 220));
imagearc($this->image, mt_rand(-10, $this->width), mt_rand(-10, $this->height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $color);
}
//
$border = imagecolorallocate($this->image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
//
imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $border);
}
/**
*
*/
private function setCaptcha() {
$str = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
//
for($i = 0; $i < $this->codeNum; $i++) {
$this->captcha .= $str{mt_rand(0, strlen($str) - 1)};
}
//
for($i = 0; $i < strlen($this->captcha); $i++) {
$color = imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
$x = floor(($this->width - 10)/$this->codeNum);
$x = $x*$i + floor(($x-self::charWidth)/2) + 5;
$y = mt_rand(2, $this->height - 20);
imagechar($this->image, 5, $x, $y, $this->captcha{$i}, $color);
}
}
/*
* , session
*/
private function exportImage() {
if(imagetypes() & IMG_GIF){
header('Content-type:image/gif');
imagegif($this->image);
} else if(imagetypes() & IMG_PNG){
header('Content-type:image/png');
imagepng($this->iamge);
} else if(imagetypes() & IMG_JPEG) {
header('Content-type:image/jpeg');
imagepng($this->iamge);
} else {
imagedestroy($this->image);
die("Don't support image type!");
}
// session ,md5
if(!isset($_SESSION)){
session_start();
}
$_SESSION[$this->sessionKey] = md5($this->captcha);
imagedestroy($this->image);
}
function __destruct() {
unset($this->width, $this->height, $this->codeNum,$this->captcha);
}
}
例3:
width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->checkCode=$this->createCheckCode();
$number=floor($width*$height/15);
if($number > 240-$codeNum){
$this->disturbColorNum= 240-$codeNum;
}else{
$this->disturbColorNum=$number;
}
}
//
function showImage($fontFace=""){
// :
$this->createImage();
// :
$this->setDisturbColor();
// :
$this->outputText($fontFace);
// :
$this->outputImage();
}
//
function getCheckCode(){
return $this->checkCode;
}
private function createImage(){
//
$this->image=imagecreatetruecolor($this->width, $this->height);
//
$backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
//
imagefill($this->image, 0, 0, $backColor);
//
$border=imagecolorallocate($this->image, 0, 0, 0);
//
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
private function setDisturbColor(){
for($i=0; $idisturbColorNum; $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);
}
for($i=0; $i<10; $i++){
$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
}
}
private function createCheckCode(){
// , 2 1 l
$code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string='';
for($i=0; $i < $this->codeNum; $i++){
$char=$code{rand(0, strlen($code)-1)};
$string.=$char;
}
return $string;
}
private function outputText($fontFace=""){
for($i=0; $icodeNum; $i++){
$fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
if($fontFace==""){
$fontsize=rand(3, 5);
$x=floor($this->width/$this->codeNum)*$i+3;
$y=rand(0, $this->height-15);
imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
}else{
$fontsize=rand(12, 16);
$x=floor(($this->width-8)/$this->codeNum)*$i+8;
$y=rand($fontSize+5, $this->height);
imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
}
}
}
private function outputImage() {
if(imagetypes() & IMG_GIF){
header("Content-Type:image/gif");
imagepng($this->image);
}else if(imagetypes() & IMG_JPG){
header("Content-Type:image/jpeg");
imagepng($this->image);
}else if(imagetypes() & IMG_PNG){
header("Content-Type:image/png");
imagepng($this->image);
}else if(imagetypes() & IMG_WBMP){
header("Content-Type:image/vnd.wap.wbmp");
imagepng($this->image);
}else{
die("PHP ");
}
}
function __destruct(){
imagedestroy($this->image);
}
}
次のように使用します.
テスト、検証コードクラスcodeを呼び出す.php
showImage(); //
$_SESSION["code"]=$code->getCheckCode(); //