PHPackages                             the-bit-bang/php-svg-framework-svgf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. the-bit-bang/php-svg-framework-svgf

ActiveLibary[Image &amp; Media](/categories/media)

the-bit-bang/php-svg-framework-svgf
===================================

Provides funtionality for creating, reading, modifying and writing svg files in php

1.0.0(8y ago)312.3k4[1 issues](https://github.com/TheBitBang/php-svg-framework-SVGF/issues)MITPHPPHP &gt;=5.6CI failing

Since Jun 18Pushed 4y ago1 watchersCompare

[ Source](https://github.com/TheBitBang/php-svg-framework-SVGF)[ Packagist](https://packagist.org/packages/the-bit-bang/php-svg-framework-svgf)[ RSS](/packages/the-bit-bang-php-svg-framework-svgf/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

SVGF: SVG Framework for PHP
===========================

[](#svgf-svg-framework-for-php)

**SVGF** (Scalable Vector Graphics Framework) is a PHP library for creating and modifying SVG files.

It is composed of:

- A PHP implementation of the [Scalable Vector Graphics (SVG) 1.1](https://www.w3.org/TR/SVG/Overview.html) specification.
- A PHP implementation of the [Document Object Model (DOM) Level 2 Style Specification](https://www.w3.org/TR/DOM-Level-2-Style/).
- Additional functionality for the SVG element manipulation.

Extended functionality
----------------------

[](#extended-functionality)

These are the main functionalities provided by **SVGF**:

- Read and write SVG files.
- Programatically modify the attributes of the elements in compliance with the (SVG) 1.1 specification.
- Programatically modify the style attribute in compliance with the (DOM) Level 2 Style Specification.
- Access SVG elements using XPath.
- Programatically create SVG basic shapes.
- Programatically create SVG texts.
- Programatically align SVG elements.
- Draw preset connectors between points and elements

Usage
-----

[](#usage)

### Create SVG files

[](#create-svg-files)

#### Create SVG with size A4 (portrait orientation)

[](#create-svg-with-size-a4-portrait-orientation)

```
$dom_doc_svg = new \DOMDocument('1.0', 'utf-8');
$svg_svg = new SVGSVGElement($dom_doc_svg);
$svg_svg->setWidth('210mm');
$svg_svg->setHeight('297mm');
$svg_svg->setViewBox('0 0 210 270');
$svg_svg->setVersion('1.1');
$svg_svg->setAttribute('xmlns','http://www.w3.org/2000/svg');
```

#### Create SVG with size A4 using SVGF (portrait orientation)

[](#create-svg-with-size-a4-using-svgf-portrait-orientation)

```
$dom_doc_svg = new \DOMDocument('1.0', 'utf-8');
$svg_svg = SVGFElement::svg($dom_doc_svg,'a4',SVGFElement::SIZE_A4,'portrait');
```

#### Create SVG with size A4 using SVGF (landscape orientation)

[](#create-svg-with-size-a4-using-svgf-landscape-orientation)

```
$dom_doc_svg = new \DOMDocument('1.0', 'utf-8');
$svg_svg = SVGFElement::svg($dom_doc_svg,'a4');
```

or

```
$dom_doc_svg = new \DOMDocument('1.0', 'utf-8');
$svg_svg = SVGFElement::svg($dom_doc_svg,'a4',SVGFElement::SIZE_A4,'landscape');
```

#### Create SVG with size Full HD

[](#create-svg-with-size-full-hd)

```
$dom_doc_svg = new \DOMDocument('1.0', 'utf-8');
$svg_svg = new SVGSVGElement($dom_doc_svg);
$svg_svg->setWidth('1920px');
$svg_svg->setHeight('1080px');
$svg_svg->setViewBox('0 0 1920 1080');
$svg_svg->setVersion('1.1');
$svg_svg->setAttribute('xmlns','http://www.w3.org/2000/svg');
```

#### Create SVG with size Full HD using SVGF

[](#create-svg-with-size-full-hd-using-svgf)

```
$dom_doc_svg = new \DOMDocument('1.0', 'utf-8');
$svg_svg = SVGFElement::svg($dom_doc_svg,'a4',SVGFElement::SIZE_FHD);
```

#### SVGF predefined sizes

[](#svgf-predefined-sizes)

- A0, A1, A2, A3, A4, A5
- ARCH\_A, ARCH\_B, ARCH\_C, ARCH\_D, ARCH\_E
- ICON\_16X16, ICON\_32X32, ICON\_48X48
- VGA, SVGA, XGA, HD, FHD, QHD, UHD, 8K

### Read existing SVG files

[](#read-existing-svg-files)

```
$path_to_file = './file_name.svg';
$dom_doc_svg = SVGFImportFromSVG::getSVGFromFile($path_to_file);
```

### Create basic shapes

[](#create-basic-shapes)

#### Create rectangle using methods

[](#create-rectangle-using-methods)

```
$svg_rect = new SVGRectElement($dom_doc_svg);
$svg_rect->setId('rect_50x50_1');
$svg_rect->setX('0');
$svg_rect->setY('0');
$svg_rect->setWidth('50');
$svg_rect->setHeight('50');
$svg_rect->setRx('5');
$svg_rect->setRy('5');
$svg_svg->appendChild($svg_rect);
```

[![](./examples/readme/rect_50x50_1.svg)](./examples/readme/rect_50x50_1.svg)

#### Create rectangle setting values

[](#create-rectangle-setting-values)

```
$svg_rect = new SVGRectElement($dom_doc_svg);
$svg_rect->id = 'rect_50x50_2';
$svg_rect->x = '0';
$svg_rect->y = '0';
$svg_rect->width = '50';
$svg_rect->height = '50';
$svg_rect->rx = '10';
$svg_rect->ry = '10';
$svg_svg->appendChild($svg_rect);
```

[![](./examples/readme/rect_50x50_2.svg)](./examples/readme/rect_50x50_2.svg)

#### Create rectangle using SVGF

[](#create-rectangle-using-svgf)

```
$svg_rect = SVGFElement::rect($dom_doc_svg,'50','50','rect_50x50_3');
$svg_svg->appendChild($svg_rect);
```

[![](./examples/readme/rect_50x50_3.svg)](./examples/readme/rect_50x50_3.svg)

#### Create circle using methods

[](#create-circle-using-methods)

```
$svg_circle = new SVGCircleElement($dom_doc_svg);
$svg_circle->setId('circle_10');
$svg_circle->setCx(25);
$svg_circle->setCy(25);
$svg_circle->setR(10);
$svg_svg->appendChild($svg_circle);
```

[![](./examples/readme/circle_10.svg)](./examples/readme/circle_10.svg)

#### Create circle setting values

[](#create-circle-setting-values)

```
$svg_circle = new SVGCircleElement($dom_doc_svg);
$svg_circle->id = 'circle_15';
$svg_circle->cx = 25;
$svg_circle->cy = 25;
$svg_circle->r = 15;
$svg_svg->appendChild($svg_circle);
```

[![](./examples/readme/circle_15.svg)](./examples/readme/circle_15.svg)

#### Create circle using SVGF

[](#create-circle-using-svgf)

```
// create circle
$svg_circle = SVGFElement::circle($dom_doc_svg,'20','circle_20','25','25');
$svg_svg->appendChild($svg_circle);
```

[![](./examples/readme/circle_20.svg)](./examples/readme/circle_20.svg)

### Apply style

[](#apply-style)

#### Apply style setting individual properties

[](#apply-style-setting-individual-properties)

```
$svg_circle = SVGFElement::circle($dom_doc_svg,10,'circle_10_style',25,25);
$svg_circle->style->setProperty('fill','#d9737a','');
$svg_circle->style->setProperty('stroke','#861a22','');
$svg_circle->style->setProperty('stroke-width','2','');
$svg_svg->appendChild($svg_circle);
```

[![](./examples/readme/circle_10_style.svg)](./examples/readme/circle_10_style.svg)

#### Apply style setting style as string with properties and values

[](#apply-style-setting-style-as-string-with-properties-and-values)

```
$svg_circle = SVGFElement::circle($dom_doc_svg,15,'circle_15_style',25,25);
$svg_circle->style = "fill: #d9737a; stroke: #861a22; stroke-width: 2;";
$svg_svg->appendChild($svg_circle);
```

[![](./examples/readme/circle_15_style.svg)](./examples/readme/circle_15_style.svg)

#### Apply style from SVGF

[](#apply-style-from-svgf)

```
$svg_circle = SVGFElement::circle($dom_doc_svg,20,'circle_20_style',25,25,'#d9737a','#861a22',2);
$svg_svg->appendChild($svg_circle);
```

[![](./examples/readme/circle_20_style.svg)](./examples/readme/circle_20_style.svg)

### Align SVG elements

[](#align-svg-elements)

Source svg file used to illustrate align functionalities:

```

```

[![](./examples/readme/align_source_file.svg)](./examples/readme/align_source_file.svg)

#### Align to center

[](#align-to-center)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::ALIGN_CENTER);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::ALIGN_CENTER);
```

[![](./examples/readme/align_center.svg)](./examples/readme/align_center.svg)

#### Align to center with offset

[](#align-to-center-with-offset)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::ALIGN_CENTER,20,0);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::ALIGN_CENTER,0,-10);
```

[![](./examples/readme/align_center_offset.svg)](./examples/readme/align_center_offset.svg)

#### Align to sides

[](#align-to-sides)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::ALIGN_BOTTOM);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::ALIGN_RIGHT);
```

[![](./examples/readme/align_1.svg)](./examples/readme/align_1.svg)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::ALIGN_LEFT);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::ALIGN_TOP);
```

[![](./examples/readme/align_2.svg)](./examples/readme/align_2.svg)

#### Position on sides

[](#position-on-sides)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::POSITION_LEFT);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::ALIGN_CENTER);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::POSITION_TOP);
```

[![](./examples/readme/align_position_1.svg)](./examples/readme/align_position_1.svg)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::POSITION_RIGHT);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::POSITION_RIGHT);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_1,SVGFObjectBox::POSITION_BOTTOM);
```

[![](./examples/readme/align_position_2.svg)](./examples/readme/align_position_2.svg)

#### Position on sides with offset

[](#position-on-sides-with-offset)

```
$svg_rect_1 = SVGFAlign::align($svg_rect_1,$svg_rect_0,SVGFObjectBox::POSITION_LEFT,-5);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::ALIGN_CENTER);
$svg_rect_2 = SVGFAlign::align($svg_rect_2,$svg_rect_0,SVGFObjectBox::POSITION_TOP,0,-5);
```

[![](./examples/readme/align_position_offset.svg)](./examples/readme/align_position_offset.svg)

### Access SVG elements using XPath

[](#access-svg-elements-using-xpath)

Source svg file used to illustrate xpath functionalities:

```

```

[![](./examples/readme/xpath_source_file.svg)](./examples/readme/xpath_source_file.svg)

To get the DOM representation:

```
$xpath = new \DOMXPath($dom_doc_svg);
```

#### Select elements by id

[](#select-elements-by-id)

```
// Change fill color of the elements rect_1 and circle_2 to #1a867e
$matches = $xpath->query("//*[@id='rect_1'] | //*[@id='circle_2']");
foreach ($matches as $match) {
	$match->style->setProperty('fill','#1a867e','');
}
```

[![](./examples/readme/xpath_select_elements_by_id.svg)](./examples/readme/xpath_select_elements_by_id.svg)

#### Select elements by tag name

[](#select-elements-by-tag-name)

```
// Change fill color of circle elements to #1a867e. Move them 25px down and 20px to the right
$matches = $xpath->query("//circle");
foreach ($matches as $match) {
	$match->style->setProperty('fill','#1a867e','');
	$match->cy = $match->cy + 25;
	$match->cx = $match->cx + 20;
}
```

[![](./examples/readme/xpath_select_elements_by_tag_name.svg)](./examples/readme/xpath_select_elements_by_tag_name.svg)

#### Select child elements

[](#select-child-elements)

```
// Change fill color of the g child elements to #1a867e
$matches = $xpath->query("//g/*");
foreach ($matches as $match) {
	$match->style->setProperty('fill','#1a867e','');
}
```

[![](./examples/readme/xpath_select_children.svg)](./examples/readme/xpath_select_children.svg)

#### Select elements by attribute value

[](#select-elements-by-attribute-value)

```
// Create a corner radius of 15px in rect_1
$matches = $xpath->query("//rect[@id='rect_1']");
foreach ($matches as $match) {
	$match->rx = 15;
	$match->ry = 15;
}

// Create a corner radius of 5px in rect_2
$matches = $xpath->query("//rect[@id='rect_2']");
foreach ($matches as $match) {
	$match->rx = 5;
	$match->ry = 5;
}

// Change fill color of rect elements with rx < 10 (or not set) to #1a867e.
$matches = $xpath->query("//rect[@rx
