Home > Backend Development > PHP Tutorial > Simple example of php color verification code

Simple example of php color verification code

WBOY
Release: 2016-07-25 09:00:23
Original
1044 people have browsed it
Use PHP to implement a colorful verification code. The code is very simple and easy to learn. Friends in need, come and take a look.

Colored verification code example:

<?php
/**
 * php 彩色验证码
 * site bbs.it-home.org
*/
header("Content-type: image/png,charset='utf-8'");
$im = imagecreatetruecolor(400, 30);
//白色
$white = imagecolorallocate($im, 255, 255, 255);
//红色
$red = imagecolorallocate($im, 255, 0, 0);
//黑色
$black=imagecolorallocate($im, 0, 0, 0);
//绿色
$green=imagecolorallocate($im, 0, 255, 0);
//蓝色
$blue=imagecolorallocate($im, 0, 0, 255);
$color_arr=array($green,$blue,$red);
$color=array_rand($color_arr);
$text = '我靠这验证码太变态啦';
$textlen=iconv_strlen($text,'utf-8');//计算字符串长度
//随机截取两个字符,变色显示
$p1=rand(1,$textlen)-1;
while(($p2=rand(1,$textlen)-1)==$p1);
$w1=iconv_substr($text,$p1,1,'utf-8');
$w2=iconv_substr($text,$p1,1,'utf-8');
//字体文件
$font = 'simkai.ttf';
imagefilledrectangle($im, 0, 0, 399, 29, $white);
for($i=0;$i<$textlen;$i++)
{
if($i==$p1||$i==$p2)
{
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $color_arr[$color], $font, iconv_substr($text,$i,1,'utf-8'));
}
else
{
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $black, $font, iconv_substr($text,$i,1,'utf-8'));
}
}
imagepng($im);
imagedestroy($im);
?>
Copy after login

The above is the implementation code of the color verification code. It is recommended that you test it on your own computer to see if it works well. Articles you may be interested in: php verification code simple function code (with renderings) Share a php verification code class and calling example Three example code sharing of php verification code A packaging class for PHP verification code Example code of php custom size verification code Example code for php to generate distorted and rotated verification code images PHP imitation QQ verification code implementation code Example of using php verification code function php5 verification code class (simple and practical) Example of php verification code (GD library generates verification code) PHP implementation code for real-time refresh when clicking on the verification code php image verification code example How to implement PHP verification code refresh and partial refresh Example of php GD library generating verification code Example of php generating verification code php random verification code php generates random verification code (picture and text) A relatively stable PHP login system verification code Use php to generate verification code with snowflake background



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template