search

PHP构造验证码

代码如下:

<?phpheader (&#39;Content-type:image/jpeg&#39;);$width=120;$height=40;$element=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;,&#39;h&#39;,&#39;i&#39;,&#39;j&#39;,&#39;k&#39;,&#39;m&#39;,&#39;n&#39;,&#39;o&#39;,&#39;p&#39;,&#39;q&#39;,&#39;r&#39;,&#39;s&#39;,&#39;t&#39;,&#39;u&#39;,&#39;v&#39;,&#39;w&#39;,&#39;x&#39;,&#39;y&#39;,&#39;z&#39;);$string=&#39;&#39;;for ($i=0;$i<5;$i++){<span style="white-space:pre">	$string.=$element[rand(0,count($element)-1)];}$img=imagecreatetruecolor($width, $height);//设置图片大小$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//随机产生背景色$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//随机产生背景色$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));imagefill($img,0,0,$colorBg);//设置图片背景色imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);//构建矩形边框for($i=0;$i	imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));//画一个单一的像素}for($i=0;$i	imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));<span style="white-space:pre">	</span>//前两个为起点坐标,后两个为终点坐标,起点控制在左半边,终点控制在右半边}//imagestring($img,5,0,0,'abcd',$colorString);imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);//绘制文字,可以选择丰富的字体的方法//说明:array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )imagejpeg($img);imagedestroy($img);

重点介绍imagettftext()方法:

array imagettftext ( resource$image ,float$size ,float$angle ,int$x ,int$y ,int$color ,string$fontfile ,string$text )

image
图像资源。见 imagecreatetruecolor()
size
字体大小。根据 GD 版本不同,应该以像素大小指定(GD1)或点大小(GD2)。
angle
角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
x
xy 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0。
y
Y 坐标。它设定了字体基线的位置,不是字符的最底端。
color
颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见 imagecolorallocate()
fontfile
是想要使用的 TrueType 字体的路径。 根据 PHP 所使用的 GD 库的不同,fontfile 没有以/ 开头时则.ttf 将被加到文件名之后并且会在库定义字体路径中尝试搜索该文件名。当使用的 GD 库版本低于 2.0.18 时,一个空格字符 而不是分号将被用来作为不同字体文件的“路径分隔符”。不小心使用了此特性将会导致一条警告信息:Warning: Could not find/open font。对受影响的版本来说唯一解决方案就是将字体移动到不包含空格的路径中去。

很多情况下字体都放在脚本的同一个目录下。下面的小技巧可以减轻包含的问题。

<span style="color:rgb(0,0,187)"><?php <br></span><span style="color:rgb(255,128,0)">// Set the enviroment variable for GD<br></span><span style="color:rgb(0,0,187)">putenv</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(221,0,0)">'GDFONTPATH=' </span><span style="color:rgb(0,119,0)">. </span><span style="color:rgb(0,0,187)">realpath</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(221,0,0)">'.'</span><span style="color:rgb(0,119,0)">));<br><br></span><span style="color:rgb(255,128,0)">// Name the font to be used (note the lack of the .ttf extension)<br></span><span style="color:rgb(0,0,187)">$font </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(221,0,0)">'SomeFont'</span><span style="color:rgb(0,119,0)">;<br></span><span style="color:rgb(0,0,187)">?></span>

text
文本字符串。 可以包含十进制数字化字符表示(形式为:€)来访问字体中超过位置 127 的字符。UTF-8 编码的字符串可以直接传递。如果字符串中使用的某个字符不被字体支持,一个空心矩形将替换该字符。

imagettftext() 返回一个含有 8 个单元的数组表示了文本外框的四个角,顺序为坐下角,右下角,右上角,左上角。这些点是相对于文本的而和角度无关,因此“左上角”指的是以水平方向看文字时其左上角。

Example #1 imagettftext() 例子

本例中的脚本将生成一个白色的 400x30 像素 PNG 图像,其中有黑色(带灰色阴影)Arial 字体写的“Testing...”。

<span style="color:rgb(0,0,187)"><?php <br></span><span style="color:rgb(255,128,0)">// Set the content-type<br></span><span style="color:rgb(0,0,187)">header</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(221,0,0)">"Content-type: image/png"</span><span style="color:rgb(0,119,0)">);<br><br></span><span style="color:rgb(255,128,0)">// Create the image<br></span><span style="color:rgb(0,0,187)">$im </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(0,0,187)">imagecreatetruecolor</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">400</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">30</span><span style="color:rgb(0,119,0)">);<br><br></span><span style="color:rgb(255,128,0)">// Create some colors<br></span><span style="color:rgb(0,0,187)">$white </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(0,0,187)">imagecolorallocate</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">255</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">255</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">255</span><span style="color:rgb(0,119,0)">);<br></span><span style="color:rgb(0,0,187)">$grey </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(0,0,187)">imagecolorallocate</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">128</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">128</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">128</span><span style="color:rgb(0,119,0)">);<br></span><span style="color:rgb(0,0,187)">$black </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(0,0,187)">imagecolorallocate</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">);<br></span><span style="color:rgb(0,0,187)">imagefilledrectangle</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">399</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">29</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$white</span><span style="color:rgb(0,119,0)">);<br><br></span><span style="color:rgb(255,128,0)">// The text to draw<br></span><span style="color:rgb(0,0,187)">$text </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(221,0,0)">'Testing...'</span><span style="color:rgb(0,119,0)">;<br></span><span style="color:rgb(255,128,0)">// Replace path by your own font path<br></span><span style="color:rgb(0,0,187)">$font </span><span style="color:rgb(0,119,0)">= </span><span style="color:rgb(221,0,0)">'arial.ttf'</span><span style="color:rgb(0,119,0)">;<br><br></span><span style="color:rgb(255,128,0)">// Add some shadow to the text<br></span><span style="color:rgb(0,0,187)">imagettftext</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">20</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">11</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">21</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$grey</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$font</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$text</span><span style="color:rgb(0,119,0)">);<br><br></span><span style="color:rgb(255,128,0)">// Add the text<br></span><span style="color:rgb(0,0,187)">imagettftext</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">20</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">0</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">10</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">20</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$black</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$font</span><span style="color:rgb(0,119,0)">, </span><span style="color:rgb(0,0,187)">$text</span><span style="color:rgb(0,119,0)">);<br><br></span><span style="color:rgb(255,128,0)">// Using imagepng() results in clearer text compared with imagejpeg()<br></span><span style="color:rgb(0,0,187)">imagepng</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">);<br></span><span style="color:rgb(0,0,187)">imagedestroy</span><span style="color:rgb(0,119,0)">(</span><span style="color:rgb(0,0,187)">$im</span><span style="color:rgb(0,119,0)">);<br></span><span style="color:rgb(0,0,187)">?></span>
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment