cvs: pear /XML_svg2image README.svg2image gvt.svg package.xml svg2image.php

From: Date: Fri, 14 Sep 2001 13:39:43 +0000
Subject: cvs: pear /XML_svg2image README.svg2image gvt.svg package.xml svg2image.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-845@lists.php.net to get a copy of this message
chregu Fri Sep 14 09:39:43 2001 EDT Added files: /pear/XML_svg2image README.svg2image gvt.svg package.xml svg2image.php Log: a class to convert svg files to png/jpeg images with the help of batik from the xml-apache project. not very fast stuff... see README.svg2image for details. - tested with batik 1.0 - has almost the same api as XML_fo2pdf

Index: pear/XML_svg2image/README.svg2image +++ pear/XML_svg2image/README.svg2image svg to image (jpeg and png) converter. with svg2image (svg = scalabe vector graphics, a xml standard defined by the w3c) it's fairly easy to convert svg files into rasterized formats like png and jpeg. Since not all web browsers support svg-graphics and not all platforms have an svg-plugins, this class can be useful for converting this graphics into formats everyone supports. An introduction into formating objects can be found at http://www.w3.org/Graphics/SVG/Overview.htm8 http://www.adobe.com/svg/main.html A tutorial is here: http://www.adobe.com/svg/tutorial/intro.html This class uses Batik from the apache consortium. It can be found here: http://xml.apache.org/batik Before you read further, some warnings: - Batik is written in Java, therefore you need ext/java support for php. This can be a quite painful task to get it really running. Some articles: http://www.phpbuilder.com/columns/marknold20001221.php3 or http://www.linuxwebdevnews.com/articles/php-java-xslt.php?pid=347 Furthermore the whole java stuff is quite ressourcehungry in php. - The whole thing seems to be quite slow. it needs on my not very slow machine several seconds to generate an image. Ypu better don't use it for online-production of the image files, use some caching instead. - Batik says, it recommends jdk 1.3.1 for it. Unfortunately I was only able to get 1.2.2 running with php on my linux box. According to them, it should run faster on 1.3.1 (which it really does on the commandline), nevertheless it works with 1.2.2 as well. - The programm needs an XServer (this is a limitation of the graphics-support in java. See http://www.geocities.com/marcoschmidt.geo/java-image-coding.html for details). Normally webservers don't have Xservers installed, but I made some good experiences with Xfvb. Requirements: You need Batik (version 1.0 was used for testing) from the xml-apache project (http://xml.apache.org/batik) and Java (1.1.x or later, i tested it with 1.2.2 from sun on linux, see the Batik-Docs for details). Furthermore you have to compile your php with --with-java and to adjust your php.ini file. My php.ini file looks the following: *** extension=libphp_java.so extension.dir=/usr/local/lib/php/20010901/ [java] java.class.path=/usr/local/lib/php/php_java.jar:/usr/local/share/java/fop.jar:/usr/local/share/java/batik.jar:/usr/share/java/xalan-2.0.1.jar:/usr/share/java/xerces.jar:/usr/local/share/java/jimi-1.0.jar:/usr/share/java/repository/ java.library=/usr/lib/java/jre/lib/i386/libjava.so *** Using this class: Produce a svg-file from a xml-file with a xsl-stylesheet (or with some other editors), then feed this class with this svg-file and you get a png/jpeg back (either directly to the browser for really dynamic image production or as a file on your filesystem) It is recommended to use the Cache-Classes from PEAR, if you want dynamic image creation, since the process of making the images takes some time. For an example of how to use Cache and svg2image see below. See also the source code for api-documentation. Usage: require_once("XML/svg2image.php"); //make a pdf from simple.fo and save the pdf in a tmp-folder $batik = new xml_svg2image(); // the following line are the default settins, so not // necessary here, but you can set it to other values $batik->setRenderer("png"); // you can set some parameters for the file, not necessary $batik->setImageHeight(100); $batik->setImageWidth(100); $batik->setImageQuality(0.8); if (PEAR::isError($error = $batik->run("gvt.svg"))) { die("batik ERROR: ". $error->getMessage()); } //print image to the outputbuffer, // including correct Header ("Content-type: application/pdf") $batik->printImage(); //delete the temporary image file $batik->deleteImage(); With Cache: require_once("XML/svg2image.php"); require_once("Cache/Output.php"); $container = "file"; $options = array("cache_dir"=>"/tmp/"); $cache = new Cache_Output("$container",$options); $cache_handle = $cache->generateID($REQUEST_URI); if ($content = $cache->start($cache_handle)) { Header("Content-type: image/png"); print $content; die(); } $batik = new xml_svg2image(); if (PEAR::isError($error = $batik->run("gvt.svg"))) { die("batik ERROR: ". $error->getMessage()); } $batik->printImage(); $batik->deleteImage(); print $cache->end("+30"); If you want to save the image in a file just give the file-name/path as a second argument to the run() method: $batik->run("gvt.svg","image.png"); If you have any questions to this class or just want to thank me for the great work (*g*), just drop me a mail (chregu@php.net). Index: pear/XML_svg2image/gvt.svg +++ pear/XML_svg2image/gvt.svg <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000802//EN" "http://www.w3.org/TR/2000/CR-SVG-20000802/DTD/svg-20000802.dtd"> <!-- ========================================================================= --> <!-- Copyright (C) The Apache Software Foundation. All rights reserved. --> <!-- --> <!-- This software is published under the terms of the Apache Software License --> <!-- version 1.1, a copy of which has been included with this distribution in --> <!-- the LICENSE file. --> <!-- ========================================================================= --> <!-- ========================================================================= --> <!-- Top level architecture diagram --> <!-- --> <!-- @author vincent.hardy@eng.sun.com --> <!-- @version $Id: gvt.svg,v 1.1 2001/09/14 13:39:43 chregu Exp $ --> <!-- ========================================================================= --> <svg width="450" height="500" viewBox="0 0 450 500"> <defs> <path id="G" transform="translate(-38.28, -32.5)" d="M48,47.5H36.875V30.75h39.5c0.064,1.465,0.113,2.555,0.146,3.27c0.031,0.715,0.049,1.35,0.049,1.902c0,4.098-0.393,7.838-1.174,11.221s-1.922,6.375-3.42,8.977c-3.648,6.115-8.357,10.791-14.123,14.027 C52.088,73.381,45.539,75,38.211,75c-5.408,0-10.434-0.904-15.074-2.709c-4.643-1.807-8.756-4.469-12.338-7.984c-3.551-3.516-6.238-7.535-8.063-12.061C0.912,47.723,0,42.807,0,37.5c0-5.338,0.92-10.277,2.76-14.818s4.521-8.537,8.041-11.988 c3.551-3.482,7.656-6.135,12.316-7.959C27.777,0.912,32.811,0,38.221,0c7.33,0,13.846,1.605,19.549,4.814s10.363,7.908,13.98,14.096l-22.967,8.465c-1.205-1.973-2.654-3.445-4.346-4.418c-1.693-0.971-3.631-1.457-5.811-1.457c-3.811,0-6.848,1.447-9.109,4.338 c-2.262,2.893-3.393,6.775-3.393,11.648c0,5.037,1.139,8.945,3.418,11.723c2.277,2.777,5.5,4.166,9.668,4.166c2.473,0,4.482-0.518,6.029-1.553S47.707,49.346,48,47.5z"/> <path id="V" style="stroke:none;" transform="translate(-39.1625, -35.75)" d="M27.271,71.5L0,0h27.807l7.566,25.885c1.105,3.777,1.928,6.969,2.465,9.574c0.535,2.605,0.885,5.047,1.049,7.324h0.488c0.324-2.408,0.789-4.947,1.391-7.617s1.375-5.568,2.32-8.695L51.09,0h27.285L51.188,71.5H27.271z"/> <path id="T" style="stroke:none;" transform="translate(-27.0625, -35.75)" d="M14.25,71.5V23.375H0V0h54.125v23.375h-14.25V71.5H14.25z"/> <filter id="dropShadow" width="1.5" height="1.5"> <feGaussianBlur in="SourceGraphic" stdDeviation="4 4"/> <feOffset dx="4" dy="4" /> </filter> <filter id="emboss" > <feGaussianBlur in="SourceAlpha" stdDeviation="6" result="blur"/> <feSpecularLighting in="blur" surfaceScale="-3" specularConstant="1" specularExponent="20" lighting-color="white" result="spec" kernelUnitLength="1" > <feDistantLight azimuth="45" elevation="60" /> </feSpecularLighting> <feComposite in="spec" in2="SourceGraphic" operator="in" result="specOut"/> <feComposite in="SourceGraphic" in2="specOut" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" result="litPaint2"/> </filter> <radialGradient id="redGradient" cx="0" cy="0" r="2" fx="-0.2" fy="-0.2" > <stop offset="0" stop-color="red" /> <stop offset="1" stop-color="black" /> </radialGradient> <radialGradient id="greenGradient" cx="0" cy="0" r="2" fx="-0.2" fy="-0.2" > <stop offset="0" stop-color="rgb(60, 255, 160)" /> <stop offset="1" stop-color="black" /> </radialGradient> <radialGradient id="blueGradient" cx="1" cy="1" r="2"> <stop offset="0" stop-color="rgb(160, 60, 255)" /> <stop offset="1" stop-color="black" /> </radialGradient> <radialGradient id="goldGradient" cx="1" cy="1" r="2"> <stop offset="0" stop-color="gold" /> <stop offset="1" stop-color="black" /> </radialGradient> </defs> <g fill="url(#redGradient)"> <rect width="150" height="125" /> <rect width="150" height="125" x="150" /> <rect width="150" height="125" x="300" /> <g transform="translate(75, 62.5)" fill="gold"> <use xlink:href="#G" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#G" filter="url(#emboss)"/> <use xlink:href="#V" x="150" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#V" x="150" filter="url(#emboss)"/> <use xlink:href="#T" x="300" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#T" x="300" filter="url(#emboss)"/> </g> </g> <g fill="url(#greenGradient)" transform="translate(0, 125)"> <rect width="150" height="125" /> <rect width="150" height="125" x="150" /> <rect width="150" height="125" x="300" /> <g transform="translate(75, 62.5)" fill="rgb(130, 30, 225)"> <use xlink:href="#G" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#G" filter="url(#emboss)"/> <use xlink:href="#V" x="150" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#V" x="150" filter="url(#emboss)"/> <use xlink:href="#T" x="300" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#T" x="300" filter="url(#emboss)"/> </g> </g> <g fill="url(#blueGradient)" transform="translate(0, 250)"> <rect width="150" height="125" /> <rect width="150" height="125" x="150" /> <rect width="150" height="125" x="300" /> <g transform="translate(75, 62.5)" fill="rgb(10, 205, 110)"> <use xlink:href="#G" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#G" filter="url(#emboss)"/> <use xlink:href="#V" x="150" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#V" x="150" filter="url(#emboss)"/> <use xlink:href="#T" x="300" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#T" x="300" filter="url(#emboss)"/> </g> </g> <g fill="url(#goldGradient)" transform="translate(0, 375)"> <rect width="150" height="125" /> <rect width="150" height="125" x="150" /> <rect width="150" height="125" x="300" /> <g transform="translate(75, 62.5)" fill="rgb(220, 0, 0)"> <use xlink:href="#G" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#G" filter="url(#emboss)"/> <use xlink:href="#V" x="150" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#V" x="150" filter="url(#emboss)"/> <use xlink:href="#T" x="300" fill="black" filter="url(#dropShadow)"/> <use xlink:href="#T" x="300" filter="url(#emboss)"/> </g> </g> </svg> Index: pear/XML_svg2image/package.xml +++ pear/XML_svg2image/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package> <name>XML_svg2image</name> <summary>Converts a svg file to a png/jpeg image with the help of apache-batik (java-program), needs therefore a php with ext/java compiled-in and the batik files from http://xml.apache.org/batik</summary> <maintainer> <initials>chregu</initials> <name>Christian Stocker</name> <email>chregu@php.net</email> </maintainer> <release> <version>0.1</version> <date>2001-09-14</date> <notes> - tested with batik 1.0 - has almost the same api as XML_fo2pdf </notes> </release> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">svg2image.php</file> <file role="doc">README.svg2image</file> <file role="doc">gvt.svg</file> </dir> </filelist> </package> Index: pear/XML_svg2image/svg2image.php +++ pear/XML_svg2image/svg2image.php <?php // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // // $Id: svg2image.php,v 1.1 2001/09/14 13:39:43 chregu Exp $ /** * svg to image converter. * * see README.svg2image for details, examples and warnings ;) * * @author Christian Stocker <chregu@nomad.ch> * @version $Id: svg2image.php,v 1.1 2001/09/14 13:39:43 chregu Exp $ * @package XML */ require_once( "PEAR.php") ; class XML_svg2image { /** * xsl-file used in this class * * @var string */ var $svg = ""; /** * image-file used in this class * * @var string */ var $image = ""; /** * Where the temporary xsl and image files should be stored * * @var string */ var $tmpdir = "/tmp"; /** * A prefix for the temporary files * * @var string */ var $tmpimageprefix = "svg"; /** * the render Type. At the moment (batik 1.0), possible values are * - png * - jpeg * * @var string */ var $renderer = "png"; /** * the content-type to be sent if printimage is called. * * @var contenttype " @see printimage() */ var $contenttype = "image/png"; /** * the width of the output image, if not set, default values are taken * * @var imageWidth " @see setImageWidth */ var $imageWidth = False; /** * the height of the output image, if not set, default values are taken * * @var imageHeight " @see setImageHeight */ var $imageHeight = False; /** * the quality of the output image, makes only sense with jpegs * * @var imageQuality " @see setImageQuality */ var $imageQuality = 0.8; /** * constructor * * @access public */ function xml_svg2image () { } /** * Calls the Main Batik-Transcoder-Java-Programm * * One has to pass an input svg-file * and if the image should be stored permanently, a filename/path for * the image. * if the image is not passed or empty/false, a temporary image-file * will be created * * @param string $svg file input svg-file * @param string $image file output image-file * @param boolean $DelSvg if the svg should be deleted after execution * @see runFromString() */ function run($svg, $image = "", $DelSvg = False) { if (!$image) $image = tempnam($this->tmpdir, $this->tmpimageprefix); $this->image = $image; $this->svg = $svg; $options = array( $this->svg,"-".$this->renderer,$this->image); switch ($this->renderer) { case "jpeg": $t = new Java("org.apache.batik.transcoder.image.JPEGTranscoder"); $q = new Java ("java.lang.Float",$this->imageQuality); $t->addTranscodingHint($t->KEY_QUALITY,$q); break; default: $t = new Java("org.apache.batik.transcoder.image.PNGTranscoder"); $this->contenttype = "image/png"; } // set the transcoding hints $t->addTranscodingHint($t->KEY_XML_PARSER_CLASSNAME,"org.apache.crimson.parser.XMLReaderImpl"); if ($this->imageWidth) { $width = new Java("java.lang.Float",$this->imageWidth); $t->addTranscodingHint($t->KEY_WIDTH, $width); } if ($this->imageHeight) { $height = new Java("java.lang.Float",$this->imageHeight); $t->addTranscodingHint($t->KEY_HEIGHT, $height); } if ($exc = java_last_exception_get()) { java_last_exception_clear(); return new PEAR_Error($exc->getMessage() ." in ". __FILE__ .":". __LINE__, 11, PEAR_ERROR_RETURN, null, null ); } // create the transcoder input $svgURI = new Java ("java.io.File",$this->svg); $svgURI = $svgURI->toURL(); $svgURI = $svgURI->toString(); $input = new Java ("org.apache.batik.transcoder.TranscoderInput",$svgURI); // create the transcoder output $ostream = @new Java ("java.io.FileOutputStream",$this->image);; if ($exc = java_last_exception_get()) { java_last_exception_clear(); return new PEAR_Error($exc->getMessage() ." in ". __FILE__ .":". __LINE__, 11, PEAR_ERROR_RETURN, null, null ); } $output = new Java("org.apache.batik.transcoder.TranscoderOutput",$ostream); // save the image @$t->transcode($input, $output); if ($exc = java_last_exception_get()) { java_last_exception_clear(); return new PEAR_Error($exc->getMessage() ." in ". __FILE__ .":". __LINE__, 11, PEAR_ERROR_RETURN, null, null ); } // flush and close the stream then exit $ostream->flush(); $ostream->close(); if ($exc = java_last_exception_get()) { java_last_exception_clear(); return new PEAR_Error($exc->getMessage() ." in ". __FILE__ .":". __LINE__, 11, PEAR_ERROR_RETURN, null, null ); } if ($DelSvg) { $this->deleteSvg($svg); } return True; } /** * If the svg is a string, not a file, use this. * * If you generate the svg dynamically (for example with a * xsl-stylesheet), you can use this method * * The Batik-Java program needs a file as an input, so a * temporary svg-file is created here (and will be deleted * in the run() function.) * * @param string $svgstring svg input svg-string * @param string $image file output image-file * @see run() */ function runFromString($svgstring, $image = "") { $svg = tempnam($this->tmpdir, $this->tmpimageprefix); $fp = fopen($svg, "w+"); fwrite($fp, $svgstring); fclose($fp); return $this->run($svg, $image, True); } /** * A wrapper to run for better readabilty * * This method just calls run.... * * @param string $svg svg input svg-string * @param string $image file output image-file * @see run() */ function runFromFile($svg, $image = "") { return $this->run($svg, $image); } /** * Deletes the created image * * If you dynamically create images and you store them * for example in a Cache, you don't need it afterwards. * If no image is given, the one generated in run() is deleted * * @param string $image file output image-file * @access public */ function deleteImage($image = "") { if (!$image) $image = $this->image; unlink ($image); } /** * Deletes the created svg * * If you dynamically create svgs, you don't need it afterwards. * If no svg-file is given, the one generated in run() is deleted * * @param string $svg file input svg-file */ function deleteSvg($svg = "") { if (!$svg) $svg = $this->svg; unlink ($svg); } /** * Prints the content header and the generated image to the output * * If you want to dynamically generate images and return them directly * to the browser, use this. * If no image-file is given, the generated from run() is taken. * * @param string $image file output image-file * @see returnimage() * @access public */ function printImage($image = "") { $image = $this->returnImage($image); Header("Content-type: ".$this->contenttype."\nContent-Length: " . strlen($image)); print $image; } /** * Returns the image * * If no image-file is given, the generated from run() is taken. * * @param string $image file output image-file * @return string image * @see run() */ function returnImage($image = "") { if (!$image) $image = $this->image; $fd = fopen($image, "r"); $content = fread( $fd, filesize($image) ); fclose($fd); return $content; } /** * sets the rendertype * * @param string $renderer the type of renderer which should be used * @param string $overwriteContentType if the contentType should be set to a approptiate one * @see $this-renderer * @access public */ function setRenderer($renderer = "png",$overwriteContentType = True) { $this->renderer = $renderer; if ($overwriteContentType) { switch ($renderer) { case "jpeg": $this->contenttype = "image/jpeg"; break; case "png": $this->contenttype = "image/png"; break; } } } /** * sets the content-type * * @param string $contenttype the content-type for the http-header * @see $contenttype * @access public */ function setContentType($contenttype = "image/png") { $this->contenttype = $contenttype; } /** * sets the width of the output image * * @param string $width the width of the image in pixel * @see $setImageWidth * @access public */ function setImageWidth($width) { $this->imageWidth = $width; } /** * sets the height of the output image * * @param string $height the height of the image in pixel * @see $setImageHeight * @access public */ function setImageHeight($height) { $this->imageHeight = $height; } /** * sets the quality of the output image * * @param string $quality the quality of the image in percent (max = 1 min = 0) * @see $setImageQuality * @access public */ function setImageQuality($quality) { $this->imageQuality = $quality; } } ?>
« previous php.pear.cvs (#845) next »