类似于下面这样
1.利用GD库函数生成图片,并在图片上写指定字符
imagecreatetruecolor 新建一个真彩色图像
imagecolorallocate 为一幅图像分配颜色(调色板) <">
Home > Backend Development > PHP Tutorial > GD library in PHP5 generates graphic verification code (with Chinese characters)_PHP tutorial

GD library in PHP5 generates graphic verification code (with Chinese characters)_PHP tutorial

WBOY
Release: 2016-07-21 15:00:01
Original
818 people have browsed it

Use the GD library in PHP5 to generate a graphical verification code

GD library in PHP5 generates graphic verification code (with Chinese characters)_PHP tutorial

similar to the following
1. Use the GD library function to generate a picture and write the specified characters on the picture
imagecreatetruecolor creates a new true color image
imagecolorallocate assigns a color (palette) to an image
imagestring draws characters
imageline draws lines
imagesetpixel plays pixels
2. Output picture
imagejpeg($img);
PHP implementation process, the comments in the code are detailed, no further explanation will be given here
verify.php

Copy code The code is as follows:

//1.qi Enable the gd library. The GD library provides a series of APIs for processing images. You can use the GD library to process images or generate images. .
// On websites, the GD library is usually used to generate thumbnails or to add watermarks to images or to generate reports on website data.
session_start();
// Convert the GBK encoded string into a UTF-8 string. The first parameter is written GBK because the encoding of this php file stored in the host is GBK encoding
// UTF-8 encoding is generally supported by browsers and has strong versatility. Let’s convert it to UTF-8 here
$str = iconv("GBK", "utf-8", "Everyone is open to green waters, green mountains, scenic spots and historic sites Your heart will be filled with joy and happiness will always be with you");
if(!is_string($str) || !mb_check_encoding($str,"utf-8"))
{
exit("No The string may not be utf-8");
}
$zhongwenku_size;
// Get the length of the string in UTF-8 encoding
$zhongwenku_size = mb_strlen($str,"UTF- 8");
// Import the above characters into the array
$zhongwenku = array();
for( $i=0; $i<$zhongwenku_size; $i++)
{
$zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8");
}
$result = "";
// The four characters to be written on the picture characters
for($i=0; $i<4; $i++)
{
switch (rand(0, 1))
{
case 0:
$ result.=$zhongwenku[rand(0, $zhongwenku_size-1)];
break;
case 1:
$result.=dechex(rand(0,15));
break;
}

}
$_SESSION["check"] = $result;

// Create a true color image with a width of 100 and a height of 30
$img = imagecreatetruecolor (100, 30);
// Assign background color
$bg = imagecolorallocate($img, 0, 0, 0);
// Assign text color
$te = imagecolorallocate($img , 255,255,255);
//Write string on the image
//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $ te);
// Special fonts can be written on the image according to the loaded font
imagettftext($img, 13, rand(2, 9), 20,20, $te, "MSYH.TTF", $result);
$_SESSION["check"] = $result;
for($i=0; $i<3; $i++)
{
// $t = imagecolorallocate( $img, rand(0, 255),rand(0, 255),rand(0, 255));
// Draw line
imageline($img, 0, rand(0, 20), rand (70,100), rand(0, 20), $te);
}
$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255) );
// Add noise to the image
for($i=0; $i<200; $i++)
{
imagesetpixel($img, rand(1, 100), rand( 1, 30), $t);
}
// Send http header information to specify that the jpeg in the image is sent this time
header("Content-type: image/jpeg");
// Output jpeg images to the browser
imagejpeg($img);
?>

submit.php
Copy code The code is as follows:

session_start();
if(@$_POST['check'])
{
if($_POST["check"] == $_SESSION["check"])
{
echo" Congratulations! The verification code is entered correctly! ";
}else{
echo" Sorry, the verification code was entered incorrectly";
}
}
?>





< ;input type="submit" value="submit">



MRYH.ttf in the code is the font you want to set.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328100.htmlTechArticleUse the GD library in PHP5 to generate a graphical verification code similar to the following 1. Use the GD library function to generate the image, and Write the specified characters on the image imagecreatetruecolor Create a new true color image image...
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