PHP 技巧 * SVG 保存为图片(分享图生成)


Posted in PHP onApril 02, 2021

一、准备

  1. 准备 svg 文件:这一步骤通常由设计完成,而案例是通过 SVG 在线编辑器 生成的;
  2. 确保你的环境支持使用 Imagick 扩展;
  3. 如果是Linux服务器,可能需要安装必要字体,否则中文可能无法正常显示;

二、过程:

test.svg

<svg width="500" height="300" xmlns="http://www.w3.org/2000/svg">
 <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
 <g>
  <title>background</title>
  <rect fill="#fff" id="canvas_background" height="302" width="502" y="-1" x="-1"/>
  <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
   <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
  </g>
 </g>
 <g>
  <title>Layer 1</title>
  <text stroke="#000" transform="matrix(1.2316112832302093,0,0,1.6103224566703835,-34.94758717722778,-66.61220433762628) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_1" y="119.89815" x="101.417837" stroke-width="0" fill="#000000">ATONG</text>
  <rect id="svg_2" height="4" width="414" y="139.8375" x="85.25" stroke-width="1.5" stroke="#000" fill="#000000"/>
  <text xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_3" y="179.8375" x="124.5" stroke-width="0" stroke="#000" fill="#000000">Tel: 8888-8888-8888</text>
  <text xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_4" y="209.8375" x="98.5" stroke-width="0" stroke="#000" fill="#000000">email: 991220405@qq.com</text>
  <rect stroke="#000" id="svg_5" height="31.999999" width="33.999999" y="0.75" x="0.75" stroke-width="1.5" fill="#000000"/>
  <rect id="svg_6" height="26" width="26" y="37.8375" x="16.5" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" stroke="#000" fill="#000000"/>
  <rect id="svg_7" height="24" width="25" y="11.8375" x="42.5" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" stroke="#000" fill="#000000"/>
  <rect id="svg_8" height="12" width="12" y="42.8375" x="50.5" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" stroke="#000" fill="#000000"/>
  <text stroke="#000" transform="matrix(0.7079451420430161,0,0,0.43161633610725403,65.21588988354182,166.3232696466148) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_9" y="299.75941" x="163.544496" fill-opacity="null" stroke-opacity="null" stroke-width="0" fill="#000000">https://wp.kder.top</text>
 </g>
</svg>

 test.php:

<?php

$svg = file_get_contents("./test.svg");
// todo:: 替换svg的内容
// $svg = str_replace("ATONG","YOUR NAME",$svg);

$im = new \Imagick();

$im->readImageBlob($svg);
$im->setImageFormat("jpeg");
$im->writeImage('./test.jpg');

$im->clear();
$im->destroy();

echo "<img src='./test.jpg'>";

 

PHP 相关文章推荐
一个php Mysql类 可以参考学习熟悉下
Jun 21 PHP
Php 构造函数construct的前下划线是双的_
Dec 08 PHP
php网上商城购物车设计代码分享
Feb 15 PHP
基于header的一些常用指令详解
Jun 06 PHP
关于file_get_contents返回为空或函数不可用的解决方案
Jun 24 PHP
php foreach正序倒序输出示例代码
Jul 01 PHP
php调用KyotoTycoon简单实例
Apr 02 PHP
php轻松实现文件上传功能
Mar 03 PHP
ThinkPHP 整合Bootstrap Ajax分页样式
Dec 23 PHP
PHP使用OB缓存实现静态化功能示例
Mar 23 PHP
PHP面向对象程序设计__tostring()和__invoke()用法分析
Jun 12 PHP
为你的 Laravel 验证器加上多验证场景的实现
Apr 07 PHP
PHP解决高并发问题
php引用传递
Apr 01 #PHP
PhpSpreadsheet中文文档 | Spreadsheet操作教程实例
Apr 01 #PHP
PHP判断是否是json字符串
Apr 01 #PHP
php TP5框架生成二维码链接
Apr 01 #PHP
php随机生成验证码,php随机生成数字,php随机生成数字加字母!
Apr 01 #PHP
phpQuery解析HTML乱码问题(补充官网未列出的乱码解决方案)
Apr 01 #PHP
You might like
详解PHP显示MySQL数据的三种方法
2008/06/05 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
php实现的简单多进程服务器类完整示例
2020/02/01 PHP
精心挑选的15款优秀jQuery 本特效插件和教程
2012/08/06 Javascript
百度移动版的url编码解码示例
2014/04/29 Javascript
javascript实现验证IP地址等相关信息代码
2015/05/10 Javascript
javascript实现Table排序的方法
2015/05/15 Javascript
如何在Linux上安装Node.js
2016/04/01 Javascript
JS防止网页被嵌入iframe框架的方法分析
2016/09/13 Javascript
使用AngularJS2中的指令实现按钮的切换效果
2017/03/27 Javascript
JavaScript之promise_动力节点Java学院整理
2017/07/03 Javascript
vue嵌套路由与404重定向实现方法分析
2018/05/04 Javascript
使用jQuery给Table动态增加行、清空table的方法
2018/09/05 jQuery
jQuery 获取除某指定对象外的其他对象 ( :not() 与.not())
2018/10/10 jQuery
JS中数据结构之栈
2019/01/01 Javascript
基于vue实现图片验证码倒计时60s功能
2019/12/10 Javascript
python-str,list,set间的转换实例
2018/06/27 Python
python 3.6.5 安装配置方法图文教程
2018/09/18 Python
Python数据库封装实现代码示例解析
2020/09/05 Python
css3实现多个元素依次显示效果
2017/12/12 HTML / CSS
CSS3 中filter(滤镜)属性使用详解
2020/04/07 HTML / CSS
用HTML5.0制作网页的教程
2010/05/30 HTML / CSS
美国男士和女士奢侈品折扣手表购物网站:Certified Watch Store
2018/06/13 全球购物
美国班级戒指、帽子和礼服、毕业产品、年鉴:Balfour
2018/11/01 全球购物
是否可以从一个static方法内部发出对非static方法的调用?
2014/08/18 面试题
英语专业学生的自我评价
2013/12/30 职场文书
运动会入场式解说词
2014/02/18 职场文书
在校大学生的职业生涯规划书
2014/03/14 职场文书
学生会竞聘书范文
2014/03/31 职场文书
电子商务助理求职自荐信
2014/04/10 职场文书
群众路线个人对照检查材料
2014/09/23 职场文书
房屋分割离婚协议书范本
2014/12/01 职场文书
入党转正介绍人意见
2015/06/03 职场文书
靠谱的活动总结
2019/04/16 职场文书
VUE使用draggable实现组件拖拽
2022/04/06 Vue.js
MySQL的存储函数与存储过程的区别解析
2022/04/08 MySQL