讲师中心 微信公众号

工具推荐

视频效率加速
Laper
Laper

剧本策划到成片协作

剧云
剧云

中文短剧剧本制作平台

SoundView
SoundView

字幕擦除翻译、配音口型同步

Seko
Seko

AI短剧一键生成成片

PHP图形验证码程序

秋静大大_7107

秋静大大_7107

发布时间:2016-05-19 14:05:23

|

1370人浏览过

|

来源于php中文网

原创

最近经常有人问我这个Blog中的图形验证码是如何做的.下面是这个php图形验证码的源程序.

欢迎指正.

class.image.php

/*
===========================================================  
   class.image.php   Zhu Weiwei
   Copyright © 2006-2008
   http://www.cncmm.com China. All Rights Reserved.
===========================================================
*/
class Image
{
   PRivate $img;
   private $width;
   private $height;
   private $color;
   private $font;
   private $mime="image/jpeg";

   /* 1,GIF  2,JPEG  3,PNG */
   private $type=2;
   private $angle=0;
   private $text;
   private $textW;
   private $textH;
   private $size=5;
   private $tcolor;

 
   function __construct($w="100", $h="50"){
     $this->width=$w;
     $this->height=$h;
   }
  
   function create() {
     if ($this->type != 1 && function_exists('imagecreatetruecolor')) {
       // 本函数不能用于 GIF 文件格式
       // 本函数需要 GD 2.0.1 或更高版本。
       // 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像
       $this->img=imagecreatetruecolor($this->width,$this->height);
     }else {
       $this->img=imagecreate($this->width,$this->height);
     }    
   }

   /*
   * 1=GIF   2=JPG
   * 3=PNG
   */
   function createFromFile($f){
     $imgInfo=getimagesize($f);
     $this->type=$imgInfo[2];

   switch($this->type){
     case 1:
       $this->loadGifFile($f);
       break;
      
     case 2:
       $this->loadJpegFile($f);
       break;
      
     case 3:
       $this->loadPngFile($f);
       break;
      
     default:
       die("This Image Type Not Support loadimage !");
       exit();
     }
   }
  
   function loadGifFile($f){
     // 1.6  -- 2.0.28
     //二者之间版本的 GD 库时本函数不可用
     $this->img=imagecreatefromgif($f ) or die("Load Gif picture error!") ;
     $this->width=imagesx($this->img); 
     $this->height=imagesy($this->img); 
     $this->mime="image/gif";
     $this->type=1;
   }
  
  
   function loadJpegFile($f){
     //JPEG 支持仅在 PHP 与 GD-1.8 或更高版本一起编译时可用
     $this->img=imagecreatefromjpeg($f) or die("Load Jpeg picture error!") ;
     $this->width=imagesx($this->img); 
     $this->height=imagesy($this->img); 
     $this->mime="image/jpeg";
     $this->type=2;
   }
  
  
   function loadPngFile($f){
     $this->img=imagecreatefrompng($f )  or die("Load Png picture error!") ;
     $this->width=imagesx($this->img); 
     $this->height=imagesy($this->img); 
     $this->mime="image/png";
     $this->type=3;
   }

 
  
