当前位置: 首页 > news >正文

php绘图添加水印,文字使用imagick库的操作

1.创建画布或者使用图片创建背景图

  /*** 创建或加载背景画布*/static function backgroundImage($backgroundImagePath = ''){if (!file_exists($backgroundImagePath) || !is_readable($backgroundImagePath)) {// 下载远程图片并本地缓存$imageData = file_get_contents($backgroundImagePath);$path = 'uploads/qr_images/backgroundImagePath.png';file_put_contents($path, $imageData);$backgroundImagePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $path;}try {self::$canvas = new Imagick($backgroundImagePath);} catch (\Exception $e) {throw new \Exception("无法加载背景图: " . $e->getMessage());}}

2.添加文字到画布上,需要获取字体,然后就是设置了颜色和字体的位置

    /*** 添加文字到画布上*/static function text($text, $fontSize, $x, $y, $color, $statusX = '', $statusY = ''){$fontPath = $_SERVER['DOCUMENT_ROOT'] . '/assets/fonts/SourceHanSansK-Regular.ttf';// 创建绘图对象$draw = new ImagickDraw();$draw->setFont($fontPath);$draw->setFontSize($fontSize);// 设置颜色$colorStr = sprintf('rgb(%d,%d,%d)', $color[0], $color[1], $color[2]);$draw->setFillColor(new ImagickPixel($colorStr));// 对齐方式处理$width = self::getTextWidth($text, $fontSize, $fontPath);$height = self::getTextHeight($text, $fontSize, $fontPath);switch ($statusX) {case 'center':$x = (self::$canvas->getImageWidth() - $width) / 2;break;case 'left':$x = 0;break;case 'right':$x = self::$canvas->getImageWidth() - $width;break;}switch ($statusY) {case 'center':$y = (self::$canvas->getImageHeight() - $height) / 2 + $height;break;case 'top':$y = $height;break;case 'bottom':$y = self::$canvas->getImageHeight() - $height;break;}// 绘制文本self::$canvas->annotateImage($draw, $x, $y, 0, $text);}
/*** 获取文字宽度*/
static function getTextWidth($text, $fontSize, $fontPath)
{$draw = new ImagickDraw();$draw->setFont($fontPath);$draw->setFontSize($fontSize);$metrics = self::$canvas->queryFontMetrics($draw, $text);return $metrics['textWidth'];
}/*** 获取文字高度*/
static function getTextHeight($text, $fontSize, $fontPath)
{$draw = new ImagickDraw();$draw->setFont($fontPath);$draw->setFontSize($fontSize);$metrics = self::$canvas->queryFontMetrics($draw, $text);return $metrics['textHeight'];
}

3.添加水印,其中设置位置

    /*** 添加图片水印*/static function image($imagePath, $x, $y, $statusX = '', $statusY = ''){if (!file_exists($imagePath) || !is_readable($imagePath)) {$imageData = file_get_contents($imagePath);$path = 'uploads/qr_images/headimgurl.png';file_put_contents($path, $imageData);$imagePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $path;try {$image = new Imagick($imagePath);} catch (\Exception $e) {throw new \Exception("无法加载水印图: " . $e->getMessage());}$imageWidth = $image->getImageWidth();$imageHeight = $image->getImageHeight();switch ($statusX) {case 'center':$x = (self::$canvas->getImageWidth() - $imageWidth) / 2;break;case 'left':$x = 0;break;case 'right':$x = self::$canvas->getImageWidth() - $imageWidth;break;}switch ($statusY) {case 'center':$y = (self::$canvas->getImageHeight() - $imageHeight) / 2;break;case 'top':$y = 0;break;case 'bottom':$y = self::$canvas->getImageHeight() - $imageHeight;break;}self::$canvas->compositeImage($image, Imagick::COMPOSITE_DEFAULT, $x, $y);}

4.保存图像

/*** 保存图像*/
static function save($outPath = '')
{$outPath = $outPath ?: 'uploads/qr_images/';if (!file_exists($outPath)) {mkdir($outPath, 0755, true);}$fileName = 'watermark_' . md5(time() . random_int(1000, 9999)) . '.png';$filePath = $outPath . $fileName;try {self::$canvas->writeImage($filePath);self::$canvas->clear();self::$canvas->destroy();} catch (\Exception $e) {return Tools::set_fail("保存失败:" . $e->getMessage());}return $filePath;
}

5.调用绘图添加水印和文字
这里的image是控制器,上方的方法都是写在其中的
在这里插入图片描述

http://www.dtcms.com/a/271139.html

相关文章:

  • Docker 高级管理-容器通信技术与数据持久化
  • C语言结构体对齐
  • SpringCloud系列 - xxl-job 分布式任务调度 (七)
  • 链表和数组和列表的区别
  • 力扣网编程150题:加油站(贪心解法)
  • Origin将Y偏移图升级为双Y轴3D瀑布图
  • SAP-ABAP:SAP中‘SELECT...WHERE...IN’语句IN的用法详解
  • 想要抢早期筹码?FourMeme专区批量交易教学
  • Cadence模块复用
  • SQL 视图与事务知识点详解及练习题
  • 基于Spring Boot+Vue的巴彦淖尔旅游网站(AI问答、腾讯地图API、WebSocket及时通讯、支付宝沙盒支付)
  • 等价矩阵和等价向量组
  • JavaScript基础篇——第五章 对象(最终篇)
  • 深度学习模型在C++平台的部署
  • 优化 FLUX.1 Kontext 以进行低精度量化的图像编辑
  • Flowable17错误事件---------------持续更新中
  • ali linux 安装libreoffice
  • Linux 系统 docker 部署 Dify
  • Airtest 的 Poco 框架中,offspring()
  • 【网络】Linux 内核优化实战 - net.ipv4.tcp_ecn
  • Protable 问题记录
  • 【RK3568+PG2L50H开发板实验例程】FPGA部分 | ROM、RAM、FIFO 的使用
  • 使用DDR4控制器实现多通道数据读写(十九)
  • 通过Tcl脚本命令:set_param labtools.auto_update_hardware 0
  • 闲庭信步使用图像验证平台加速FPGA的开发:第六课——测试图案的FPGA实现
  • 闲庭信步使用图像验证平台加速FPGA的开发:第七课——获取RAW图像
  • RAM带宽计算及分析
  • 双esp8266-01s间TCP通讯
  • 云服务器域名可以设置多少二级域名
  • 为什么Spring中推荐使用构造函数而不是@Autowired字段注入