nstdio/svg

此包已被废弃,不再维护。未建议替代包。

PHP SVG 库。

v0.4.2 2018-07-13 19:55 UTC

This package is not auto-updated.

Last update: 2020-01-24 17:08:56 UTC


README

PHP 上的完整 SVG 实现。目前,库中包含完整的 SVG 规范。SVGMagick 还包含了许多处理滤镜、渐变(例如具有方向和位置的均匀渐变)以及 SVG 基本形状(例如多边形、矩形、折线)路径的实用方法。SVGMagick 尚未有一个稳定的 API,因此任何内容都可能随时更改。

希望从社区中得到关于 SVGMagick 的反馈和建议,使其变得更好。

要求

  • php >= 5.4.0
  • ext-dom(默认启用)

安装

建议使用 composer 进行安装

$ composer require nstdio/svg: "dev-master"

或者在您的 composer.json 文件的 require 部分添加

"nstdio/svg": "dev-master"

基本用法

每个 SVG 标签在 SVGMagick 中都有对应的类。创建 SVGMagick 对象需要什么?您需要将父对象作为构造函数的第一个参数传递。因此,您不需要在父对象上显式调用 append

use nstdio\svg\container\Defs;
use nstdio\svg\container\SVG;
use nstdio\svg\gradient\LinearGradient;

// ...

$svg = new SVG();
$defs = new Defs($svg); // <defs> will be appended in <svg>.
$gradient = new LinearGradient($defs) // <linearGradient> will be appended in <defs>

除了 SVG 之外,所有类都遵循此规则。

形状


use nstdio\svg\container\SVG;
use nstdio\svg\shape\Rect;

// ...

$svg = new SVG(); // by default width = 640, height = 480.

$rect = new Rect($svg, 120, 300, 3, 3); // creating Rect object and appending <rect> element to <svg>

// You have two way to set <rect> element attributes.

// Use magic methods. Any attribute can be setted using magic setter.
// Note that for setting dash-separated attribute you must use camelCase.
// For setting stroke-linecap you must set strokeLinecap propery of corresponding object.

$rect->rx = 5;
$rect->ry = 5;
$rect->stroke = 'darkgreen';
$rect->fill = 'limegreen';
$rect->strokeWidth = 1.5;  // In this particular case strokeWidth will be converted into stroke-width.

// Or use apply method.
$rect->apply(['stroke' => 'darkgreen', 'fill' => 'limegreen', 'stroke-width' => 1.5]);
$rect->setBorderRadius(5); // setting rx and ry at once.

(new Circle($svg, 75, 200, 70))->apply([
    'fill' => '#001f3f',
    'fillOpacity' => 0.6,
    'stroke' => '#FF851B',
    'strokeWidth' => 5,
]);

echo $svg; // or $svg->draw() to get svg structure;

结果

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="640" height="480" version="1.1" viewBox="0 0 640 480">
    <rect height="120" width="300" x="3" y="3" rx="5" ry="5" stroke="darkgreen" fill="limegreen" stroke-width="1.5"></rect>
    <circle cx="75" cy="200" r="70" fill="#001f3f" fill-opacity="0.6" stroke="#FF851B" stroke-width="5"></circle>
</svg>