   function setBackgroundColor($r, $g, $b){
     ## 用color 颜色填充了的矩形
     ## 其左上角坐标为 x1,y1
     ## 右下角坐标为 x2,y2
     ## 0, 0 是图像的最左上角
     $color =imagecolorallocate($this->img, $r, $g, $b);
     return imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $color);
   }
  
   function setColor($r, $g, $b){
     $this->color=imagecolorallocate($this->img, $r, $g, $b);
     return (-1 == $this->color) ? (false) : ($this->color);
   }
  
  
   function setTextColor($r, $g, $b){
     $this->tcolor=imagecolorallocate($this->img, $r, $g, $b);
     return (-1 == $this->tcolor) ? (false) : ($this->tcolor);
   }
  
   
   function setText($t){
     $this->text=$t;
   }

   function waterMark($r, $g, $b, $alpha){
     $this->tcolor=imagecolorclosestalpha($this->img, $r, $g, $b, $alpha);
   }
   
  
   function setFontFile($f){ 
     $this->font=$f;
   }
  
   function addTrueTypeText($xoffset,$yoffset){
     #$x=( ( $this->width - $this->textW - 10) > 0) ? ($this->width - $this->textW - 10) : (0) ;
     #$y=( ( $this->height-10) >  0) ? ($this->height-10) : ($this->height);
     // 用 TrueType 字体向图像写入文本
     // **x,y 坐标是字符串左下角坐标,整幅图像的左上角为 0,0
     imagettftext($this->img,
       $this->size,
       $this->angle,
       $xoffset,
       $yoffset,
       $this->tcolor,
       $this->font,
       $this->text);
   }

   function addText() {
     // http://www.devdao.com/ 用 color 颜色将字符串 s 画到 image
     // **x,y 坐标是字符串左上角坐标,整幅图像的左上角为 0,0
     // 如果font 是 1,2,3,4 或 5,则使用内置字体。
     imagestring ($this->img, $font, $x, $y, $this->text, $this->tcolor);    
   }
  
   function setSize($s){
     $this->size=$s;
   }
  
   function setAngle($a){
     $this->angle=$a;
   }   
  
  
   function setWidth($w){
     $this->width=$w;
   }
  
  
   function setHeight($h){
     $this->height=$h;
   }
  
  
   function getWidth(){
     return $this->width;
   }
  
  
   function getHeight(){
     return $this->height;
   }

   function setPixel($x,$y,$color){
     return imagesetpixel($this->img, $x, $y, $color);
   }

   function setLine($x1,$y1,$x2,$y2,$color){
     return imageline($this->img, $x1, $y1, $x2, $y2, $color);
   }
  
   function createImageFile($f="default"){
     if( !empty($f) ){
       switch($this->type){
         case 1:
         imagegif($this->img, $f.".gif",80);
         break;
        
         case 2:
         imagejpeg($this->img, $f.".jpg" , 80);
         break;
        
         case 3:
         imagepng($this->img, $f.".png", 80);
         break;
        
         default:
         die("This Image Type Not Support create !");
         exit();
       }
       imagedestroy($this->img);
     }
   }

                  
   function show(){
     header("Content-type: ".$this->mime); 
     switch($this->type){
       case 1:
       imagegif($this->img);
       break;
      
       case 2:
       imagejpeg($this->img);
       break;
      
       case 3:
       imagepng($this->img);
       break;
      
       default:
       die("This Image Type Not Support  show !");
       exit();
     }
     imagedestroy($this->img);
   }
   
   /* Zoom Out Picture */
   function roomOut($maxW, $maxH){
     $WW=(($maxW) and ($this->width > $maxW));
     $HH=(($maxH) and ($this->height > $maxH));
    
     if($WW or $HH){
       if($WW){
         $wRatio =$maxW / $this->width;
         $wResize=true;
       }
       if($HH){
         $hRatio =$maxH / $this->height;
         $hResize=true;
       }
      
       if($wResize and $hResize){
         if($wRatio            $ratio=$wRatio;
         }else{
           $ratio=$hRatio;
         }
       }elseif($wResize){
         $ratio=$wRatio;
       }elseif($hResize){
         $ratio=$hRatio;
       }
     }else{
       $ratio=1;
     }
    
     $newWidth=$this->width * $ratio; 
     $newHeight=$this->height * $ratio;
    
     if(function_exists("imagecopyresampled")){
       $newimg=imagecreatetruecolor($newWidth, $newHeight);
       imagecopyresampled($newimg, $this->img, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
     }else{
       $newimg=imagecreate($newWidth, $newHeight);
       imagecopyresized($newimg, $this->img, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
     }
     $this->img=$newimg;    
     $this->width=  $newWidth;
     $this->height=$newHeight;
   }
}
imgverifycode.php

/*
===========================================================  
   imgverifycode.php   Zhu Weiwei
   Copyright © 2006-2008
   http://www.cncmm.com China. All Rights Reserved.
===========================================================
*/
require('class.image.php');
# char number
$n = 4;
#font size
$s = 18;
#font padding
$p = 5;
#font angle -a,a
$a = 15;
#image width
$w = ($s+$p)*$n+$p;
#image height;
$h = intval($s*1.5);

$c = ($h + $s)/2;
$o = abs($h-$s-2*$p)/2;
$r = 1;

$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
#$str = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz';
#$str = '0123456789';

$len = strlen($str)-1;
$code = '';


$im = new Image($w,$h);
$im->create();
$im->setBackgroundColor( mt_rand(0xdd,0xff), mt_rand(0xdd,0xff), mt_rand(0xdd,0xff) );

$im->setSize( $s );

for($i=$p; $i   $char = $str{mt_rand(0,$len)};
   $code = $code.$char;
  
   $im->setFontFile($CFG->dirroot.'/font/'.mt_rand(2,5).'.ttf');
  
   $im->setText( $char );
   $im->setAngle( mt_rand(-$a,$a) );
   $im->setTextColor(mt_rand(0x0,0x66),mt_rand(0x0,0x66),mt_rand(0x0,0x66) );
   $im->addTrueTypeText($i,mt_rand($c-$o,$c+$o));
   for($j=0; $j     $xs = mt_rand($i-$s/2+$p,$i+$s/2-$p);
     $ys = mt_rand($p,$h-$p);
     $color = $im->setColor(mt_rand(0x0,0x66),mt_rand(0x0,0x66),mt_rand(0x0,0x66));
     $im->setLine( $xs, $ys, $xs+mt_rand(-$s/2,$s/2), $ys+mt_rand(-$s/2,$s/2), $color);
   }
}  

$SESS = new session();
$SESS->setParameter('verifycode',$code);

for($i=0; $i   $color = $im->setColor(mt_rand(0x0,0x66),mt_rand(0x0,0x66),mt_rand(0x0,0x66));
   $im->setPixel(mt_rand(0,$w), mt_rand(0,$h),$color);
}

$im->show();

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

超级麦吉

一款通过数字员工协作完成调研、数据分析与文档制作的AI办公平台。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

Atoms
Atoms Hot

Atoms是一款AI智能体工具,第一支自动构建真实业务的 AI 团队。

讯飞绘文

讯飞绘文是一款由科大讯飞推出的一站式 AIGC 内容运营平台。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

VibeKnow
VibeKnow Hot

一款AI视频创作工具,主要用于全球首个AI知识视频创作平台,文档、文章、网页,一键生成视频,适合需要提升相关任务效率的用户。

咔片AIPPT

一款在线AI演示文稿制作工具,可根据主题和内容需求辅助生成PPT结构与页面,提高演示材料制作效率。

相关专题

更多
Navicat创建数据库教程
Navicat创建数据库教程

本专题整理Navicat创建数据库相关教程,介绍新建前的准备、基础设置和创建后的检查方法。内容兼顾入门学习及实际使用需求,帮助用户了解不同数据库类型下的操作差异,为后续建表、保存数据和日常管理提供参考。

0

2026.11.20

Navicat入门教程大全
Navicat入门教程大全

本专题整理Navicat入门教程,从认识软件界面到开展数据库操作,介绍初次使用需要了解的知识和方法。内容兼顾连接配置、数据管理及日常使用中的疑问,方便用户按学习进度查阅,为后续实践和独立操作打好基础。

0

2026.11.20

Navicat数据库连接配置指南
Navicat数据库连接配置指南

从确认数据库信息到建立可用连接,本专题串联Navicat连接数据库的相关知识和操作方法。既有初次配置时需要了解的基础内容,也有连接后的检查思路,方便用户按实际需求查阅教程,逐步熟悉数据库连接的设置流程。

0

2026.11.20

PHP8.6数组与数据处理教程
PHP8.6数组与数据处理教程

本专题汇总PHP8.6数组与数据处理教程,涵盖CSV读取和导出、引用遍历删除、array_filter回调模式、配置键名冲突、pathinfo文件名校验、GMP大整数进制转换及UTF-8正则筛选,说明升级兼容变化、输入范围和失败处理方法。

0

2026.11.20

PHP8.6升级迁移教程
PHP8.6升级迁移教程

本专题汇总PHP8.6升级迁移与弃用接口改造教程,涵盖反射调用、对象属性赋值、标识符更名、构造函数与__debugInfo调整,以及插件类名检查、常量导出、ArrayIterator排序、SPL和DTO查询参数转换,帮助梳理弃用提示并完成代码兼容改造。

0

2026.11.20

PHP8.6安装配置与FPM运维教程
PHP8.6安装配置与FPM运维教程

本专题汇总PHP8.6正式版安装部署与FPM运维教程,涵盖Linux并存安装、独立ini配置、扩展重新编译、Nginx切换与回滚、FPM状态监控和慢日志排查,以及环境变量、请求超时、OPcache刷新和预加载配置。

0

2026.11.20

Nginx CVE-2026-123007排查与防护指南
Nginx CVE-2026-123007排查与防护指南

Nginx HTTP/2漏洞该怎么修复?本专题围绕CVE-2026-123007,介绍安全版本升级、模块兼容检查和配置恢复方法,以及日志排查、权限设置等后续加固操作,方便你按实际部署情况查找对应教程。

20

2026.11.20

GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

80

2026.11.17

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

80

2026.11.17

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn