advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
PEAR::Image_Canvas Methods
Partners & Affiliates
advertisement
advertisement
Rate this item | 0 users have rated this item.
 Print Print
 
Add the Graphics Power of SVG to PHP
Discover how to generate and manipulate SVG documents dynamically—directly from PHP. 

advertisement
calable Vector Graphics (SVG) is an XML specification for producing vector-based graphic content that you can create, modify, and manipulate dynamically from programming languages. Here, you'll explore the most important techniques and libraries for integrating SVG with PHP to create diverse graphic content—from simple shapes to complex graphs. You'll see how to:

  • Convert an SVG document into a PHP document.
  • Use the object and embed elements to include an SVG document in an XHTML document.
  • Generate SVG using PHP's echo command.
  • Generate SVG using the phpHtmlLib library.
  • Generate SVG documents using the PEAR::XML_SVG package.
  • Generate SVG documents using the PEAR::Image_Canvas package.
  • Integrate PHP , SVG, and AJAX.
Three Simple Techniques
To get started, here are three of the simplest techniques for using SVG in PHP.

  1. You can convert any SVG document into a PHP document by replacing the .svg extension with the .php extension and specifying the image/svg+xml MIME type using the header() function:
  2.    <?php
           header("Content-type: image/svg+xml");
       ?>
       <?xml version="1.0" encoding="iso-8859-1"?>
       <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
         "http://www.w3.org/TR/2001/
          REC-SVG-20010904/DTD/svg10.dtd">
       <svg width="310" height="140" 
         xmlns="http://www.w3.org/2000/svg" 
         xmlns:xlink="http://www.w3.org/1999/xlink">
         <g style="stroke:black;fill:lightgreen" 
            transform="translate(30,30)">
           <rect x="10" y="10" width="100" height="30" 
             style="stroke-width:4"/>
           <circle cx="170" cy="25" r="20" 
             style="stroke-width:4"/>
           <line x1="265" y1="10" x2="200" y2="70" 
             style="stroke-width:4"/>
           <text x="80" y="90" style="font:size: 8">
             Basic shapes</text>
         </g>
       </svg>
    
  3. You can include an SVG document (saved with the .php extension) into an XHTML document, through the object or embed element:
  4.    <object type="image/svg+xml" width="500" 
         height="400" 
         data="svg.php" 
         pluginspage="http://www.adobe.com/svg/viewer/install/">
       </object>
       
       <embed type="image/svg+xml" width="500" 
         height="400" src="svg.php" 
         pluginspage=
           "http://www.adobe.com/svg/viewer/install/">
       </embed>
    
  5. You can generate an SVG document dynamically using the echo command:
  6.    <?php
         header("Content-type: image/svg+xml");
         echo '<?xml version="1.0" encoding="iso-8859-1"?>';
         echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
           "http://www.w3.org/TR/2001/
            REC-SVG-20010904/DTD/svg10.dtd">';
         echo '<svg width="310" height="140" 
           xmlns="http://www.w3.org/2000/svg"         
           xmlns:xlink="http://www.w3.org/1999/xlink">';
         echo '<g style="stroke:black;fill:lightgreen" 
           transform="translate(30,30)">';
         echo '<rect x="10" y="10" width="100" 
           height="30" style="stroke-width:4"/>';
         echo '<circle cx="170" cy="25" r="20" 
           style="stroke-width:4"/>';
         echo '<line x1="265" y1="10" x2="200" y2="70" 
           style="stroke-width:4"/>';
         echo '<text x="80" y="90" style="font:size: 8">
           Basic shapes</text>';
         echo '</g>';
         echo '</svg>';
       ?>
    
To go beyond these techniques, it's best to download some helper packages that provide SVG support—classes and methods that simplify working with, generating, and modifying SVG. The rest of this article explains how to obtain and use these packages.

Page 1 of 5
advertisement
  Next Page: Generating SVG with phpHtmlLib
Page 1: IntroductionPage 4: Generating SVG with PEAR::XML_SVG
Page 2: Generating SVG with phpHtmlLibPage 5: Generating SVG with PEAR::Image_Canvas
Page 3: Drawing Basic Shapes 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES