PHP中GD库的使用

由于我要用到php的验证码

php 复制代码
<?php
session_start();

// 生成验证码
$random_code = substr(md5(uniqid(mt_rand(), true)), 0, 6);

// 将验证码保存到 session 中
$_SESSION['captcha'] = $random_code;

// 创建图片
$font = 6;
$image_width = 100;
$image_height = 40;

// 创建图像
$image = imagecreatetruecolor($image_width, $image_height);

// 设置背景色
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background_color);

// 设置验证码颜色
$text_color = imagecolorallocate($image, 0, 0, 0);

// 在图像上绘制验证码
imagestring($image, $font, 15, 10, $random_code, $text_color);

// 输出图像
header("Content-Type: image/png");
imagepng($image);

// 释放内存
imagedestroy($image);
?>

php.ini 配置文件中开启 GD 扩展即可使用

相关推荐
BingoGo13 小时前
2025 年 PHP 常见面试题整理以及对应答案和代码示例
后端·php
Bruce1232 天前
web专题之php代审(二)
php
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
BingoGo2 天前
PHP-FPM 深度调优指南 告别 502 错误,让你的 PHP 应用飞起来
后端·php
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt