UP PREVIOUS NEXT   Technologies Internet et Education, © TECFA
  3. PHP-SVG avec des librairies SVG

3. PHP-SVG avec des librairies SVG

Travailler avec une libraire améliore la flexibilité et le coût de maintenance de votre code. Si vous ne trouvez rien qui vous convient, faites la vôtre (au moins quelques fonctions utiles pour les opérations répétitives).

3.1 phpHtmlLib

phpHtmlLib est une très grande librairie à fonctions multiples

Exemple 3-1: Exemple SVG-phpHtmlLib simple

$phphtmllib = $_SERVER["DOCUMENT_ROOT"] . "/lib/php/phphtmllib";
include_once("$phphtmllib/includes.inc");
include_once($phphtmllib."/widgets/svg/SVGDocumentClass.inc");
 
//0. Create a page object with dimensions
$svgpage = new SVGDocumentClass(800,600);
 
//1. add a nice rectangle 
$rect = svg_rect(50,50,600,400,"none","black",3);
 
//2. add polyline
$line = "50,375 150,375 150,325 250,325 250,375 350,375 350,250 450,250   450,375 550,375 550,175 600,175";
$zoliline = svg_polyline($line,"none","blue","2");
 
//3. Add an ellipse
$oeuf = svg_ellipse(300,300,200,100,"red","black","1");
 
//4. Add these objects to the page object
$svgpage->add($rect);
$svgpage->add($oeuf);
$svgpage->add($zoliline);
 
//5. render the whole thing
print $svgpage->render();

Exemple 3-2: Exemple SVG-phpHtmlLib SVGXYLineGraph

.....
$width = 500;
$height = 500;
 
$svgdoc = new SVGDocumentClass("100%","100%");
$graph = new SVGXYLineGraph("Popularity/usage/activity of TECFA's PHP examples", $width,$height);
$graph->set_x_title("X-Axis Year");
$graph->set_y_title("Popularity");
 
//add three lines with different colors
$graph->add_line("0,1,2.3,4.2,6,8", "1,2,2.7,0.3,6,1", "red");
$graph->add_line("0,1,4.1,6", "0,4,2,3", "blue");
$graph->add_line("0,1,2,3,4,5,7", "0,4,3,1,7,8,10", "black");
 
//add the line graph widget to the document.
$svgdoc->add( $graph );
 
print $svgdoc->render();

3.2 SVG Class de Killian

Exemple 3-3: Exemple d'animation avec SVG class

// *** Define the path to the SVG class dir. ***
 
define("SVG_CLASS_BASE", "./ori/");
// Include the class files.
require_once(SVG_CLASS_BASE."Svg.php");
 
// Create an instance of SvgDocument. All other objects will be added to this
// instance for printing. Also set the height and width of the viewport.
$svg =& new SvgDocument("400", "400");
 
// Create an instance of SvgGroup.
// Set the style, transforms for child objects.
$g =& new SvgGroup("stroke:black", "translate(200 100)");
 
// Add a parent to the g instance.
$g->addParent($svg);
 
 
// Create and animate a circle.
$circle = new SvgCircle("0", "0", "100", "stroke-width:3", "");
$circle->addChild(new SvgAnimate("r", "XML", "0", "75", "", "3s", "freeze"));
$circle->addChild(new SvgAnimate("fill", "CSS", "green", "red", "", "3s",
                                 "freeze"));
 
// Once the circle is created move it right and down // DKS addition
$circle->addChild(new SvgAnimate("cx", "XML", "", "200", "4s", "3s", "freeze"));
$circle->addChild(new SvgAnimate("cy", "XML", "", "200", "4s", "3s", "freeze"));
 
// Make the circle a child of g.
$g->addChild($circle);
 
 
// Create and animate some text.
$text = new SvgText("0", "0", "SVG is cool",
                    "font-size:20;text-anchor:middle;", "");
$text->addChild(new SvgAnimate("font-size", "auto", "0", "20", "", "3s",
                               "freeze"));
 
// Make the text a child of g.
$g->addChild($text);
 
// Send a message to the svg instance to start printing.
$svg->printElement()

UP PREVIOUS NEXT -- TIE