PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP 验证码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

今天分享一篇博客上 https://jingxin.me/blog/blog/2013/01/13/PHP-yan-zheng-ma/ 关于 PHP 验证码的处理,这里是用原生 PHP生成的。

gd是一个强大的PHP图像处理库,最近在做验证码加强的策略,才发现用PHP作图也能玩出很多花样来。

几个重要函数

  1. imagecreatetruecolor 创建一张空的画布

  2. imagecreatefrompng 从文件创建一个图片句柄

  3. imagecolorallocate 拾取一种颜色(rgb)

  4. imagettftext 向画布写入文字

  5. imagecopy 合并两张图片,可指定拷贝区域及大小

  6. imagecolorat 从图片指定像素点拾取一种颜色

  7. imagesetpixel 画一个像素点

  8. imagearc 画一个椭圆,截取部分可用来绘制曲线

数字加英文的组合

  1. public function genCode($n = 4) {

  2.    $dict = 'ABCDEFGHIJKLNMPQRSTUVWXYZ123456789';

  3.    $dictlen = strlen($dict);

  4.    $image = $this->image;

  5.    $verify = '';

  6.    $fontfile = $this->sourcedir . $this->fonts[0];

  7.    $colors = array(

  8.        imagecolorallocate($image, 255, 0, 0) , //红

  9.        imagecolorallocate($image, 0, 0, 255) , //蓝

  10.        imagecolorallocate($image, 0, 0, 0) , //黑

  11.    );

  12.    for ($i = 0;$i < $n;$i++) {

  13.        $verify.= $code = substr($dict, mt_rand(0, $dictlen - 1) , 1);

  14.        imagettftext($image, 20, mt_rand(-15, 15) , ($i * 15) + 3, mt_rand(20, 25) , $colors[array_rand($colors) ], $fontfile, $code);

  15.    }

  16.    return $this;

  17. }

  18. //加入了50个噪点和一条干扰曲线

  19. public function addNoise($n = 50) {

  20.    $image = $this->image;

  21.    $color = imagecolorallocate($image, 0, 0, 0);

  22.    for ($i = 0;$i < $n;$i++) { //噪声点

  23.        imagesetpixel($image, mt_rand(0, $this->width) , mt_rand(0, $this->height) , $color);

  24.    }

  25.    return $this;

  26. }

  27. public function addLine($n = 1) {

  28.    $image = $this->image;

  29.    $color = imagecolorallocate($image, 0, 0, 0);

  30.    for ($i = 0;$i < $n;$i++) {

  31.        imagearc($image, rand(-10, $this->width + 10) , rand(-10, 0) , rand($this->width * 2 + 10, $this->width * 2 + 40) , rand($this->height, $this->height + 20) , 0, 360, $color);

  32.    }

  33.    return $this;

  34. }

