2017 © Pedro Peláez
 

library php-svg

Read, edit, write, and render SVG files with PHP (yoya's customize)

image

yoya/php-svg

Read, edit, write, and render SVG files with PHP (yoya's customize)

  • Thursday, May 31, 2018
  • by yoya
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 34 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

php-svg (yoya's customize)

, (*1)

This is a vector graphics library for PHP, which surely is a broad specification. That is due to the fact that the goal of this project is to offer features in three different, big areas:, (*2)

  • Generating SVG images from PHP code and outputting them, either into XML strings or into files.
  • Loading and parsing XML strings into document trees that can be easily modified and then also turned back into strings.
  • Transforming parsed or generated document trees into raster graphics, like PNG.

Contributing

These tasks will take a lot of time and effort, so you are welcome to contribute if this is a project you are interested in.
In case you decide to contribute, please honor these things:, (*3)

  1. External libraries shall not be used.
  2. Please set your editor to use 4 spaces for indentation. In general, it would be good to follow the existing code style for consistency.
  3. Source files must end with a newline character.
  4. By contributing code, you agree to license that code under the MIT license to this project.

Installation

This package is available through Composer/Packagist:, (*4)

$ composer require yoya/php-svg

Manual

Download this repo, or the latest release, and put it somewhere in your project. Then do:, (*5)

<?php
require_once __DIR__.'/<path_where_you_put_it>/autoloader.php';

The rest works exactly the same as with Composer, just without things like nice version management., (*6)


Getting Started

Creating an image

The following code generates an SVG with a blue square, sets the Content-Type header and echoes it:, (*7)

<?php

use SVG\SVGImage;
use SVG\Nodes\Shapes\SVGRect;

// image with 100x100 viewport
$image = new SVGImage(100, 100);
$doc = $image->getDocument();

// blue 40x40 square at (0, 0)
$square = new SVGRect(0, 0, 40, 40);
$square->setStyle('fill', '#0000FF');
$doc->addChild($square);

header('Content-Type: image/svg+xml');
echo $image;

Rasterizing

To convert an instance of SVGImage to a PHP/GD image resource, or in other words convert it to a raster image, you simply call toRasterImage($width, $height) on it. Example:, (*8)

<?php

use SVG\SVGImage;
use SVG\Nodes\Shapes\SVGCircle;

$image = new SVGImage(100, 100);
$doc = $image->getDocument();

// circle with radius 20 and green border, center at (50, 50)
$doc->addChild(
    (new SVGCircle(50, 50, 20))
        ->setStyle('fill', 'none')
        ->setStyle('stroke', '#0F0')
        ->setStyle('stroke-width', '2px')
);

// rasterize to a 200x200 image, i.e. the original SVG size scaled by 2
$rasterImage = $image->toRasterImage(200, 200);

header('Content-Type: image/png');
imagepng($rasterImage);

Loading an SVG

You can load SVG images both from strings and from files. This example loads one from a string, moves the contained rectangle and echoes the new SVG:, (*9)

<?php

use SVG\SVGImage;

$svg  = '<svg width="100" height="100">';
$svg .= '<rect width="50" height="50" fill="#00F" />';
$svg .= '</svg>';

$image = SVGImage::fromString($svg);
$doc = $image->getDocument();

$rect = $doc->getChild(0);
$rect->setX(25)->setY(25);

header('Content-Type: image/svg+xml');
echo $image;

For loading from a file instead, you would call SVGImage::fromFile($file). That function supports local file paths as well as URLs., (*10)

The Versions

31/05 2018

dev-master

9999999-dev http://pwiki.awm.jp/~yoya/?php-svg

Read, edit, write, and render SVG files with PHP (yoya's customize)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

svg

31/05 2018

v1.0.0

1.0.0.0 http://pwiki.awm.jp/~yoya/?php-svg

Read, edit, write, and render SVG files with PHP (yoya's customize)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

svg

24/08 2017

dev-develop

dev-develop https://github.com/meyfa/php-svg

Read, edit, write, and render SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

svg

29/07 2017

v0.6.0

0.6.0.0 https://github.com/meyfa/php-svg

Read, edit, write, and render SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

svg

06/02 2017

v0.5.4

0.5.4.0 https://github.com/JangoBrick/php-svg

Read, edit, write, and render SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

03/01 2017

v0.5.3

0.5.3.0 https://github.com/JangoBrick/php-svg

Read, edit, write, and render SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

12/12 2016

v0.5.2

0.5.2.0 https://github.com/JangoBrick/php-svg

Read, edit, write, and render SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

08/12 2016

v0.5.1

0.5.1.0 https://github.com/JangoBrick/php-svg

Read, edit, write, and render SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

12/11 2016

v0.5.0

0.5.0.0 https://github.com/JangoBrick/php-svg

Generate SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

29/10 2016

v0.4.2

0.4.2.0 https://github.com/JangoBrick/php-svg

Generate SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

14/10 2016

v0.4.1

0.4.1.0 https://github.com/JangoBrick/php-svg

Generate SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

13/10 2016

v0.4.0

0.4.0.0 https://github.com/JangoBrick/php-svg

Generate SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

09/10 2016

v0.3.0

0.3.0.0 https://github.com/JangoBrick/php-svg

Generate SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg

19/09 2016

v0.2.0

0.2.0.0 https://github.com/JangoBrick/php-svg

Generate SVG files with PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar JangoBrick

svg