使用PHP动态生成SVGZ图片(gzip压缩的SVG)
经测试SVG的动画性能很差,简单的动画CPU都能占到 30%左右。
可能的用途:
- 动态天气图片
- 访问统计计数图片
- 文字验证生成
- 动态头像
- 静态外连图片
- 图片CHART
<?php header('Content-Type:image/svg+xml'); header('Content-Encoding:gzip'); $image_width = @$_GET['w'] ?: 800; $image_height = @$_GET['h'] ?: 600; $svg = <<<EOF <svg version="1.1" width="{$image_width}" height="{$image_height}" viewBox="0 0 {$image_width} {$image_height}" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" > <defs> <g id="E" transform="translate(-340,-90)"> <circle cx="340" cy="90" r="15" fill="blue" /> <path id="path2" d="M390,80 h 0 a50,19 0 1,0 1,1 z" fill="none" stroke="white" stroke-width="1"/> <circle cx="0" cy="0" r="5" fill="white" stroke="black" stroke-width="1" > <animateMotion dur="30s" repeatCount="indefinite"> <mpath xlink:href="#path2" /> </animateMotion> </circle> </g> <radialGradient id="sunfill" cx="50%" cy="50%" r="100%"> <stop stop-color="#FF0000" offset="0%" stop-opacity="1"/> <stop stop-color="#FBF900" offset="95%" stop-opacity="1"/> <stop stop-color="#FFFFFF" offset="100%" stop-opacity="1"/> </radialGradient> </defs> <rect x="0" y="0" width="100%" height="100%" fill="black" /> <circle cx="50%" cy="50%" r="10%" fill="url(#sunfill)" /> <path id="path1" d="M700,200 h 0 a350,200 0 1,0 1,1 z" fill="none" stroke="white" stroke-width="1"/> <use x="0" y="0" xlink:href="#E"> <animateMotion dur="365s" repeatCount="indefinite" > <mpath xlink:href="#path1" /> </animateMotion> </use> EOF; $err = 'svg 生成错误!' . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']; if ($err != '') { $svg .= '<text x="10" y="20" font-size="12" text-anchor="start" fill="#f00">' . $err . '</text>'; } $svg .= '</svg>'; $svg = gzencode($svg, 9); echo $svg;
欢迎转载,转载请注明:转载自[ http://www.cnblogs.com/zjfree/ ]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
2011-10-22 加密狗