PHP 验证码生成器:简单易用示例代码
以下是一个简单的 PHP 验证码生成器示例代码:
<?php
session_start();
$width = 100;
$height = 40;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$char_length = strlen($characters);
$font_size = 20;
$image = imagecreatetruecolor($width, $height);
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $background_color);
$code = '';
for ($i = 0; $i < 6; $i++) {
$char = $characters[rand(0, $char_length - 1)];
$code .= $char;
$x = ($width / 6) * $i + 10;
$y = rand($height / 2 - 10, $height / 2 + 10);
imagettftext($image, $font_size, rand(-15, 15), $x, $y, $text_color, 'arial.ttf', $char);
}
$_SESSION['captcha'] = $code;
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
该代码将生成一个 100x40 像素的验证码图像,包含 6 个随机字符,使用 arial 字体呈现。验证码图像将以 PNG 格式输出,并存储在会话中以供验证。请注意,此示例需要在服务器上安装 FreeType 库以支持 TrueType 字体。
原文地址: http://www.cveoy.top/t/topic/jM1R 著作权归作者所有。请勿转载和采集!