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 修改、增加xml结点属性的实现代码
Oct 22 PHP
php中并发读写文件冲突的解决方案
Oct 25 PHP
php汉字转拼音的示例
Feb 27 PHP
php计算几分钟前、几小时前、几天前的几个函数、类分享
Apr 09 PHP
php获取mysql字段名称和其它信息的例子
Apr 14 PHP
PHP获取时间排除周六、周日的两个方法
Jun 30 PHP
php通过array_push()函数添加多个变量到数组末尾的方法
Mar 18 PHP
自定义min版smarty模板引擎MinSmarty.class.php文件及用法
May 20 PHP
深入了解PHP中的Array数组和foreach
Nov 06 PHP
PHP中检索字符串的方法分析【strstr与substr_count方法】
Feb 17 PHP
详解Yaf框架PHPUnit集成测试方法
Dec 27 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
Feb 08 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代码审计比较有意思的例子
2014/05/07 PHP
php实现压缩多个CSS与JS文件的方法
2014/11/11 PHP
PHP根据图片色界在不同位置加水印的方法
2015/07/01 PHP
Thinkphp5+uploadify实现的文件上传功能示例
2018/05/26 PHP
js 分页全选或反选标识实现代码
2011/08/09 Javascript
Javascript表格翻页效果的具体实现
2013/10/05 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
js分页代码分享
2014/04/28 Javascript
jQuery学习笔记之jQuery.extend(),jQuery.fn.extend()分析
2014/06/09 Javascript
html文本框提示效果的示例代码
2014/06/28 Javascript
js实现仿爱微网两级导航菜单效果代码
2015/08/31 Javascript
浅析JS原型继承与类的继承
2016/04/07 Javascript
详解ECMAScript6入门--Class对象
2017/04/27 Javascript
Bootstrap Table 双击、单击行获取该行及全表内容
2018/08/31 Javascript
JS Web Flex弹性盒子模型代码实例
2020/03/10 Javascript
Vue微信公众号网页分享的示例代码
2020/05/28 Javascript
[49:40]2018DOTA2亚洲邀请赛小组赛 A组加赛 TNC vs Newbee
2018/04/03 DOTA
Python实现控制台进度条功能
2016/01/04 Python
python 实现自动远程登陆scp文件实例代码
2017/03/13 Python
Python 通过调用接口获取公交信息的实例
2018/12/17 Python
一行python实现树形结构的方法
2019/08/09 Python
简单易懂Pytorch实战实例VGG深度网络
2019/08/27 Python
Pytorch 实现权重初始化
2019/12/31 Python
Python中sys模块功能与用法实例详解
2020/02/26 Python
分享CSS3中必须要知道的10个顶级命令
2012/04/26 HTML / CSS
J.Crew官网:美国知名休闲服装品牌
2017/05/19 全球购物
Famous Footwear加拿大:美国多品牌运动休闲鞋店
2018/12/05 全球购物
请说出你所知道的线程同步的方法
2013/04/19 面试题
造价工程师个人求职信
2013/09/21 职场文书
毕业生求职自荐信怎么写
2014/01/08 职场文书
本科毕业生专业自荐书范文
2014/02/05 职场文书
政府绩效管理实施方案
2014/05/04 职场文书
读群众路线的心得体会
2014/09/03 职场文书
python使用openpyxl库读写Excel表格的方法(增删改查操作)
2021/05/02 Python
基于PyQt5制作一个群发邮件工具
2022/04/08 Python
Golang 实现WebSockets
2022/04/24 Golang