[PHP] 基于GD库的php验证码类(支持中英文字体、背景、干扰点线、扭曲.......) →→→→→进入此内容的聊天室

来自 , 2021-01-01, 写在 PHP, 查看 164 次.
URL http://www.code666.cn/view/b4a721cf
  1. <?php
  2. /*
  3.  * Captcha Class base on PHP GD Lib
  4.  * @author Design
  5.  * @version 1.0
  6.  * @copyright js8.in 2010
  7.  * @demo
  8.  * include('captchaClass.php');
  9.  * $captchaDemo=new Captcha();
  10.  * $captchaDemo->createImage();
  11.  */
  12. class Captcha
  13. {
  14.     //@定义验证码图片高度
  15.     private $height;
  16.     //@定义验证码图片宽度
  17.     private $width;
  18.     //@定义验证码字符个数
  19.     private $textNum;
  20.     //@定义验证码字符内容
  21.     private $textContent;
  22.     //@定义字符颜色
  23.     private $fontColor;
  24.     //@定义随机出的文字颜色
  25.     private $randFontColor;
  26.     //@定义字体大小
  27.     private $fontSize;
  28.     //@定义字体
  29.     private $fontFamily;
  30.     //@定义背景颜色
  31.     private $bgColor;
  32.     //@定义随机出的背景颜色
  33.     private $randBgColor;
  34.     //@定义字符语言
  35.     private $textLang;
  36.     //@定义干扰点数量
  37.     private $noisePoint;
  38.     //@定义干扰线数量
  39.     private $noiseLine;
  40.     //@定义是否扭曲
  41.     private $distortion;
  42.     //@定义扭曲图片源
  43.     private $distortionImage;
  44.     //@定义是否有边框
  45.     private $showBorder;
  46.     //@定义验证码图片源
  47.     private $image;
  48.    
  49.     //@Constructor 构造函数
  50.     public function Captcha()
  51.     {
  52.         $this->textNum    = 4;
  53.         $this->fontSize   = 16;
  54.         $this->fontFamily = 'c:\windows\fontsSIMYOU.ttf'; //设置中文字体,可以改成linux的目录
  55.         $this->textLang   = 'en';
  56.         $this->noisePoint = 30;
  57.         $this->noiseLine  = 3;
  58.         $this->distortion = false;
  59.         $this->showBorder = false;
  60.     }
  61.    
  62.    
  63.     //@设置图片宽度
  64.     public function setWidth($w)
  65.     {
  66.         $this->width = $w;
  67.     }
  68.    
  69.     //@设置图片高度
  70.     public function setHeight($h)
  71.     {
  72.         $this->height = $h;
  73.     }
  74.    
  75.     //@设置字符个数
  76.     public function setTextNumber($textN)
  77.     {
  78.         $this->textNum = $textN;
  79.     }
  80.    
  81.     //@设置字符颜色
  82.     public function setFontColor($fc)
  83.     {
  84.         $this->fontColor = sscanf($fc, '#%2x%2x%2x');
  85.     }
  86.    
  87.     //@设置字号
  88.     public function setFontSize($n)
  89.     {
  90.         $this->fontSize = $n;
  91.     }
  92.    
  93.     //@设置字体
  94.     public function setFontFamily($ffUrl)
  95.     {
  96.         $this->fontFamily = $ffUrl;
  97.     }
  98.    
  99.     //@设置字符语言
  100.     public function setTextLang($lang)
  101.     {
  102.         $this->textLang = $lang;
  103.     }
  104.    
  105.     //@设置图片背景
  106.     public function setBgColor($bc)
  107.     {
  108.         $this->bgColor = sscanf($bc, '#%2x%2x%2x');
  109.     }
  110.    
  111.     //@设置干扰点数量
  112.     public function setNoisePoint($n)
  113.     {
  114.         $this->noisePoint = $n;
  115.     }
  116.    
  117.     //@设置干扰线数量
  118.     public function setNoiseLine($n)
  119.     {
  120.         $this->noiseLine = $n;
  121.     }
  122.    
  123.     //@设置是否扭曲
  124.     public function setDistortion($b)
  125.     {
  126.         $this->distortion = $b;
  127.     }
  128.    
  129.     //@设置是否显示边框
  130.     public function setShowBorder($border)
  131.     {
  132.         $this->showBorder = $border;
  133.     }
  134.    
  135.     //@初始化验证码图片
  136.     public function initImage()
  137.     {
  138.         if (empty($this->width)) {
  139.             $this->width = floor($this->fontSize * 1.3) * $this->textNum + 10;
  140.         }
  141.         if (empty($this->height)) {
  142.             $this->height = $this->fontSize * 2;
  143.         }
  144.         $this->image = imagecreatetruecolor($this->width, $this->height);
  145.         if (empty($this->bgColor)) {
  146.             $this->randBgColor = imagecolorallocate($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
  147.         } else {
  148.             $this->randBgColor = imagecolorallocate($this->image, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
  149.         }
  150.         imagefill($this->image, 0, 0, $this->randBgColor);
  151.     }
  152.    
  153.     //@产生随机字符
  154.     public function randText($type)
  155.     {
  156.         $string = '';
  157.         switch ($type) {
  158.             case 'en':
  159.                 $str = 'ABCDEFGHJKLMNPQRSTUVWXY3456789';
  160.                 for ($i = 0; $i < $this->textNum; $i++) {
  161.                     $string = $string . ',' . $str[mt_rand(0, 29)];
  162.                 }
  163.                 break;
  164.             case 'cn':
  165.                 for ($i = 0; $i < $this->textNum; $i++) {
  166.                     $string = $string . ',' . chr(rand(0xB0, 0xCC)) . chr(rand(0xA1, 0xBB));
  167.                 }
  168.                 $string = iconv('GB2312', 'UTF-8', $string); //转换编码到utf8
  169.                 break;
  170.         }
  171.         return substr($string, 1);
  172.     }
  173.    
  174.     //@输出文字到验证码
  175.     public function createText()
  176.     {
  177.         $textArray         = explode(',', $this->randText($this->textLang));
  178.         $this->textContent = join('', $textArray);
  179.         if (empty($this->fontColor)) {
  180.             $this->randFontColor = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  181.         } else {
  182.             $this->randFontColor = imagecolorallocate($this->image, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);
  183.         }
  184.         for ($i = 0; $i < $this->textNum; $i++) {
  185.             $angle = mt_rand(-1, 1) * mt_rand(1, 20);
  186.             imagettftext($this->image, $this->fontSize, $angle, 5 + $i * floor($this->fontSize * 1.3), floor($this->height * 0.75), $this->randFontColor, $this->fontFamily, $textArray[$i]);
  187.         }
  188.     }
  189.    
  190.     //@生成干扰点
  191.     public function createNoisePoint()
  192.     {
  193.         for ($i = 0; $i < $this->noisePoint; $i++) {
  194.             $pointColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  195.             imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $pointColor);
  196.         }
  197.        
  198.     }
  199.    
  200.     //@产生干扰线
  201.     public function createNoiseLine()
  202.     {
  203.         for ($i = 0; $i < $this->noiseLine; $i++) {
  204.             $lineColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), 20);
  205.             imageline($this->image, 0, mt_rand(0, $this->width), $this->width, mt_rand(0, $this->height), $lineColor);
  206.         }
  207.     }
  208.    
  209.     //@扭曲文字
  210.     public function distortionText()
  211.     {
  212.         $this->distortionImage = imagecreatetruecolor($this->width, $this->height);
  213.         imagefill($this->distortionImage, 0, 0, $this->randBgColor);
  214.         for ($x = 0; $x < $this->width; $x++) {
  215.             for ($y = 0; $y < $this->height; $y++) {
  216.                 $rgbColor = imagecolorat($this->image, $x, $y);
  217.                 imagesetpixel($this->distortionImage, (int) ($x + sin($y / $this->height * 2 * M_PI - M_PI * 0.5) * 3), $y, $rgbColor);
  218.             }
  219.         }
  220.         $this->image = $this->distortionImage;
  221.     }
  222.    
  223.     //@生成验证码图片
  224.     public function createImage()
  225.     {
  226.         $this->initImage(); //创建基本图片
  227.         $this->createText(); //输出验证码字符
  228.         if ($this->distortion) {
  229.             $this->distortionText();
  230.         } //扭曲文字
  231.         $this->createNoisePoint(); //产生干扰点
  232.         $this->createNoiseLine(); //产生干扰线
  233.         if ($this->showBorder) {
  234.             imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->randFontColor);
  235.         } //添加边框
  236.         imagepng($this->image);
  237.         imagedestroy($this->image);
  238.         if ($this->distortion) {
  239.             imagedestroy($this->$distortionImage);
  240.         }
  241.         return $this->textContent;
  242.     }
  243.    
  244. }
  245. ?>
  246.  
  247.  
  248. 使用方法:
  249. <?php
  250. //session_start();
  251. header("Content-type:image/png");
  252. include('captcha5_class.php');
  253. $captcha5 = new Captcha();
  254.  
  255. //@设置验证码宽度
  256. //$captcha5->setWidth(200);
  257.  
  258. //@设置验证码高度
  259. //$captcha5->setHeight(50);
  260.  
  261. //@设置字符个数
  262. $captcha5->setTextNumber(5);
  263.  
  264. //@设置字符颜色
  265. //$captcha5->setFontColor('#ff9900');
  266.  
  267. //@设置字号大小
  268. //$captcha5->setFontSize(25);
  269.  
  270. //@设置字体
  271. $captcha5->setFontFamily('c:\windows\fonts\STXINGKA.TTF');
  272.  
  273. //@设置语言
  274. $captcha5->setTextLang('cn');
  275.  
  276. //@设置背景颜色
  277. //$captcha5->setBgColor('#000000');
  278.  
  279. //@设置干扰点数量
  280. //$captcha5->setNoisePoint(600);
  281.  
  282. //@设置干扰线数量
  283. //$captcha5->setNoiseLine(10);
  284.  
  285. //@设置是否扭曲
  286. //$captcha5->setDistortion(true);
  287.  
  288. //@设置是否显示边框
  289. $captcha5->setShowBorder(true);
  290.  
  291. //输出验证码
  292. $code = $captcha5->createImage();
  293. //$_SESSION['captchaCode']['content']=$code;
  294. //$_SESSION['captchaCode']['time']=microtime();
  295. ?>

回复 "基于GD库的php验证码类(支持中英文字体、背景、干扰点线、扭曲.......)"

这儿你可以回复上面这条便签

captcha