Home  >  Article  >  Backend Development  >  How to implement the PHP image verification code generation function

How to implement the PHP image verification code generation function

WBOY
WBOYOriginal
2023-06-15 20:58:071311browse

Implementation method of PHP image verification code generation function

Image verification code is a security verification code displayed in the form of an image, which can effectively prevent various network attacks, such as brute force cracking and automated script attacks wait. As a popular Web programming language, PHP has built-in image processing functions and libraries that can easily implement image verification code functions. This article will introduce a PHP-based image verification code generation function implementation method.

First of all, we need to clarify the elements of the image verification code: canvas, verification code characters, interference lines, interference points, etc. Next, we will implement these elements one by one to implement a complete PHP image verification code generation function.

  1. Create Canvas

To create a canvas, we can use PHP's imagecreatetruecolor() function. This function creates a truecolor image of the specified size. The following is the calling format of this function:

resource imagecreatetruecolor ( int $width , int $height )

Among them, the width and height parameters are the image width and height respectively. Here is a code example for creating a canvas:

// 创建画布
$image_width = 140; // 画布宽度
$image_height = 50; // 画布高度
$image = imagecreatetruecolor($image_width, $image_height);
  1. Set the background color

To set the background color, we can use PHP's imagefill() function. This function tints the image, filling the entire image with the specified color. The following is the calling format of this function:

bool imagefill ( resource $image , int $x , int $y , int $color )

Among them, the image parameter is the image resource, x and y The parameters are the coordinates of the dyeing starting point, and the color parameter is the color, which is created using the imagecolorallocate() function.

The following is a code example for setting the background color:

// 设置背景颜色
$bg_color = imagecolorallocate($image, 255, 255, 255); // 白色 
imagefill($image, 0, 0, $bg_color); // 填充背景为白色
  1. Add interference lines and interference points

In order to improve the security of the verification code, we need Add interference lines and interference points. PHP's basic image processing functions can implement these functions.

To add interference lines, you can use PHP's imageline() function. This function draws a straight line. The following is the calling format of this function:

bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

among them, image The parameters are image resources, (x1, y1) and (x2, y2) are the starting and ending coordinates of the line segment, and the color parameter is the color.

To add interference points, you can use PHP's imagesetpixel() function. This function draws a pixel in the image. The following is the calling format of this function:

bool imagesetpixel ( resource $image , int $x , int $y , int $color )

Among them, the image parameter is the image resource, (x, y) is the coordinate of the pixel point, and the color parameter is the color.

The following is a code example for adding interference lines and interference points:

// 添加干扰线
for ($i = 0; $i < 5; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imageline($image, rand(0, $image_width), rand(0, $image_height),
        rand(0, $image_width), rand(0, $image_height), $line_color);
}
// 添加干扰点
for ($i = 0; $i < 50; $i++) {
    $pixel_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imagesetpixel($image, rand(0, $image_width), rand(0, $image_height), $pixel_color);
}
  1. Add verification code characters

The last step, we need to add verification code characters . Verification code characters can be numbers, letters, symbols, etc. For convenience, we only consider numerical verification codes here.

To add verification code characters, you can use PHP's imagestring() function or imagettftext() function.

When using the imagestring() function, the function outputs a string directly on the image. The following is the calling format of this function:

bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )

where, image The parameters are image resources, font is the font size, x and y are the starting point coordinates of the string, string is the output string, and color is the color.

When using the imagettftext() function, this function outputs the specified string to the specified canvas according to the specified font, size and arbitrary rotation angle. The following is the calling format of this function:

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

Among them, the image parameter is the image resource, size is the font size, angle is the rotation angle, (x, y) is the coordinates of the starting point of the string, color is the color, fontfile is the font file, and text is the output String.

The following is a code example for generating verification code characters:

// 添加验证码字符
$code = '';
for ($i = 0; $i < 4; $i++) {
    $digital_color = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); // 随机颜色
    $digital = rand(0, 9); // 随机数字
    $code .= $digital;
    // 按一定的规则将字符画布输出
    imagettftext($image, 20, rand(-30, 30), $i * 30 + 10, rand($image_height/2, $image_height-20),
        $digital_color, './arial.ttf', $digital);
}

Finally, the verification code characters are stored in the session for subsequent comparison and verification.

$_SESSION['code'] = $code;

Full code example:

session_start();

header("Content-type: image/png");

$image_width = 140; // 画布宽度
$image_height = 50; // 画布高度

$image = imagecreatetruecolor($image_width, $image_height); // 创建画布

$bg_color = imagecolorallocate($image, 255, 255, 255); // 白色 
imagefill($image, 0, 0, $bg_color); // 填充背景为白色

// 添加干扰线
for ($i = 0; $i < 5; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imageline($image, rand(0, $image_width), rand(0, $image_height),
        rand(0, $image_width), rand(0, $image_height), $line_color);
}

// 添加干扰点
for ($i = 0; $i < 50; $i++) {
    $pixel_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imagesetpixel($image, rand(0, $image_width), rand(0, $image_height), $pixel_color);
}

// 添加验证码字符
$code = '';
for ($i = 0; $i < 4; $i++) {
    $digital_color = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); // 随机颜色
    $digital = rand(0, 9); // 随机数字
    $code .= $digital;
    // 按一定的规则将字符画布输出
    imagettftext($image, 20, rand(-30, 30), $i * 30 + 10, rand($image_height/2, $image_height-20),
        $digital_color, './arial.ttf', $digital);
}

$_SESSION['code'] = $code; // 将验证码存入session

imagepng($image); // 输出验证码图片

imagedestroy($image); // 销毁图像资源

Summary

The PHP image verification code generation function is a very practical technology to prevent network attacks. This article introduces the implementation method of the PHP image verification code generation function, including creating a canvas, setting the background color, adding interference lines and interference points, adding verification code characters, etc. Through the combined use of these elements, high-quality, secure image verification codes can be generated.

The above is the detailed content of How to implement the PHP image verification code generation function. For more information, please follow other related articles on the PHP Chinese website!

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