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执行sql语句的写法
Mar 10 PHP
php加水印的代码(支持半透明透明打水印,支持png透明背景)
Jan 17 PHP
php上传apk后自动提取apk包信息的使用(示例下载)
Apr 26 PHP
探讨PHP使用eAccelerator的API开发详解
Jun 09 PHP
PHP实现的简易版图片相似度比较
Jan 07 PHP
简单谈谈php延迟静态绑定
Jan 26 PHP
Smarty简单生成表单元素的方法示例
May 23 PHP
Yii2增删改查之查询 where参数详细介绍
Aug 08 PHP
详解php中serialize()和unserialize()函数
Jul 08 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
Jun 16 PHP
详解PHP版本兼容之openssl调用参数
Jul 25 PHP
PHP树形结构tree类用法示例
Feb 01 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作的文本留言本的例子(四)
2006/10/09 PHP
PHPStorm+XDebug进行调试图文教程
2016/06/13 PHP
PHP递归删除多维数组中的某个值
2017/04/17 PHP
PHP正则匹配到2个字符串之间的内容方法
2018/12/24 PHP
获取任意Html元素与body之间的偏移距离 offsetTop、offsetLeft (For:IE5+ FF1 )[
2006/12/22 Javascript
用js实现层随着内容大小动态渐变改变 推荐
2009/12/19 Javascript
基于jQuery实现点击同时更改两个iframe的网址
2010/07/01 Javascript
jQuery中end()方法用法实例
2015/01/08 Javascript
javascript实现全角半角检测的方法
2015/07/23 Javascript
JavaScript检测并限制复选框选中个数的方法
2015/08/12 Javascript
javaScript中的原型解析【推荐】
2016/05/05 Javascript
JavaScript重载函数实例剖析
2016/05/13 Javascript
详解JavaScript中的属性和特性
2016/12/08 Javascript
用node-webkit把web应用打包成桌面应用(windows环境)
2018/02/01 Javascript
微信小程序定位当前城市的方法
2018/07/19 Javascript
解决vue-cli webpack打包后加载资源的路径问题
2018/09/25 Javascript
在webstorm中配置less的方法详解
2020/09/25 Javascript
Vue中父子组件的值传递与方法传递
2020/09/28 Javascript
python转换摩斯密码示例
2014/02/16 Python
Python基于回溯法子集树模板实现图的遍历功能示例
2017/09/05 Python
python3库numpy数组属性的查看方法
2018/04/17 Python
python连接mysql数据库并读取数据的实现
2020/09/25 Python
在pycharm中使用pipenv创建虚拟环境和安装django的详细教程
2020/11/30 Python
CSS3实现跳动的动画效果
2016/09/12 HTML / CSS
HTML5是什么 HTML5是什么意思 HTML5简介
2012/10/26 HTML / CSS
详解canvas绘制多张图的排列顺序问题
2019/01/21 HTML / CSS
New Balance美国官网:运动鞋和健身服装
2017/04/11 全球购物
俄罗斯隐形眼镜和眼镜在线商店:Cronos
2020/06/02 全球购物
分厂厂长岗位职责
2013/12/29 职场文书
学校庆元旦歌咏比赛主持词
2014/03/18 职场文书
加强作风建设工作总结
2014/10/23 职场文书
写给孩子的新学期寄语
2015/02/27 职场文书
淘宝文案策划岗位职责
2015/04/14 职场文书
2015年营业员工作总结
2015/04/23 职场文书
Canvas三种动态画圆实现方法说明(小结)
2021/04/16 Javascript
oracle数据库去除重复数据
2022/05/20 Oracle