使用php生成图片

可以用这方法生成图片 水印 字体可以在资源绑定下载,如果字体路径不对,则不会输出文字图片

public function generateImage(text,id)

{

header("Cache-Control: no-cache, must-revalidate");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// 设置内容类型为PNG

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

// 创建一个120x40的图像

$width = 200;

$height = 40;

image = imagecreatetruecolor(width, $height);

// 设置背景色

backgroundColor = imagecolorallocate(image, 255, 255, 255);

imagefill(image, 0, 0, backgroundColor);

// 设置文字颜色

textColor = imagecolorallocate(image, 217, 217, 242);

// 设置字体文件路径

$path = ROOT_PATH;

fontFile = path.'public/static/admin/fonts/iconfont/simhei.ttf'; // 替换为实际字体文件路径' //注意直接跟图片地址就好

// 生成文字

// $text = "dfsd代发收到";

// 绘制文

imagettftext(image, 14, 0, 10, 30, textColor, fontFile, text);

imagePath = path.'public/upload/shuiyin/'.$id.'.png'; // 替换为你的保存路径

// 输出图像

imagepng(image,imagePath); //保存图片

// imagepng($image);//输出到浏览器

// 销毁图像资源

imagedestroy($image);

}

相关推荐
Chester_19997 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲8 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员9 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导9 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan9 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251499 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
大衛說10 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
_祝你今天愉快10 小时前
从驱动来分析服务的添加过程
android
可乐鸡翅yeah_11 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线
2601_9622035112 小时前
Java进阶07集合(续)
java·开发语言