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 相关文章推荐
Excel数据导入Mysql数据库的实现代码
Jun 05 PHP
常用的PHP数据库操作方法(MYSQL版)
Jun 08 PHP
php检测网页是否被百度收录的函数代码
Oct 09 PHP
php小技巧之过滤ascii控制字符
May 14 PHP
php中限制ip段访问、禁止ip提交表单的代码分享
Aug 22 PHP
PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
Nov 20 PHP
php实现RSA加密类实例
Mar 26 PHP
PHP使用pear自带的mail类库发邮件的方法
Jul 08 PHP
微信JSSDK分享功能图文实例详解
Apr 08 PHP
php实现映射操作实例详解
Oct 02 PHP
详解php反序列化
Jun 10 PHP
phpstorm激活码2020附使用详细教程
Sep 25 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
dhtmlxTree目录树增加右键菜单以及拖拽排序的实现方法
2013/04/26 PHP
php魔术函数__call()用法实例分析
2015/02/13 PHP
php 无限级分类 获取顶级分类ID
2016/03/13 PHP
PHP-FPM和Nginx的通信机制详解
2019/02/01 PHP
php使用goto实现自动重启swoole、reactphp、workerman服务的代码
2020/04/13 PHP
Javascript 实现TreeView CheckBox全选效果
2010/01/11 Javascript
JS Loading功能的简单实现
2013/11/29 Javascript
获取鼠标在div中的相对位置的实现代码
2013/12/30 Javascript
Vue.JS入门教程之事件监听
2016/12/01 Javascript
微信小程序实现瀑布流布局与无限加载的方法详解
2017/05/12 Javascript
详解Vue组件之间的数据通信实例
2017/06/17 Javascript
vue.js中v-on:textInput无法执行事件问题的解决过程
2017/07/12 Javascript
vue实现留言板todolist功能
2017/08/16 Javascript
最新Javascript程序员面试试题和解题方法
2017/11/23 Javascript
使用vue2实现购物车和地址选配功能
2018/03/29 Javascript
js删除数组中某几项的方法总结
2019/01/16 Javascript
Vue项目中Api的组织和返回数据处理的操作
2019/11/04 Javascript
webpack proxy 使用(代理的使用)
2020/01/10 Javascript
在Django框架中伪造捕捉到的URLconf值的方法
2015/07/18 Python
对python中raw_input()和input()的用法详解
2018/04/22 Python
python之django母板页面的使用
2018/07/03 Python
用Python去除图像的黑色或白色背景实例
2019/12/12 Python
Python类反射机制使用实例解析
2019/12/30 Python
如何让python的运行速度得到提升
2020/07/08 Python
canvas实现高阶贝塞尔曲线(N阶贝塞尔曲线生成器)
2018/01/10 HTML / CSS
HTML5自定义视频播放器源码
2020/01/06 HTML / CSS
美国手工艺品市场的领导者:Annie’s
2019/04/04 全球购物
JMS中Topic和Queue有什么区别
2013/05/15 面试题
优秀班干部事迹材料
2014/01/26 职场文书
女儿十岁生日答谢词
2014/01/27 职场文书
创意婚礼策划方案
2014/05/18 职场文书
乡镇领导班子四风整顿行动工作汇报
2014/10/25 职场文书
打架检讨书范文
2015/01/27 职场文书
写给同学的新学期寄语
2015/02/27 职场文书
文艺部部长竞选稿
2015/11/21 职场文书
关于践行三严三实的心得体会
2016/01/05 职场文书