search
Homephp教程php手册日常整理PHP中简单的图形处理(经典)

本篇文章是小编日常整理的php图形处理,如何使用GD2函数在照片上添加文字,使用GD2函数创建一个4位的验证码。对本文内容感兴趣的朋友可以了解了解

1.加载GD库

  GD库是一个开放的动态创建图像、源代码公开的函数库,可以从官方网站处下载。目前,GD库支持GIF、PNG、JPEG、WBMP和XBM等多种图像格式,,用于对图像的处理。

  GD库在PHP 5中是默认安装的,但要激活GD库,必须修改php.ini文件。将该文件中的“;extension=php_gd2.dll”选项前的分号“;”删除,保存修改后的文件并重新启动Apache服务器即可生效。

2.创建一个简单的图像

  使用GD2函数库可以实现各种图形图像的处理。创建画布是使用GD2函数库来创建图像的第一步,无论创建什么样的图像,首先都需要创建一个画布,其他操作都将在这个画布上完成。在GD2函数库中创建画布,可以通过imagecreate()函数实现。

  使用imagecreate()函数创建一个宽度为200像素,高度为60像素的画布,并设置画布颜色RGB(225,66,159),最后输出一个GIF格式的图像,代码如下:

3.使用GD2函数在照片上添加文字

  PHP中的GD库支持中文,但必须要以UTF-8格式的参数来进行传递,如果使用imageString()函数直接绘制中文字符串就会显示乱码,这是因为GD2对中文只能接收UTF-8编码格式,并且默认使用英文字体,所以要输出中文字符串,必须对中文字符串进行转码,并设置中文字符使用的字体。否则,输出的只能是乱码。

使用imageTTFText()函数将文字“这是一个测试”输出到图像中,代码如下:

4.PHP生成验证码

创建一个checks.php文件在文件中使用GD2函数创建一个4位的验证码,并将生成的验证码保存到session中:

创建一个用户登录的表单并调用checks.php在表单中输出图像的内容:

alert('验证码不能为空');window.location.href='index.php';"; } if($checks==$_SESSION[check_checks]){ echo "<script> alert('用户登录成功!');window.location.href='index.php';</script>"; }else{ echo "<script> alert('您输入的验证码不正确!');window.location.href='index.php';</script>"; } } ?>

rand
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.