search
HomeBackend DevelopmentPHP TutorialPHP5 GD library generates graphic verification code (Chinese characters)_PHP tutorial
PHP5 GD library generates graphic verification code (Chinese characters)_PHP tutorialJul 20, 2016 am 11:14 AM
php5WritefunctionusepicturegraphicspicturecharacterLibrarydesignationChinese charactergeneratecodeverify

1. Use the GD library function to generate an image and write the specified characters on the image.
imagecreatetruecolor creates a new true color image.
imagecolorallocate assigns a color (palette) to an image.
imagestring draws characters
imageline draws lines
imagesetpixel hits pixels
2, and outputs pictures
imagejpeg($img);
PHP implementation process, the code is commented in detail, and I won’t explain too much here
verify .php

<?<span php
</span><span //</span><span 1.qi启用gd库GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。
// 在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。
//by www.jbxue.com 2013-7-30</span>
<span session_start</span><span ();
</span><span //</span><span  把GBK编码的字符串转换成UTF-8字符串,第一个参数之所以写GBK,是因为本php文件在主机中存储的编码是GBK编码
// UTF-8编码浏览器普遍支持,通用性强,这里就转换成UTF-8</span>
<span $str</span> = <span iconv</span>("GBK", "utf-8", "芸芸众生绿水青山名胜古迹敞开心胸便会云蒸霞蔚快乐将永远伴随着你"<span );
</span><span if</span>(!<span is_string</span>(<span $str</span>) || !mb_check_encoding(<span $str</span>,"utf-8"<span ))
{
</span><span exit</span>("不是字符串或者不是utf-8"<span );
}
</span><span $zhongwenku_size</span><span ;
</span><span //</span><span  按UTF-8编码方式获取字符串的长度</span>
<span $zhongwenku_size</span> = mb_strlen(<span $str</span>,"UTF-8"<span );
</span><span //</span><span  把上述字符导入数组中</span>
<span $zhongwenku</span> = <span array</span><span ();
</span><span for</span>( <span $i</span>=0; <span $i</span><<span $zhongwenku_size</span>; <span $i</span>++<span )
{
</span><span $zhongwenku</span>[<span $i</span>] = mb_substr(<span $str</span>, <span $i</span>,1,"UTF-8"<span );
}
</span><span $result</span> = ""<span ;
</span><span //</span><span  图片上要写入的四个字符</span>
<span for</span>(<span $i</span>=0; <span $i</span><4; <span $i</span>++<span )
{
</span><span switch</span> (<span rand</span>(0, 1<span ))
{
</span><span case</span> 0:
<span $result</span>.=<span $zhongwenku</span>[<span rand</span>(0, <span $zhongwenku_size</span>-1<span )];
</span><span break</span><span ;
</span><span case</span> 1:
<span $result</span>.=<span dechex</span>(<span rand</span>(0,15<span ));
</span><span break</span><span ;
}

}
</span><span $_SESSION</span>["check"] = <span $result</span><span ;

</span><span //</span><span  创建一个真彩图片 宽100,高30</span>
<span $img</span> = imagecreatetruecolor(100, 30<span );
</span><span //</span><span  分配背景颜色</span>
<span $bg</span> = imagecolorallocate(<span $img</span>, 0, 0, 0<span );
</span><span //</span><span  分配文字颜色</span>
<span $te</span> = imagecolorallocate(<span $img</span>, 255,255,255<span );
</span><span //</span><span  在图片上写字符串
//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);
// 在图片上根据载入字体可以写出特殊字体</span>
imagettftext(<span $img</span>, 13, <span rand</span>(2, 9), 20 ,20, <span $te</span>, "MSYH.TTF",<span $result</span><span );
</span><span $_SESSION</span>["check"] = <span $result</span><span ;
</span><span for</span>(<span $i</span>=0; <span $i</span><3; <span $i</span>++<span )
{
</span><span //</span><span  $t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));
// 画线</span>
imageline(<span $img</span>, 0, <span rand</span>(0, 20), <span rand</span>(70,100), <span rand</span>(0, 20), <span $te</span><span );
}
</span><span $t</span> = imagecolorallocate(<span $img</span>, <span rand</span>(0, 255),<span rand</span>(0, 255),<span rand</span>(0, 255<span ));
</span><span //</span><span  为图片添加噪点</span>
<span for</span>(<span $i</span>=0; <span $i</span><200; <span $i</span>++<span )
{
imagesetpixel(</span><span $img</span>, <span rand</span>(1, 100), <span rand</span>(1, 30), <span $t</span><span );
}
</span><span //</span><span  发送http头信息 指定本次发送的是image中的jpeg</span>
<span header</span>("Content-type: image/jpeg"<span );
</span><span //</span><span  输出jpeg图片至浏览器</span>
imagejpeg(<span $img</span><span );
</span>?>

<?<span php
</span><span //</span><span 提交页面
//by www.jbxue.com</span>
<span session_start</span><span ();
</span><span if</span>(@<span $_POST</span>['check'<span ])
{
</span><span if</span>(<span $_POST</span>["check"] == <span $_SESSION</span>["check"<span ])
{
</span><span echo</span>" 恭喜你!验证码输入正确!"<span ;
}</span><span else</span><span {
</span><span echo</span>" 不好意思验证码输入错误"<span ;
}
}
</span>?>
<form action="" method="post">
<img  src="/static/imghwm/default1.png"  data-src="verify.php"  class="lazy"/ alt="PHP5 GD library generates graphic verification code (Chinese characters)_PHP tutorial" > </br>
<input type="text" name="check"></br>
<input type="submit" value="submit"></br>
</form>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440246.htmlTechArticle1, use the GD library function to generate an image, and write the specified characters on the image imagecreatetruecolor Create a new true color image imagecolorallocate as An image is assigned colors (palette) images...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

五大热门Go语言库汇总:开发必备利器五大热门Go语言库汇总:开发必备利器Feb 22, 2024 pm 02:33 PM

五大热门Go语言库汇总:开发必备利器,需要具体代码示例Go语言自从诞生以来,受到了广泛的关注和应用。作为一门新兴的高效、简洁的编程语言,Go的快速发展离不开丰富的开源库的支持。本文将介绍五大热门的Go语言库,这些库在Go开发中扮演了至关重要的角色,为开发者提供了强大的功能和便捷的开发体验。同时,为了更好地理解这些库的用途和功能,我们会结合具体的代码示例进行讲

学习Python,常用的这22个库怎能不掌握?学习Python,常用的这22个库怎能不掌握?Apr 12, 2023 am 10:25 AM

如今全球各个行业内 Python 的使用状况怎么样呢?这个问题就是我写这篇文章的初衷。我找出了22个最常用的 Python 包,希望能给你一些启发。首先我列出了最近一年内 PyPI 上下载量最高的 Python 包。我们来看看这些包的作用,它们的之间的关系,以及为什么会如此流行。1、Urllib38.93亿次下载Urllib3 是 Python 的 HTTP 客户端,它提供了许多 Python 标准库没有的功能。 线程安全 连接池 客户端 SSL/TLS 验证 使用 multipart 编码进行

PHP8.0中的国际化库PHP8.0中的国际化库May 14, 2023 pm 05:51 PM

PHP8.0中的国际化库:UnicodeCLDR和Intl扩展随着全球化的进程,开发跨语言、跨地域的应用程序变得越来越普遍。国际化是实现这一目标的重要组成部分。在PHP8.0中,引入了UnicodeCLDR和Intl扩展,这两个组件都为开发者提供了更好的国际化支持。UnicodeCLDRUnicodeCLDR(CommonLocaleDat

轻松掌握Pillow库安装方法:指南分享轻松掌握Pillow库安装方法:指南分享Jan 17, 2024 am 08:56 AM

Pillow库是Python中一个非常强大的图像处理库,它基于PythonImagingLibrary(PIL)发展而来,并在其基础上进行了优化和扩展。Pillow库提供了丰富的图像处理功能,可以处理各种类型的图像文件,并进行图像的编辑、合并、滤镜处理等操作。本文将为大家提供一个Pillow库的安装指南,帮助你轻松掌握这个强大的图像处理工具。一、安装P

XML 解析的 Java 库对比:寻找最佳解决方案XML 解析的 Java 库对比:寻找最佳解决方案Mar 09, 2024 am 09:10 AM

简介XML(可扩展标记语言)是一种用于存储和传输数据的流行格式。在Java中解析XML是许多应用程序的一个必要任务,从数据交换到文档处理。为了有效地解析XML,开发人员可以使用各种Java库。本文将比较一些最流行的XML解析库,重点关注它们的特性、功能和性能,以帮助开发人员做出明智的选择。DOM(文档对象模型)解析库JavaXMLDOMAPI:由oracle提供的标准DOM实现。它提供了一个对象模型,允许开发人员访问和操作XML文档。DocumentBuilderFactoryfactory=D

php5如何改80端口php5如何改80端口Jul 24, 2023 pm 04:57 PM

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

PyCharm 添加库的方法和技巧大揭秘PyCharm 添加库的方法和技巧大揭秘Feb 21, 2024 pm 10:09 PM

PyCharm添加库的方法和技巧大揭秘PyCharm是一款功能强大的Python集成开发环境,为Python开发者提供了丰富的功能和工具。在使用PyCharm进行开发的过程中,添加库是一个常见的需求。本文将详细介绍PyCharm添加库的方法和技巧,包括通过PyCharm自带的功能和使用pip工具安装库。一、通过PyCharm自

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version