汉字和带公式的验证码

  1. public function genHanzi($n = 2) {

  2.    $dict = "的一是在了不和有大这主中人上为们地个用工时要";

  3.    $dictlen = mb_strlen($dict, 'UTF-8');

  4.    $image = $this->image;

  5.    $fontfile = $this->sourcedir . $this->fonts[array_rand($this->fonts) ];

  6.    $color = imagecolorallocate($image, 0, 0, 0);

  7.    $verify = '';

  8.    for ($i = 0;$i < $n;$i++) {

  9.        $verify.= $word = mb_substr($dict, mt_rand(0, $dictlen - 1) , 1, 'UTF-8');

  10.        imagettftext($image, rand(18, 22) , rand(-20, 20) , 5 + $i * 25, 25, $color, $fontfile, $word);

  11.    }

  12.    $this->verify = $verify;

  13.    return $this;

  14. }

  15.  

  16. public function genFomula() {

  17.    $symbols = array(

  18.        '+' => '+','-' => '-','×' => '*','加' => '+','减' => '-','乘' => '*'

  19.    );

  20.    $numbers = array(

  21.        '0' => 0,'1' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'叁' => 3,'肆' => 4,'伍' => 5,'陆' => 6,'柒' => 7,'捌' => 8,'玖' => 9,

  22.    );

  23.    $image = $this->image;

  24.    $fontfile = $this->sourcedir . $this->fonts[array_rand($this->fonts) ];

  25.    $numidx1 = array_rand($numbers);

  26.    $num1 = $numbers[$numidx1];

  27.    $symbol = array_rand($symbols);

  28.    $color = imagecolorallocate($image, 0, 0, 0);

  29.    while (1) {

  30.        $numidx2 = array_rand($numbers);

  31.        $num2 = $numbers[$numidx2];

  32.        if ($symbols[$symbol] != '-' || $num2 <= $num1) { //减法结果不为负数

  33.            break;

  34.        }

  35.    }

  36.    eval("\$verify = " . "$num1" . $symbols[$symbol] . "$num2;");

  37.    $verify = intval($verify);

  38.    $codelist = array(

  39.        $numidx1,

  40.        $symbol,

  41.        $numidx2,

  42.        '='

  43.    );

  44.    foreach ($codelist as $i => $code) {

  45.        imagettftext($image, mt_rand(14, 16) , mt_rand(-15, 15) , ($i * 18) + 3, mt_rand(20, 25) , $color, $fontfile, $code);

  46.    }

  47.    return $this;

  48. }

对字体进行扭曲

  1. public function twist() {

  2.    $distImage = imagecreatetruecolor($this->width, $this->height);

  3.    imagecopy($distImage, $this->backimg, 0, 0, 0, 0, $this->width, $this->height);

  4.    for ($x = 0;$x < $this->width;$x++) {

  5.        for ($y = 0;$y < $this->height;$y++) {

  6.            $rgb = imagecolorat($this->image, $x, $y);

  7.            imagesetpixel($distImage, (int)($x + sin($y / $this->height * 2 * M_PI - M_PI * 0.1) * 4) , $y, $rgb);

  8.        }

  9.    }

  10.    $this->image = $distImage;

  11.    return $this;

  12. }

一个gif动态图

  1. public function genCodeAnimate($n = 4, $flags = 40) {

  2.    $dict = 'ABCDEFGHIJKLNMPQRSTUVWXYZ123456789';

  3.    $dictlen = strlen($dict);

  4.    $verify = '';

  5.    $fontfile = $this->sourcedir . $this->fonts[0];

  6.    $colors = array(

  7.        imagecolorallocate($this->image, 255, 0, 0) , //红

  8.        imagecolorallocate($this->image, 0, 0, 255) , //蓝

  9.        imagecolorallocate($this->image, 0, 0, 0) , //黑

  10.    );

  11.    $fontColors = array();

  12.    $fontSizes = array();

  13.    $gifs = array();

  14.    for ($i = 0;$i < $n;$i++) {

  15.        $verify.= substr($dict, mt_rand(0, $dictlen - 1) , 1);

  16.        $fontColors[$i] = $colors[array_rand($colors) ];

  17.        $fontSizes[$i] = rand(18, 22);

  18.    }

  19.    for ($f = 0;$f < $flags;$f++) {

  20.        $image = $this->imgClone($this->image);

  21.        $angle = - 15 + abs($f - $flags / 2) * 2; //角度

  22.        $y = 20 + abs($f - $flags / 2) * 0.5;

  23.        for ($i = 0;$i < $n;$i++) {

  24.            $code = substr($verify, $i, 1);

  25.            imagettftext($image, $fontSizes[$i], $angle, ($i * 15) - 20 + abs($f - $flags / 2) * 5, $y, $fontColors[$i], $fontfile, $code);

  26.        }

  27.        header("Content-type: image/gif");

  28.        imagegif($image);

  29.        imagedestroy($image);

  30.        $gifs[] = ob_get_contents();

  31.        ob_clean();

  32.    }

  33.    ob_start();

  34.    $gifEncoder = new GIFEncoder($gifs, 100, 0, 1, 0, 0, 1, 'bin');

  35.    header('Content-type: image/gif');

  36.    echo $gifEncoder->GetAnimation();

  37.    return $verify;

  38. }

