PHP结构验证码
PHP构造验证码
代码如下:
<?phpheader ('Content-type:image/jpeg');$width=120;$height=40;$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');$string='';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()方法:
- image
- 图像资源。见 imagecreatetruecolor()。
- size
- 字体大小。根据 GD 版本不同,应该以像素大小指定(GD1)或点大小(GD2)。
- angle
- 角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
- x
- 由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
