PHP Classes

PHP SVG Magick Library: Compose vectorial graphics in SVG format

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStarStar 80%Total: 234 All time: 8,112 This week: 66Up
Version License PHP version Categories
svgmagick 1.0.3Custom (specified...5PHP 5, Graphics, Traits
Description 

Author

This package can compose vectorial graphics in SVG format.

It provides classes that can compose the definition of different types of vectorial graphic elements as a DOM XML document.

Currently it supports the main SVG container, shapes, text, fonts, filters, gradients, animations and many more.

The composed SVG document is returned as a XML string.

Picture of Edgar Asatryan
Name: Edgar Asatryan <contact>
Classes: 4 packages by
Country: Armenia Armenia
Innovation award
Innovation award
Nominee: 1x

 

Example

<?php
use nstdio\svg\container\Defs;
use
nstdio\svg\container\G;
use
nstdio\svg\container\SVG;
use
nstdio\svg\gradient\UniformGradient;
use
nstdio\svg\shape\Rect;
use
nstdio\svg\text\Text;

require_once
__DIR__ . '/../vendor/autoload.php';

$style = ['stroke' => 'lightgray', 'stroke-width' => 0.5, 'rx' => 3, 'ry' => 3,];
$colors = ['#1D4350', '#A43931'];

$svg = new SVG(800, 800);

$defs = new Defs($svg);
$g1 = new G($svg);


$linearGradient = UniformGradient::verticalFromTop($defs, $colors);
$rect = (new Rect($g1, 100, 150, 3, 3))->apply($style)->applyGradient($linearGradient);
(new
Text($g1, "Top Bottom"))->apply(['x' => 3, 'y' => $rect->height + 20]);

$verticalFromBottom = UniformGradient::verticalFromBottom($defs, $colors);
$rect2 = (new Rect($g1, 100, 150, 160, 3))->apply($style)->applyGradient($verticalFromBottom);
(new
Text($g1, "Bottom Top"))->apply(['x' => $rect2->x, 'y' => $rect2->height + 20]);

$diagonalFromTopLeft = UniformGradient::diagonalFromTopLeft($defs, ['#D38312', '#A83279']);
$rect3 = (new Rect($g1, 100, 150, 3, $rect2->height + 33))->apply($style)->applyGradient($diagonalFromTopLeft);
(new
Text($g1, "From Top Left"))->apply(['x' => $rect3->x, 'y' => 250]);

$diagonalFromBottomRight = UniformGradient::diagonalFromBottomRight($defs, ['#D38312', '#A83279']);
$rect3 = (new Rect($g1, 100, 150, $rect2->x, $rect2->height + 33))->apply($style)->applyGradient($diagonalFromBottomRight);
(new
Text($g1, "From Bottom Right"))->apply(['x' => $rect3->x, 'y' => 250]);

$diagonalFromBottomLeft = UniformGradient::diagonalFromBottomLeft($defs, ['#D38312', '#A83279']);
$rect3 = (new Rect($g1, 100, 150, 316, $rect2->height + 33))->apply($style)->applyGradient($diagonalFromBottomLeft);
(new
Text($g1, "From Bottom Left"))->apply(['x' => $rect3->x, 'y' => 250]);

$diagonalFromTopRight = UniformGradient::diagonalFromTopRight($defs, ['#D38312', '#A83279']);
$rect3 = (new Rect($g1, 100, 150, 471, $rect2->height + 33))->apply($style)->applyGradient($diagonalFromTopRight);
(new
Text($g1, "From Top Right"))->apply(['x' => $rect3->x, 'y' => 250]);

$horizontalFromLeft = UniformGradient::horizontalFromLeft($defs, ['#780206', '#061161']);
$rect3 = (new Rect($g1, 100, 150, 3, 2 * $rect2->height + 66))->apply($style)->applyGradient($horizontalFromLeft);
(new
Text($g1, "From Left Right"))->apply(['x' => $rect3->x, 'y' => 385]);

$horizontalFromRight = UniformGradient::horizontalFromRight($defs, ['#780206', '#061161']);
$rect3 = (new Rect($g1, 100, 150, 160, 2 * $rect2->height + 66))->apply($style)->applyGradient($horizontalFromRight);
(new
Text($g1, "From Right Left"))->apply(['x' => $rect3->x, 'y' => 385]);

$radialTopLeft = UniformGradient::radialTopLeft($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 3, 3 * $rect2->height + 96))->apply($style)->applyGradient($radialTopLeft);
(new
Text($g1, "Radial Top Left"))->apply(['x' => $rect3->x, 'y' => 515]);


$radialTopRight = UniformGradient::radialTopRight($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 160, 3 * $rect2->height + 96))->apply($style)->applyGradient($radialTopRight);
(new
Text($g1, "Radial Top Right"))->apply(['x' => $rect3->x, 'y' => 515]);

$radialBottomRight = UniformGradient::radialBottomRight($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 316, 3 * $rect2->height + 96))->apply($style)->applyGradient($radialBottomRight);
(new
Text($g1, "Radial Bottom Right"))->apply(['x' => $rect3->x, 'y' => 515]);


$radialBottomLeft = UniformGradient::radialBottomLeft($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 471, 3 * $rect2->height + 96))->apply($style)->applyGradient($radialBottomLeft);
(new
Text($g1, "Radial Bottom Left"))->apply(['x' => $rect3->x, 'y' => 515]);


$radialTopCenter = UniformGradient::radialTopCenter($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 3, 4 * $rect3->height + 126))->apply($style)->applyGradient($radialTopCenter);
(new
Text($g1, "Radial Top Center"))->apply(['x' => $rect3->x, 'y' => 645]);

$radialLeftCenter = UniformGradient::radialLeftCenter($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 160, 4 * $rect3->height + 126))->apply($style)->applyGradient($radialLeftCenter);
(new
Text($g1, "Radial Left Center"))->apply(['x' => $rect3->x, 'y' => 645]);


$radialBottomCenter = UniformGradient::radialBottomCenter($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 316, 4 * $rect2->height + 126))->apply($style)->applyGradient($radialBottomCenter);
(new
Text($g1, "Radial Bottom Center"))->apply(['x' => $rect3->x, 'y' => 645]);


$radialBottomRight = UniformGradient::radialRightCenter($defs, ['#000', '#e74c3c']);
$rect3 = (new Rect($g1, 100, 150, 471, 4 * $rect2->height + 126))->apply($style)->applyGradient($radialBottomRight);
(new
Text($g1, "Radial Right Center"))->apply(['x' => $rect3->x, 'y' => 645]);

echo
$svg->draw();


Details

SVGMagick Build Status Scrutinizer Code Quality Code Coverage

The full SVG implementation on PHP. Currently, the library contains the full SVG specification. SVGMagick also contains a number of useful methods for working with filter, the gradient (e.g. UniformGradient with direction and position) and the transformation of the SVG basic shapes (e.g. polygon, rect, polyline) in the path. SVGMagick does not yet have a stable API, so everything can change at any time.

Hope to read feedbacks and suggestions from community about SVGMagick to make it better.

Requirements

  • php >= 5.4.0
  • ext-dom (enabled by default)

Installation

The suggested installation method is via composer:

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

or add

"nstdio/svg": "dev-master"

to the require section of your composer.json file.

Basic Usage

Every SVG tag has corresponding class in SVGMagick. What is needed for the creation of SVGMagick object? You have to pass as the first parameter of the constructor is the parent object. So you don't need to explicitly call append on parent object.

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>

All classes are subject to this rule except for SVG.

Shapes


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;

Result

<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>

  Files folder image Files (201)  
File Role Description
Files folder imageexample (18 files)
Files folder imagesrc (1 directory)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:234
This week:0
All time:8,112
This week:66Up
User Ratings User Comments (2)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:100%StarStarStarStarStarStar
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:80%StarStarStarStarStar
Rank:17
 
No result screens - its bad.
8 years ago (KillerDr)
80%StarStarStarStarStar
No result screens - its bad.
8 years ago (KillerDr)
80%StarStarStarStarStar