完整代码

  1. <?PHP

  2. class VerifyPic {

  3.  

  4.    protected $image;

  5.    protected $width;

  6.    protected $height;

  7.    protected $backimg;

  8.    protected $fonts = array(

  9.        'STXINGKA.TTF'

  10.    );

  11.    protected $verify;

  12.    protected $sourcedir;

  13.  

  14.    public function __construct($width, $height) {

  15.        $this->sourcedir = './';

  16.        $image = imagecreatetruecolor($width, $height);

  17.        $backimg = imagecreatefrompng($this->sourcedir . '4.png');

  18.        imagecopy($image, $backimg, 0, 0, 0, 0, $width, $height);

  19.        $this->width = $width;

  20.        $this->height = $height;

  21.        $this->image = $image;

  22.        $this->backimg = $backimg;

  23.    }

  24.  

  25.    public function genCode($n = 4) {

  26.        $dict = 'ABCDEFGHIJKLNMPQRSTUVWXYZ123456789';

  27.        $dictlen = strlen($dict);

  28.        $image = $this->image;

  29.        $verify = '';

  30.        $fontfile = $this->sourcedir . $this->fonts[0];

  31.        $colors = array(

  32.            imagecolorallocate($image, 255, 0, 0) , //红

  33.            imagecolorallocate($image, 0, 0, 255) , //蓝

  34.            imagecolorallocate($image, 0, 0, 0) , //黑

  35.        );

  36.        for ($i = 0;$i < $n;$i++) {

  37.            $verify.= $code = substr($dict, mt_rand(0, $dictlen - 1) , 1);

  38.            imagettftext($image, 20, mt_rand(-15, 15) , ($i * 15) + 3, mt_rand(20, 25) , $colors[array_rand($colors) ], $fontfile, $code);

  39.        }

  40.        return $this;

  41.    }

  42.  

  43.    public function genHanzi($n = 2) {

  44.        $dict = "的一是在了不和有大这主中人上为们地个用工时要动国产以我到他会作来分生对于学下级就年阶义发成部民可出能方进同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批如应形想制心样干都向变关点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫康遵牧遭幅园腔订香肉弟屋敏恢忘衣孙龄岭骗休借丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩";

  45.        $dictlen = mb_strlen($dict, 'UTF-8');

  46.        $image = $this->image;

  47.        $fontfile = $this->sourcedir . $this->fonts[array_rand($this->fonts) ];

  48.        $color = imagecolorallocate($image, 0, 0, 0);

  49.        $verify = '';

  50.        for ($i = 0;$i < $n;$i++) {

  51.            $verify.= $word = mb_substr($dict, mt_rand(0, $dictlen - 1) , 1, 'UTF-8');

  52.            imagettftext($image, rand(18, 22) , rand(-20, 20) , 5 + $i * 25, 25, $color, $fontfile, $word);

  53.        }

  54.        $this->verify = $verify;

  55.        return $this;

  56.    }

  57.  

  58.    public function genFomula() {

  59.        $symbols = array(

  60.            '+' => '+',

  61.            '-' => '-',

  62.            '×' => '*',

  63.            '加' => '+',

  64.            '减' => '-',

  65.            '乘' => '*'

  66.        );

  67.        $numbers = array(

  68.            '0' => 0,

  69.            '1' => 1,

  70.            '2' => 2,

  71.            '3' => 3,

  72.            '4' => 4,

  73.            '5' => 5,

  74.            '6' => 6,

  75.            '7' => 7,

  76.            '8' => 8,

  77.            '9' => 9,

  78.            '零' => 0,

  79.            '四' => 4,

  80.            '五' => 5,

  81.            '六' => 6,

  82.            '七' => 7,

  83.            '八' => 8,

  84.            '九' => 9,

  85.            '壹' => 1,

  86.            '贰' => 2,

  87.            '叁' => 3,

  88.            '肆' => 4,

  89.            '伍' => 5,

  90.            '陆' => 6,

  91.            '柒' => 7,

  92.            '捌' => 8,

  93.            '玖' => 9,

  94.        );

  95.        $image = $this->image;

  96.        $fontfile = $this->sourcedir . $this->fonts[array_rand($this->fonts) ];

  97.        $numidx1 = array_rand($numbers);

  98.        $num1 = $numbers[$numidx1];

  99.        $symbol = array_rand($symbols);

  100.        $color = imagecolorallocate($image, 0, 0, 0);

  101.        while (1) {

  102.            $numidx2 = array_rand($numbers);

  103.            $num2 = $numbers[$numidx2];

  104.            if ($symbols[$symbol] != '-' || $num2 <= $num1) { //减法结果不为负数

  105.                break;

  106.            }

  107.        }

  108.        eval("\$verify = " . "$num1" . $symbols[$symbol] . "$num2;");

  109.        $verify = intval($verify);

  110.        $codelist = array(

  111.            $numidx1,

  112.            $symbol,

  113.            $numidx2,

  114.            '='

  115.        );

  116.        foreach ($codelist as $i => $code) {

  117.            imagettftext($image, mt_rand(14, 16) , mt_rand(-15, 15) , ($i * 18) + 3, mt_rand(20, 25) , $color, $fontfile, $code);

  118.        }

  119.        return $this;

  120.    }

  121.  

  122.    /**

  123.     * 动画

  124.     */

  125.    public function genCodeAnimate($n = 4, $flags = 40) {

  126.        $dict = 'ABCDEFGHIJKLNMPQRSTUVWXYZ123456789';

  127.        $dictlen = strlen($dict);

  128.        $verify = '';

  129.        $fontfile = $this->sourcedir . $this->fonts[0];

  130.        $colors = array(

  131.            imagecolorallocate($this->image, 255, 0, 0) , //红

  132.            imagecolorallocate($this->image, 0, 0, 255) , //蓝

  133.            imagecolorallocate($this->image, 0, 0, 0) , //黑

  134.        );

  135.        $fontColors = array();

  136.        $fontSizes = array();

  137.        $gifs = array();

  138.        for ($i = 0;$i < $n;$i++) {

  139.            $verify.= substr($dict, mt_rand(0, $dictlen - 1) , 1);

  140.            $fontColors[$i] = $colors[array_rand($colors) ];

  141.            $fontSizes[$i] = rand(18, 22);

  142.        }

  143.        for ($f = 0;$f < $flags;$f++) {

  144.            $image = $this->imgClone($this->image);

  145.            $angle = - 15 + abs($f - $flags / 2) * 2; //角度

  146.            $y = 20 + abs($f - $flags / 2) * 0.5;

  147.            for ($i = 0;$i < $n;$i++) {

  148.                $code = substr($verify, $i, 1);

  149.                imagettftext($image, $fontSizes[$i], $angle, ($i * 15) - 20 + abs($f - $flags / 2) * 5, $y, $fontColors[$i], $fontfile, $code);

  150.            }

  151.            header("Content-type: image/gif");

  152.            imagegif($image);

  153.            imagedestroy($image);

  154.            $gifs[] = ob_get_contents();

  155.            ob_clean();

  156.        }

  157.        ob_start();

  158.        $gifEncoder = new GIFEncoder($gifs, 100, 0, 1, 0, 0, 1, 'bin');

  159.        header('Content-type: image/gif');

  160.        echo $gifEncoder->GetAnimation();

  161.        return $verify;

  162.    }

  163.  

  164.    public function flush() {

  165.        header('Content-type: image/png');

  166.        imagepng($this->image);

  167.        imagedestroy($this->image);

  168.        imagedestroy($this->backimg);

  169.        return $this->verify;

  170.    }

  171.  

  172.    /**

  173.     * 扭曲

  174.     */

  175.    public function twist() {

  176.        $distImage = imagecreatetruecolor($this->width, $this->height);

  177.        imagecopy($distImage, $this->backimg, 0, 0, 0, 0, $this->width, $this->height);

  178.        for ($x = 0;$x < $this->width;$x++) {

  179.            for ($y = 0;$y < $this->height;$y++) {

  180.                $rgb = imagecolorat($this->image, $x, $y);

  181.                imagesetpixel($distImage, (int)($x + sin($y / $this->height * 2 * M_PI - M_PI * 0.1) * 4) , $y, $rgb);

  182.            }

  183.        }

  184.        $this->image = $distImage;

  185.        return $this;

  186.    }

  187.  

  188.    /**

  189.     * 加噪点

  190.     */

  191.    public function addNoise($n = 50) {

  192.        $image = $this->image;

  193.        $color = imagecolorallocate($image, 0, 0, 0);

  194.        for ($i = 0;$i < $n;$i++) { //噪声点

  195.            imagesetpixel($image, mt_rand(0, $this->width) , mt_rand(0, $this->height) , $color);

  196.        }

  197.        return $this;

  198.    }

  199.  

  200.    /**

  201.     * 加噪音线

  202.     */

  203.    public function addLine($n = 1) {

  204.        $image = $this->image;

  205.        $color = imagecolorallocate($image, 0, 0, 0);

  206.        for ($i = 0;$i < $n;$i++) {

  207.            imagearc($image, rand(-10, $this->width + 10) , rand(-10, 0) , rand($this->width * 2 + 10, $this->width * 2 + 40) , rand($this->height, $this->height + 20) , 0, 360, $color);

  208.        }

  209.        return $this;

  210.    }

  211.  

  212.    public function imgClone($image) {

  213.        $copy = imagecreatetruecolor($this->width, $this->height);

  214.        imagecopy($copy, $image, 0, 0, 0, 0, $this->width, $this->height);

  215.        return $copy;

  216.    }

  217.  

  218. }

  219.  

  220. Class GIFEncoder {

  221.  

  222.    var $GIF = "GIF89a"; /* GIF header 6 bytes        */

  223.    var $VER = "GIFEncoder V2.06"; /* Encoder version                */

  224.    var $BUF = Array();

  225.    var $LOP = 0;

  226.    var $DIS = 2;

  227.    var $COL = - 1;

  228.    var $IMG = - 1;

  229.    var $ERR = Array(

  230.        'ERR00' => "Does not supported function for only one image!",

  231.        'ERR01' => "Source is not a GIF image!",

  232.        'ERR02' => "Unintelligible flag ",

  233.        'ERR03' => "Could not make animation from animated GIF source",

  234.    );

  235.  

  236.    /*

  237.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  238.      ::

  239.      ::        GIFEncoder...

  240.      ::

  241.    */

  242.  

  243.    function GIFEncoder($GIF_src, $GIF_dly, $GIF_lop, $GIF_dis, $GIF_red, $GIF_grn, $GIF_blu, $GIF_mod) {

  244.        if (!is_array($GIF_src) && !is_array($GIF_tim)) {

  245.            printf("%s: %s", $this->VER, $this->ERR['ERR00']);

  246.            exit(0);

  247.        }

  248.        $this->LOP = ($GIF_lop > - 1) ? $GIF_lop : 0;

  249.        $this->DIS = ($GIF_dis > - 1) ? (($GIF_dis < 3) ? $GIF_dis : 3) : 2;

  250.        $this->COL = ($GIF_red > - 1 && $GIF_grn > - 1 && $GIF_blu > - 1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;

  251.  

  252.        for ($i = 0;$i < count($GIF_src);$i++) {

  253.            if (strToLower($GIF_mod) == "url") {

  254.                $this->BUF[] = fread(fopen($GIF_src[$i], "rb") , filesize($GIF_src[$i]));

  255.            } else if (strToLower($GIF_mod) == "bin") {

  256.                $this->BUF[] = $GIF_src[$i];

  257.            } else {

  258.                printf("%s: %s ( %s )!", $this->VER, $this->ERR['ERR02'], $GIF_mod);

  259.                exit(0);

  260.            }

  261.            if (substr($this->BUF[$i], 0, 6) != "GIF87a" && substr($this->BUF[$i], 0, 6) != "GIF89a") {

  262.                printf("%s: %d %s", $this->VER, $i, $this->ERR['ERR01']);

  263.                exit(0);

  264.            }

  265.            for ($j = (13 + 3 * (2 << (ord($this->BUF[$i] {

  266.                10

  267.            }) & 0x07))) , $k = TRUE;$k;$j++) {

  268.                switch ($this->BUF[$i] {

  269.                        $j

  270.                }) {

  271.                    case "!":

  272.                        if ((substr($this->BUF[$i], ($j + 3) , 8)) == "netscape") {

  273.                            printf("%s: %s ( %s source )!", $this->VER, $this->ERR['ERR03'], ($i + 1));

  274.                            exit(0);

  275.                        }

  276.                    break;

  277.                    case ";":

  278.                        $k = FALSE;

  279.                    break;

  280.                }

  281.            }

  282.        }

  283.        GIFEncoder::GIFAddHeader();

  284.        for ($i = 0;$i < count($this->BUF);$i++) {

  285.            GIFEncoder::GIFAddFrames($i, $GIF_dly[$i]);

  286.        }

  287.        GIFEncoder::GIFAddFooter();

  288.    }

  289.  

  290.    /*

  291.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  292.      ::

  293.      ::        GIFAddHeader...

  294.      ::

  295.    */

  296.  

  297.    function GIFAddHeader() {

  298.        $cmap = 0;

  299.  

  300.        if (ord($this->BUF[0] {

  301.            10

  302.        }) & 0x80) {

  303.            $cmap = 3 * (2 << (ord($this->BUF[0] {

  304.                10

  305.            }) & 0x07));

  306.  

  307.            $this->GIF.= substr($this->BUF[0], 6, 7);

  308.            $this->GIF.= substr($this->BUF[0], 13, $cmap);

  309.            $this->GIF.= "!\377\13netscape2.0\3\1" . GIFEncoder::GIFWord($this->LOP) . "\0";

  310.        }

  311.    }

  312.  

  313.    /*

  314.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  315.      ::

  316.      ::        GIFAddFrames...

  317.      ::

  318.    */

  319.  

  320.    function GIFAddFrames($i, $d) {

  321.  

  322.        $Locals_str = 13 + 3 * (2 << (ord($this->BUF[$i] {

  323.            10

  324.        }) & 0x07));

  325.  

  326.        $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;

  327.        $Locals_tmp = substr($this->BUF[$i], $Locals_str, $Locals_end);

  328.  

  329.        $Global_len = 2 << (ord($this->BUF[0] {

  330.            10

  331.        }) & 0x07);

  332.        $Locals_len = 2 << (ord($this->BUF[$i] {

  333.            10

  334.        }) & 0x07);

  335.  

  336.        $Global_rgb = substr($this->BUF[0], 13, 3 * (2 << (ord($this->BUF[0] {

  337.            10

  338.        }) & 0x07)));

  339.        $Locals_rgb = substr($this->BUF[$i], 13, 3 * (2 << (ord($this->BUF[$i] {

  340.            10

  341.        }) & 0x07)));

  342.  

  343.        $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";

  344.  

  345.        if ($this->COL > - 1 && ord($this->BUF[$i] {

  346.            10

  347.        }) & 0x80) {

  348.            for ($j = 0;$j < (2 << (ord($this->BUF[$i] {

  349.                10

  350.            }) & 0x07));$j++) {

  351.                if (ord($Locals_rgb{3 * $j + 0}) == ($this->COL >> 0) & 0xFF && ord($Locals_rgb{3 * $j + 1}) == ($this->COL >> 8) & 0xFF && ord($Locals_rgb{3 * $j + 2}) == ($this->COL >> 16) & 0xFF) {

  352.                    $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";

  353.                    break;

  354.                }

  355.            }

  356.        }

  357.        switch ($Locals_tmp{0}) {

  358.            case "!":

  359.                $Locals_img = substr($Locals_tmp, 8, 10);

  360.                $Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);

  361.            break;

  362.            case ",":

  363.                $Locals_img = substr($Locals_tmp, 0, 10);

  364.                $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);

  365.            break;

  366.        }

  367.        if (ord($this->BUF[$i] {

  368.            10

  369.        }) & 0x80 && $this->IMG > - 1) {

  370.            if ($Global_len == $Locals_len) {

  371.                if (GIFEncoder::GIFBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {

  372.                    $this->GIF.= ($Locals_ext . $Locals_img . $Locals_tmp);

  373.                } else {

  374.                    $byte = ord($Locals_img{9});

  375.                    $byte|= 0x80;

  376.                    $byte&= 0xF8;

  377.                    $byte|= (ord($this->BUF[0] {

  378.                        10

  379.                    }) & 0x07);

  380.                    $Locals_img{9} = chr($byte);

  381.                    $this->GIF.= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);

  382.                }

  383.            } else {

  384.                $byte = ord($Locals_img{9});

  385.                $byte|= 0x80;

  386.                $byte&= 0xF8;

  387.                $byte|= (ord($this->BUF[$i] {

  388.                    10

  389.                }) & 0x07);

  390.                $Locals_img{9} = chr($byte);

  391.                $this->GIF.= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);

  392.            }

  393.        } else {

  394.            $this->GIF.= ($Locals_ext . $Locals_img . $Locals_tmp);

  395.        }

  396.        $this->IMG = 1;

  397.    }

  398.  

  399.    /*

  400.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  401.      ::

  402.      ::        GIFAddFooter...

  403.      ::

  404.    */

  405.  

  406.    function GIFAddFooter() {

  407.        $this->GIF.= ";";

  408.    }

  409.  

  410.    /*

  411.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  412.      ::

  413.      ::        GIFBlockCompare...

  414.      ::

  415.    */

  416.  

  417.    function GIFBlockCompare($GlobalBlock, $LocalBlock, $Len) {

  418.  

  419.        for ($i = 0;$i < $Len;$i++) {

  420.            if ($GlobalBlock{3 * $i + 0} != $LocalBlock{3 * $i + 0} || $GlobalBlock{3 * $i + 1} != $LocalBlock{3 * $i + 1} || $GlobalBlock{3 * $i + 2} != $LocalBlock{3 * $i + 2}) {

  421.                return (0);

  422.            }

  423.        }

  424.  

  425.        return (1);

  426.    }

  427.  

  428.    /*

  429.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  430.      ::

  431.      ::        GIFWord...

  432.      ::

  433.    */

  434.  

  435.    function GIFWord($int) {

  436.  

  437.        return (chr($int & 0xFF) . chr(($int >> 8) & 0xFF));

  438.    }

  439.  

  440.    /*

  441.      :::::::::::::::::::::::::::::::::::::::::::::::::::

  442.      ::

  443.      ::        GetAnimation...

  444.      ::

  445.    */

  446.  

  447.    function GetAnimation() {

  448.        return ($this->GIF);

  449.    }

  450.  

  451. }

  452. // https://jingxin.me/blog/blog/2013/01/13/PHP-yan-zheng-ma/

  453. $vp = new VerifyPic(70, 30);

  454. // $vp->genHanzi()->twist()->flush();

  455. // $vp->genFomula()->twist()->flush();

  456. $vp->genCodeAnimate()->twist()->flush();

 

大佬总结

以上是大佬教程为你收集整理的PHP 验证码全部内容,希望文章能够帮你解决PHP 验证码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: