学习使用php实现汉字验证码

学习使用php实现汉字验证码

php 复制代码
<?php
//开启session ,方便验证
session_start();
//创建背景画布
$image = imagecreatetruecolor(200, 60);
$background = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background);

//创建背景画布
for ($i = 0; $i < 300; $i++) {
    $pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240));
    $pixX = rand(10, 190);
    $pixY = rand(5, 55);
    imagesetpixel($image, $pixX, $pixY, $pixColor);
}

//4条水平线
for ($i = 0; $i < 5; $i++) {
    $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = 0;
    $lineX2 = 300;
    $lineY1 = ($i + 1) * 12;
    $lineY2 = ($i + 1) * 12;
    imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}
//10条垂直线
for ($i = 0; $i < 30; $i++) {
    $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = ($i + 1) * 10;
    $lineX2 = ($i + 1) * 10;
    $lineY1 = 0;
    $lineY2 = 60;
    imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}

//汉字库
$text_str = '君|不|见|黄|河|之|水|天|上|来|奔|流|到|海|不|复|回|君|不|见|高|堂|明|镜|悲|白|发|朝|如|青|丝|暮|成|雪|人|生|得|意|须|尽|欢|莫|使|金|樽|空|对|月|天|生|我|材|必|有|用|千|金|散|尽|还|复|来|烹|羊|宰|牛|且|为|乐|会|须|一|饮|三|百|杯|岑|夫|子|丹|丘|生|将|进|酒|杯|莫|停|与|君|歌|一|曲|请|君|为|我|倾|耳|听|钟|鼓|馔|玉|不|足|贵|但|愿|长|醉|不|愿|醒|古|来|圣|贤|皆|寂|寞|惟|有|饮|者|留|其|名|陈|王|昔|时|宴|平|乐|斗|酒|十|千|恣|欢|谑|主|人|何|为|言|少|钱|径|须|沽|取|对|君|酌|五|花|马|千|金|裘|呼|儿|将|出|换|美|酒|与|尔|同|销|万|古|愁';

$text = explode('|', $text_str);

$text_count = bcsub(count($text), 1);

//创建背景画布
$text_new = '';
for ($i = 0; $i < 4; $i++) {
    $textColor = imagecolorallocate($image, rand(20, 100), rand(20, 100), rand(20, 100));
    $textX = $i * 50 + 10;
    $textY = rand(40, 60);
    //随机生成数字
    $rand_i = rand(0, $text_count);
    $text_new .= $text[$rand_i];
    imagettftext($image, 30, rand(20, 50), $textX, $textY, $textColor, "simhei.ttf", $text[$rand_i]);
}
//存入session
$_SESSION['text_new'] = $text_new;

header("Content-Type:image/png");
imagepng($image);

?>

效果图

相关推荐
人生游戏牛马NPC1号2 分钟前
学习 Flutter (一)
android·学习·flutter
Aczone281 小时前
嵌入式 数据结构学习 (六) 树、哈希表与内核链表
数据结构·学习·算法
想成为大佬的每一天1 小时前
Linux驱动学习day22(interrupt子系统)
学习
Chef_Chen1 小时前
从0开始学习R语言--Day43--Wald检验
学习
真的想上岸啊1 小时前
学习C++、QT---21(QT中QFile库的QFile读取文件、写入文件的讲解)
c++·qt·学习
之歆2 小时前
Python-正则表达式-信息提取-滑动窗口-数据分发-文件加载及分析器-浏览器分析-学习笔记
python·学习·正则表达式
rui锐rui2 小时前
大数据学习7:Azkaban调度器
学习
北风toto4 小时前
python学习DataFrame数据结构
数据结构·python·学习
qq_386322695 小时前
华为网路设备学习-26(BGP协议 一)
学习
DKPT6 小时前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式