+ /**
+ * Based on image specification, write a very simple SVG file to disk.
+ * Ignores the background spec because transparency is cool. :)
+ * @param $spec: spec describing background and circles to draw
+ * @param $format: file format to write (which is obviously always svg here)
+ * @param $filename: filename to write to
+ */
+ public function writeSvg( $spec, $format, $filename ) {
+ $svg = new SimpleXmlElement( '<svg/>' );
+ $svg->addAttribute( 'xmlns', 'http://www.w3.org/2000/svg' );
+ $svg->addAttribute( 'version', '1.1' );
+ $svg->addAttribute( 'width', $spec['width'] );
+ $svg->addAttribute( 'height', $spec['height'] );
+ $g = $svg->addChild( 'g' );
+ foreach ( $spec['draws'] as $drawSpec ) {
+ $circle = $g->addChild( 'circle' );
+ $circle->addAttribute( 'fill', $drawSpec['fill'] );
+ $circleSpec = $drawSpec['circle'];
+ $circle->addAttribute( 'cx', $circleSpec['originX'] );
+ $circle->addAttribute( 'cy', $circleSpec['originY'] );
+ $circle->addAttribute( 'r', $circleSpec['radius'] );
+ };
+ if ( ! $fh = fopen( $filename, 'w' ) ) {
+ throw new Exception( "couldn't open $filename for writing" );
+ }
+ fwrite( $fh, $svg->asXML() );
+ if ( !fclose($fh) ) {
+ throw new Exception( "couldn't close $filename" );
+ }
+ }
+