All Projects → meyfa → Php Svg

meyfa / Php Svg

Licence: mit
Vector graphics (SVG) library for PHP

Projects that are alternatives of or similar to Php Svg

Svg.skia
An SVG rendering library.
Stars: ✭ 122 (-52.34%)
Mutual labels:  rendering, vector-graphics, svg
Svglib
Read SVG files and convert them to other formats.
Stars: ✭ 139 (-45.7%)
Mutual labels:  rendering, vector-graphics, svg
Picasso
Picasso is a high quality 2D vector graphic rendering library. It support path , matrix , gradient , pattern , image and truetype font.
Stars: ✭ 205 (-19.92%)
Mutual labels:  rendering, vector-graphics, svg
Rst
PHP library to parse reStructuredText documents
Stars: ✭ 90 (-64.84%)
Mutual labels:  parser, library
Anglesharp.js
👼 Extends AngleSharp with a .NET-based JavaScript engine.
Stars: ✭ 68 (-73.44%)
Mutual labels:  parser, library
Cat
Plain C library for parsing AT commands for use in host devices.
Stars: ✭ 77 (-69.92%)
Mutual labels:  parser, library
Anglesharp.css
👼 Library to enable support for cascading stylesheets in AngleSharp.
Stars: ✭ 27 (-89.45%)
Mutual labels:  parser, library
Commonmark Java
Java library for parsing and rendering CommonMark (Markdown)
Stars: ✭ 1,675 (+554.3%)
Mutual labels:  parser, library
Argumentum
C++ command line parsing library
Stars: ✭ 92 (-64.06%)
Mutual labels:  parser, library
Typin
Declarative framework for interactive CLI applications
Stars: ✭ 126 (-50.78%)
Mutual labels:  parser, library
Isobmff
C++ Library for ISO/IEC 14496-12 - ISO Base Media File Format (QuickTime, MPEG-4, HEIF, etc)
Stars: ✭ 157 (-38.67%)
Mutual labels:  parser, library
Php Svg Lib
SVG file parsing / rendering library
Stars: ✭ 1,146 (+347.66%)
Mutual labels:  parser, svg
Sharpmath
A small .NET math library.
Stars: ✭ 36 (-85.94%)
Mutual labels:  parser, library
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-67.97%)
Mutual labels:  parser, library
Substitution Schedule Parser
Java library for parsing schools' substitution schedules. Supports multiple different systems mainly used in the German-speaking countries, including Untis, svPlan, and DAVINCI
Stars: ✭ 33 (-87.11%)
Mutual labels:  parser, library
Simplepie
A simple Atom/RSS parsing library for PHP.
Stars: ✭ 1,389 (+442.58%)
Mutual labels:  parser, library
Libnmea
Lightweight C library for parsing NMEA 0183 sentences
Stars: ✭ 146 (-42.97%)
Mutual labels:  parser, library
Sdk
Library for using Grafana' structures in Go programs and client for Grafana REST API.
Stars: ✭ 193 (-24.61%)
Mutual labels:  parser, library
vectorexpress-api
Vector Express is a free service and API for converting, analyzing and processing vector files.
Stars: ✭ 66 (-74.22%)
Mutual labels:  png, vector-graphics
Badx12
A Python Library for parsing ANSI ASC X12 files.
Stars: ✭ 25 (-90.23%)
Mutual labels:  parser, library

php-svg

Build Status Maintainability Test Coverage

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:

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

  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

Composer (recommended)

This package is available through Composer/Packagist:

$ composer require meyfa/php-svg

Manual

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

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


Getting Started

Creating an image

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

<?php

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

// image with 100x100 viewport
$image = new SVG(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 SVG to a PHP/GD image resource, or in other words convert it to a raster image, you simply call toRasterImage($width, $height [, $background]) on it. Example:

<?php

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

$image = new SVG(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.
// the background will be transparent by default.
$rasterImage = $image->toRasterImage(200, 200);

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

If you require a specific background color, e.g. white, use the 3rd parameter. It supports all CSS colors (including named colors, hexadecimal, rgba, etc.):

<?php
$rasterImage = $image->toRasterImage(200, 200, '#FFFFFF');

Specifying a background color is mandatory for JPEG output, as JPEG does not support transparency.

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:

<?php

use SVG\SVG;

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

$image = SVG::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 SVG::fromFile($file). That function supports local file paths as well as URLs.

For additional documentation, see the wiki.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].