msztorc/svg-convert

Various helpers to convert SVG files to other formats including PNG & PDF.

1.0.0 2019-04-26 20:55 UTC

This package is auto-updated.

Last update: 2024-04-24 02:28:49 UTC


README

Build Status License

svg-convert

svg-convert

Various helpers to convert SVG files to other formats, especially PNG and PDF. Package includes CLI (phantomjs script) to render and php adapter to manipulate and convert SVG format using phantomjs or rsvg-convert (librsvg).

Install PhantomJS

Before installing PhantomJS, you will need to install some required packages on your system. You can install all of them with the following commands:

sudo apt-get update

sudo apt-get install build-essential chrpath libssl-dev libxft-dev libfreetype6-dev libfreetype6 libfontconfig1-dev libfontconfig1 -y

Next, you will need to download the PhantomJS. You can download the latest stable version of the PhantomJS from their official website.

wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

Extract the downloaded archive file to desired system location:

sudo tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/share/

Next, create a symlink of PhantomJS binary file to systems bin directory:

sudo ln -s /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/

Use psvg-convert script globally

chmod +x src/js/psvg-convert.js
sudo cp src/js/psvg-convert.js /usr/local/bin/psvg-convert

Install RSVG (only if you want to convert svg using librsvg)

sudo apt-get update

sudo apt-get install libcairo2-dev libspectre-dev librsvg2-dev \ libpoppler-glib-dev librsvg2-bin

Examples

Convert SVG to PNG format using PhantomJS CLI script

psvg-convert inputfile.svg outputfile.png

Usage

psvg-convert svgfile outputfile [--width=800 --height=600 --zoom=2.5 --format=png|pdf]

Convert SVG file in PHP

Initializing method with phantomjs argument when you want use to PhantomJS engine

$svg = (new SVG)->init('phantomjs');

or if you want to use rsvg-convert

$svg = (new SVG)->init('rsvg');
<?php

    $svg = (new SVG)->init('phantomjs', 'inputfile.svg');
    $svg->setFormat('png');
    $svg->convert();
    $svg->save('outputfile.png');

Manipulate and convert SVG file in PHP

<?php

    $svg = (new SVG)->init('phantomjs', 'inputfile.svg');
    $svg->setAttribute('path', 'fill', '#131C77'); //change fill color to all paths
    $svg->setFormat('png');
    $svg->convert();
    $svg->save('outputfile.png');

Other useful methods

Open file

$svg->open('file.svg'); //open file

Set zoom

$svg->setZoom(2.5); //enlarge original svg size up to 2.5x

Set size

$svg->setWidth(500); //set output width to 500px
$svg->setHeight(300); //set output height to 300px

//echo 'output size: ' . $svg->getWidth() . 'x' . $svg->getHeight();

Set output format

$svg->setFormat('pdf'); //set output format to pdf

//echo 'output format: ' . $svg->getFormat();

Display file

$svg->display(); //display image

Download file

$svg->download(); //download file

Save file

$svg->save('file.png'); //save file

Image tests

All images you can find in unit tests folder.

PhantomJS rsvg-convert
68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7067616c6c6172646f2e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7267616c6c6172646f2e706e67
68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f706361722e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f726361722e706e67
68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f707562756e74752e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f727562756e74752e706e67
68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7064656269616e2e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7264656269616e2e706e67
68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7074696765722e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7274696765722e706e67

SVG manipulation

68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7064656269616e2e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7064656269616e2d626c75652e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7064656269616e2d677265656e2e706e67

    $svg = (new SVG)->init('phantomjs', 'debian.svg');
    $svg->setAttribute('path', 'fill', '#131C77')->setFormat('png')->convert()->save('debian-blue.png');
    $svg->setAttribute('path', 'fill', '#06A70D')->setFormat('png')->convert()->save('debian-green.png');

68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f707562756e7475312e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f707562756e7475322e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f707562756e7475332e706e67 68747470733a2f2f737a746f72632e636f6d2f7376672d636f6e766572742f696d616765732f7562756e74752e676966

$svg1 = (new SVG)->init('phantomjs', 'ubuntu.svg')->setFormat('png')->convert()->save('ubuntu1.png');

$svg2 = (new SVG)->init('phantomjs', 'ubuntu.svg')
    ->setAttribute('use[1]', 'fill', '#f40')
    ->setAttribute('use[2]', 'fill', '#f80')
    ->setAttribute('use[3]', 'fill', '#d00')
    ->setAttribute('use[4]', 'fill', '#f40')
    ->setAttribute('use[5]', 'fill', '#f80')
    ->setAttribute('use[6]', 'fill', '#d00')
    ->setFormat('png')->convert()->save('ubuntu2.png');

$svg3 = (new SVG)->init('phantomjs', 'ubuntu.svg')
    ->setAttribute('use[1]', 'fill', '#f80')
    ->setAttribute('use[2]', 'fill', '#d00')
    ->setAttribute('use[3]', 'fill', '#f40')
    ->setAttribute('use[4]', 'fill', '#f80')
    ->setAttribute('use[5]', 'fill', '#d00')
    ->setAttribute('use[6]', 'fill', '#f40')
    ->setFormat('png')->convert()->save('ubuntu3.png');

Create gif using imagemagick

convert -loop 0 -delay 25 ubuntu1.png ubuntu2.png ubuntu3.png ubuntu.gif

License

MIT