Квартиры, дома, земельные участки Краснодарского края без посредников. Объявления собственников недвижимости.
Реклама здесь
Главная
Примеры PHP Примеры JavaScript Примеры Ajax Примеры CSS,HTML

Пример класса captcha на php(3)


<?php
/******************************************************************

  Projectname:  CAPTCHA class
  Version:    2.0
  Author:    Pascal Rehfeldt <Pascal@Pascal-Rehfeldt.com>
  Last modified: 15. January 2006

  * GNU General Public License (Version 2, June 1991)
  *
  * This program is free software; you can redistribute
  * it and/or modify it under the terms of the GNU
  * General Public License as published by the Free
  * Software Foundation; either version 2 of the License,
  * or (at your option) any later version.
  *
  * This program is distributed in the hope that it will
  * be useful, but WITHOUT ANY WARRANTY; without even the
  * implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE. See the GNU General Public License
  * for more details.

  Description:
  This class can generate CAPTCHAs, see README for more details!

 ******************************************************************/
 
error_reporting(E_ALL);

 class 
captcha
 
{
  var 
$Length;
  var 
$CaptchaString;
  var 
$fontpath;
  var 
$fonts;
  function 
captcha ($length 6)
  {
   
header('Content-type: image/png');
   
$this->Length  $length;
   
//$this->fontpath = dirname($_SERVER['SCRIPT_FILENAME']) . '/fonts/';
   
$this->fontpath './fonts/';   
   
$this->fonts  $this->getFonts();
   
$errormgr    = new error;

   if (
$this->fonts == false)
   { 
//$errormgr = new error;
     
$errormgr->addError('No fonts available!');
     
$errormgr->displayError();
     die();
   }
   if (
function_exists('imagettftext') == false)
   {
$errormgr->addError('');
    
$errormgr->displayError();
    die();
   }
   
$this->stringGen();
   
$this->makeCaptcha();
  } 
//captcha
  
  
function getFonts ()
  {
   
$fonts = array();
   if (
$handle = @opendir($this->fontpath))
   {
    while ((
$file readdir($handle)) !== false)
    {
$extension strtolower(substr($filestrlen($file) - 33));
     if (
$extension == 'ttf')  $fonts[] = $file;
    }
    
closedir($handle);
   }
   else return 
false;
   if (
count($fonts) == 0) return false;
   else     return 
$fonts;
  
  } 
//getFonts
  
  
function getRandFont ()
  {
   return 
$this->fontpath $this->fonts[mt_rand(0count($this->fonts) - 1)];
  } 
//getRandFont

  
function stringGen ()
  {
   
$uppercase range('A''Z');
   
//$lowercase = range('a', 'z');
   
$numeric  range(09);
   
$CharPool  array_merge($uppercase$numeric);
   
$PoolLength count($CharPool) - 1;
   for (
$i 0$i $this->Length$i++)
       
$this->CaptchaString .= $CharPool[mt_rand(0$PoolLength)];
  } 
//StringGen

  
function makeCaptcha ()
  {
   
$imagelength $this->Length 25 16;
   
$imageheight 75;
   
$image    imagecreate($imagelength$imageheight);
   
//$bgcolor   = imagecolorallocate($image, 222, 222, 222);
   
$bgcolor   imagecolorallocate($image255255255);
   
$stringcolor imagecolorallocate($image000);
   
$filter   = new filters;
   
$filter->signs($image$this->getRandFont());
   for (
$i 0$i strlen($this->CaptchaString); $i++)
   {
imagettftext($image25mt_rand(-1515), $i 25 10,
           
mt_rand(3070),
           
$stringcolor,
           
$this->getRandFont(),
           
$this->CaptchaString{$i});
   }
   
//$filter->noise($image, 10);
   //$filter->blur($image, 6);
   
imagepng($image);
   
imagedestroy($image);
  } 
//MakeCaptcha

  
function getCaptchaString ()
  {
   return 
$this->CaptchaString;
  } 
//GetCaptchaString
  
 
//class: captcha


 
class error
 
{
   var 
$errors;
   function 
error ()
   {
    
$this->errors = array();
   } 
//error
   
   
function addError ($errormsg)
   {
    
$this->errors[] = $errormsg;
   } 
//addError
   
   
function displayError ()
   {
   
$iheight   count($this->errors) * 20 10;   
   
$iheight   = ($iheight 130) ? 130 $iheight;
   
$image    imagecreate(600$iheight);
   
$errorsign  imagecreatefromjpeg('./gfx/errorsign.jpg');
   
imagecopy($image$errorsign1111180120);
   
$bgcolor   imagecolorallocate($image255255255);
   
$stringcolor imagecolorallocate($image000);
   for (
$i 0$i count($this->errors); $i++)
    {
$imx = ($i == 0) ? $i 20 $i 20;
     
$msg 'Error[' $i ']: ' $this->errors[$i];
     
imagestring($image5190$imx$msg$stringcolor);
    }
   
imagepng($image);
   
imagedestroy($image);
   } 
//displayError
   
   
function isError ()
   {
    if (
count($this->errors) == 0)  return false;
    else                return 
true;
   } 
//isError
 
//class: error

 
class filters
 
{
  function 
noise (&$image$runs 30)
  {
   
$w imagesx($image);
   
$h imagesy($image);
   for (
$n 0$n $runs$n++)
   {
    for (
$i 1$i <= $h$i++)
    {
     
$randcolor imagecolorallocate($image,
                     
mt_rand(0255),
                     
mt_rand(0255),
                     
mt_rand(0255));
     
imagesetpixel($image,
            
mt_rand(1$w),
            
mt_rand(1$h),
            
$randcolor);
    }
   } 
  } 
//noise
  
  
function signs (&$image$font$cells 3)
  {
   
$w imagesx($image);
   
$h imagesy($image);
     for (
$i 0$i $cells$i++)
     {  
$centerX   mt_rand(1$w);
    
$centerY   mt_rand(1$h);
    
$amount   mt_rand(115);
    
$stringcolor imagecolorallocate($image175175175);
      
       for (
$n 0$n $amount$n++)
       {
$signs range('A''Z');
    
$sign $signs[mt_rand(0count($signs) - 1)];
        
imagettftext($image25,
              
mt_rand(-1515),
              
$centerX mt_rand(-5050),
              
$centerY mt_rand(-5050),
              
$stringcolor$font$sign);
       }
     }
  } 
//signs
  
  
function blur (&$image$radius 3)
  {
   
$radius round(max(0min($radius50)) * 2);
   
$w    imagesx($image);
   
$h    imagesy($image);
   
$imgBlur imagecreate($w$h);
   for (
$i 0$i $radius$i++)
   {
    
imagecopy   ($imgBlur$image,  0011$w 1$h 1);
    
imagecopymerge($imgBlur$image,  1100$w,   $h,   50.0000);
    
imagecopymerge($imgBlur$image,  0110$w 1$h,   33.3333);
    
imagecopymerge($imgBlur$image,  1001$w,   $h 125.0000);
    
imagecopymerge($imgBlur$image,  0010$w 1$h,   33.3333);
    
imagecopymerge($imgBlur$image,  1000$w,   $h,   25.0000);
    
imagecopymerge($imgBlur$image,  0001$w,   $h 120.0000);
    
imagecopymerge($imgBlur$image,  0100$w,   $h,   16.6667);
    
imagecopymerge($imgBlur$image,  0000$w,   $h,   50.0000);
    
imagecopy   ($image $imgBlur0000$w,   $h);
   }
   
imagedestroy($imgBlur);
  } 
//blur
 
//class: filters
?>
Скопировать в буфер
источник резервного питания

© Copyright 2008-2012 by KDG