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之where语句生成器
Mar 24 PHP
浅析php变量修饰符static的使用
Jun 28 PHP
ie与session丢失(新窗口cookie丢失)实测及解决方案
Jul 15 PHP
解决PHP4.0 和 PHP5.0类构造函数的兼容问题
Aug 01 PHP
一个经典的PHP验证码类分享
Nov 18 PHP
php集成环境xampp中apache无法启动问题解决方案
Nov 18 PHP
php将图片保存为不同尺寸图片的图片类实例
Mar 30 PHP
Smarty最简单实现列表奇偶变色的方法
Jul 01 PHP
Zend Framework缓存Cache用法简单实例
Mar 19 PHP
php 防止表单重复提交两种实现方法
Nov 03 PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
Nov 14 PHP
php更新cookie内容的详细方法
Sep 30 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
德劲1103的维修打理经验
2021/03/02 无线电
谈谈PHP语法(2)
2006/10/09 PHP
PHP+MYSQL 出现乱码的解决方法
2008/08/08 PHP
PHP 实现链式操作
2021/03/09 PHP
jquery 笔记 事件
2011/11/02 Javascript
浅析onsubmit校验表单时利用ajax的return false无效问题
2013/07/10 Javascript
高效的获取当前元素是父元素的第几个子元素
2013/10/15 Javascript
了解JavaScript中的选择器
2019/05/24 Javascript
javascript function(函数类型)使用与注意事项小结
2019/06/10 Javascript
vue登录注册实例详解
2019/09/14 Javascript
layui前端时间戳转化实例
2019/11/15 Javascript
Bootstrap table 服务器端分页功能实现方法示例
2020/06/01 Javascript
vue 解决在微信内置浏览器中调用支付宝支付的情况
2020/11/09 Javascript
python查看微信好友是否删除自己
2016/12/19 Python
Python爬虫实现网页信息抓取功能示例【URL与正则模块】
2017/05/18 Python
机器学习python实战之决策树
2017/11/01 Python
对python读取zip压缩文件里面的csv数据实例详解
2019/02/08 Python
django用户登录验证的完整示例代码
2019/07/21 Python
django-xadmin根据当前登录用户动态设置表单字段默认值方式
2020/03/13 Python
如何查看Django ORM执行的SQL语句的实现
2020/04/20 Python
浅谈pytorch中torch.max和F.softmax函数的维度解释
2020/06/28 Python
基于Python的一个自动录入表格的小程序
2020/08/05 Python
利用Python实现字幕挂载(把字幕文件与视频合并)思路详解
2020/10/21 Python
飞利浦比利时官方网站:Philips比利时
2016/08/24 全球购物
台湾家适得:Homeget
2019/02/11 全球购物
如何利用find命令查找文件
2015/02/07 面试题
大客户销售经理职责
2013/12/04 职场文书
自荐书范文
2013/12/08 职场文书
简单英文演讲稿
2014/01/01 职场文书
护理学专业求职信
2014/06/29 职场文书
完整版商业计划书
2014/09/15 职场文书
PySwarms(Python粒子群优化工具包)的使用:GlobalBestPSO例子解析
2021/04/05 Python
golang import自定义包方式
2021/04/29 Golang
利用Python判断你的密码难度等级
2021/06/02 Python
MySql数据库触发器使用教程
2022/06/01 MySQL
Win10系统搭建ftp文件服务器详细教程
2022/08/05 Servers