PHP变色验证码代码示例
以下是实现变色验证码的PHP代码示例: ```php <?php // 设置内容类型为图像 header('Content-Type: image/png'); // 创建画布 $width = 100; $height = 30; $image = imagecreatetruecolor($width, $height); // 定义颜色 $bg_color = imagecolorallocate($image, 255, 255, 255); // 白色背景 $text_color = imagecolorallocate($image, 0, 0, 0); // 黑色文字 // 填充背景 imagefill($image, 0, 0, $bg_color); // 生成随机验证码字符串 $code = ''; for ($i = 0; $i < 4; $i++) { $char = rand(0, 9); $code .= $char; } // 在图像上绘制验证码 imagestring($image, 5, 10, 5, $code, $text_color); // 输出图像 imagepng($image); // 释放内存 imagedestroy($image); ?> ```