Browse DevX
Sign up for e-mail newsletters from DevX


Add the Graphics Power of SVG to PHP : Page 2

Discover how to generate and manipulate SVG documents dynamically—directly from PHP.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Generating SVG with phpHtmlLib
One easy way to get solid SVG support is to download phpHtmlLib, an application development framework for developing Model-View-Controller (MVC) object-oriented web applications in PHP. PhpHtmlLib supports AJAX, caching, and rich client web applications, along with a comprehensive set of useful PHP classes and methods for working with HTML and XML documents—including, of course, SVG.

To install phpHtmlLib on your machine follow these steps:

  1. Unzip the archive into an folder.
  2. Copy the includes.inc file from the {$phpHtmlLib}/examples folder into your applications directory.
  3. Edit includes.inc to indicate the location of your autoload.inc path file. For example:
  4. include_once( '../phpLibs/phphtmllib/autoload.inc');

The base class of phpHtmlLib is Container, which has these direct descendents:

  • XMLTagClass builds and renders XML tags.
  • SVGGraph is the base class for generating SVG graphs.
  • XMLDocument lets you build a complete XML document and render it.
Because SVG is an XML-based language, the XMLTagClass class serves as the base class for an SVGTag class, which you use to set an SVG tag's attributes and values. Similarly, the XMLDocument class represents the base class for the SVGDocument class, which you use to create a new empty SVG document. You can obtain an SVGDocument instance using the following constructor:

SVGDocument __construct ( [string $width = "100%"], [string $height = "100%"])

In the preceding code, the $width and $height arguments represent the width and height of the SVG visualization window. Table 1 lists other important methods exposed by the SVGDocument class.

Table 1: Useful Methods: Here are the SVGDocument class's most important methods, with a brief description of each.
Method Description
void add (mixed $content) This method adds a component to the SVG document. The received argument is a string or a tag object.
string render(
 [int $indent_level = 0],
 [int $output_debug = 0])
This method renders an SVG document. The $indent_level argument represents the indentation level for the container and the output_debug argument represents a compatibility flag.
function svg_circle(
  $cx, $cy, $radius,
This function builds an SVG <circle> tag using the center coordinates, radius, and some design parameters.
function svg_ellipse(
  $cx, $cy, $rx, $ry,
This function builds an SVG <ellipse> tag using the center coordinates, the x-axis, and y-axis radius.
function svg_line(
  $x1, $y1, $x2, $y2,
This function builds an SVG <line> tag using the (x1, y1), (x2 ,y2) coordinates and some drawing-style parameters.
function svg_polygon(
This function builds an SVG <polygon> tag using an array of (x,y) points. The polygon is closed automatically after the last point is specified.
function svg_polyline(
  $points, $fill="none,",
Much like a polygon, this function creates an SVG <polyline> tag using an array of (x,y) coordinates and some drawing-style parameters.
function svg_rect(
  $x, $y, $width,
This function creates an SVG <rect> tag using the specified left upper-corner coordinate and the width and height values.
function svg_text(
  $x, $y, $rotate=NULL,
This function builds an SVG <text> tag in which text will be placed at the specified point, rotated to the specified number of degrees.
function svg_g(
This function builds an SVG <g> (graphic container) tag using the specified style and transform parameters.

Thanks for your registration, follow us on our social networks to keep up-to-date