2017 © Pedro Peláez
 

library svg

A library used for manipulating and creating svg

image

krak/svg

A library used for manipulating and creating svg

  • Wednesday, April 26, 2017
  • by ragboyjr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 121 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 1 % Grown

The README.md

Svg

Krak Svg is a library for creating and exporting svg., (*1)

Installation

{
    "require": {
        "krak/svg": "^0.2"
    }
}

Design & Usage

The Svg library is broken up into several components: Visitors, Iterators, Serializers, and Element., (*2)

Building an Svg Tree

<?php
$svg = new Krak\Svg\Element\Svg();
$svg->getAttributes()
    ->setWidth(320)
    ->setHeight(320)
    ->setXmlns('http://www.w3.org/2000/svg')
    ->setAttribute(
        new Krak\Element\SimpleAttribute(
            'xmlns:xlink',
            'http://www.w3.org/1999/xlink'
        )
    );

$group = new Krak\Svg\Element\Group();

$transform_attr = new Krak\Svg\Element\TransformAttribute();
$transform_attr->translate(30, 20);
$group->getAttributes()->setAttribute($transform_attr);

$rect = new Krak\Svg\Element\Rect();
$rect->setAttributes(krak\svg\element\attr_iter_to_col([
        'width' => 80,
        'height' => 80,
        'x' => 0,
        'y' => 0,
        'fill' => '#ffffff',
    ]));

$group->appendChild($rect);

$imagick = new Imagick('file.jpg');
$image = new Krak\Svg\Element\Image();
$image->getAttributes()
    ->setAttribute(
        krak_svg_image_attr(
            $imagick->getImageBlob(),
            'image/jpeg',
            true // base64 decode
        )
    )
    ->setX(0)
    ->setY(0);

$svg->appendChild($group);
$svg->appendChild($image);

Serializing an Svg Tree

<?php
$string_serializer = new Krak\Svg\StringSvgSerializer();
$png_serializer = new Krak\Svg\RsvgCliSvgSerializer($string_serializer);

$svg_string_data = $string_serializer->serializeSvg($svg);
$svg_png_data = $png_serializer->serializeSvg($svg);

The StringSerializer will export the tree into svg tree., (*3)

The RsvgCliSvgSerializer uses the rsvg command line utility to export an svg to a png, pdf, ps or any other format supported by rsvg. This serializer requires a string serializer thought to convert it to a string first., (*4)

Iteratoring an Svg Tree

<?php
foreach (krak\svg\iter_top_down($svg) as $depth => $el) {
    // ...
}
foreach (krak\svg\iter_bottom_up($svg) as $depth => $el) {
    // ...
}
/* the following are just aliases of what's above */
foreach (new Krak\Svg\TopDownIterator($svg) as $depth => $el) {
    // ..
}
foreach (new Krak\Svg\BottomUpIterator($svg) as $depth => $el) {
    // ..
}

We currently support to types of iterators: Top down and bottom up. The functional iterators are implemented as generators traversing the svg tree iteratively (as apposed to recursively). The iterator classes are just wrappers around the functional iterator generators., (*5)

  • Top Down: Starts from the top of the tree and outputs on the way down.
  • Bottom Up: Starts from the bottom of the tree and prints the lowest nodes first and works it's way up.

Element Model

The element model is made up of Elements and Attributes. Each Element as an AttributeCollection which can hold Attributes. Most attributes are just SimpleAttriutes, but there are some special attributes that can be created and set per element., (*6)

Elements can also have hints which are just meta data associated with an element., (*7)

Visitors and Walkers

Visitors and walkers are a way for modifying the svg tree before serializing it., (*8)

  • AttributeString Visitor: Converts an elements attributes into a string
  • HeightCalculator Visitor: Calculates the height of an svg element by summing the height of all of it's child elements. It only applies height calculation on elements with the query hint of Krak\Svg\Visitor\HeightCalculatorVisitor::HINT value.

The Versions

26/04 2017

dev-master

9999999-dev http://gitlab.bighead.net/bighead/krak-svg

A library used for manipulating and creating svg

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

svg exporter krak

26/04 2017

v0.3.1

0.3.1.0 http://gitlab.bighead.net/bighead/krak-svg

A library used for manipulating and creating svg

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

svg exporter krak

07/10 2016

v0.3.0

0.3.0.0 http://gitlab.bighead.net/bighead/krak-svg

A library used for manipulating and creating svg

  Sources   Download

MIT

The Requires

  • php ~5.5

 

The Development Requires

svg exporter krak

03/10 2016

v0.2.1

0.2.1.0 http://gitlab.bighead.net/bighead/krak-svg

A library used for manipulating and creating svg

  Sources   Download

MIT

The Requires

  • php ~5.5

 

The Development Requires

svg exporter krak

04/08 2015

v0.2.0

0.2.0.0 http://gitlab.bighead.net/bighead/krak-svg

A library used for manipulating and creating svg

  Sources   Download

MIT

The Requires

  • php ~5.5

 

The Development Requires

svg exporter krak

25/03 2015

v0.1.0

0.1.0.0 http://gitlab.bighead.net/bighead/krak-svg

A library used for manipulating and creating svg

  Sources   Download

MIT

The Requires

  • php ~5.5

 

The Development Requires

svg exporter krak