kaareln / php-svg-path-data
使用面向对象结构添加 SVG 路径数据
1.0.4
2023-03-27 14:20 UTC
Requires
- php: >=8.1.0
Requires (Dev)
- meyfa/phpunit-assert-gd: ^v3.0.0
- phpmd/phpmd: ^2.13.0
- phpunit/phpunit: ^9.5.26
This package is not auto-updated.
Last update: 2024-09-24 19:35:43 UTC
README
PHP-SVG-PATH-DATA 是一个 PHP 库,可以帮助在处理 PHP-SVG 库时使用。它允许使用面向对象结构添加 SVG 路径数据。
安装
PHP-SVG-PATH-DATA 需要 PHP 版本 8.1 或更高。此软件包可在 Packagist 上找到,并可以使用 Composer 进行安装
$ composer require kaareln/php-svg-path-data
使用
SVGPath 指令以字符串形式表示,这可能使其难以操作。然而,通过使用对象表示形式,可以更轻松地构建和解析路径。这种方法还提供了面向对象编程的好处,例如能够保持对路径不同部分的引用并按需操作它们。这可以在处理 SVGPaths 时导致更高效和更有组织的代码。
$path = new SVGPath(); $data = new SVGPathData(); $data->addCommand(new Move(0, 0)); $topLine = new Line(20, 0); $data->addCommand($topLine); $rightLine = new Line(20, 20); $data->addCommand($rightLine); // at this point I have a references to $topLine and $rightLine, which I can use to modify individually, for example I can move them by 20px $topLine->x += 20; $rightLine->x += 20; // convert the instructions to string and save as d param $path->setArgument('d', $data->__toString());
使用解析器,可以创建和修改表示路径不同部分的单独对象,以进行更精确的更改和更大的灵活性
// assume $path is an instanceof SVGPath $pathData = SVGPathData::fromString($path->getArgument('d')); // loop for each command in reverse order and possibly change/read parts of the path foreach ($pathData as $command) { if ($command instanceof Line) { // move each line coordinates by 20px $command->x += 20; } } // alternatively you can use `transform` method that runs against each command and allows replacing them. In this example I replace all bezier curves with straight lines $pathData->transform(function (PathDataCommandInterface $command) { if ($command instanceof BezierCurve) { return new Line($command->x, $command->y); } }); // update the path $path->setArgument('d', $pathData->__toString());
贡献
欢迎拉取请求。对于重大更改,请首先提交问题以讨论您想要更改的内容。
请确保根据需要更新测试。