Image_3D
[ class tree: Image_3D ] [ index: Image_3D ] [ all elements ]

Source for file SVG.php

Documentation is available at SVG.php

  1. <?php
  2.  
  3. class Image_3D_Driver_SVG extends Image_3D_Driver {
  4.     
  5.     protected $_x;
  6.     protected $_y;
  7.     
  8.     protected $_id;
  9.     
  10.     protected $_gradients;
  11.     protected $_polygones;
  12.     
  13.     public function __construct({
  14.         $this->_image '';
  15.         $this->_id = 1;
  16.         $this->_gradients = array();
  17.         $this->_polygones = array();
  18.     }
  19.     
  20.     public function createImage($x$y{
  21.         $this->_x = (int) $x;
  22.         $this->_y = (int) $y;
  23.  
  24.         $this->_image = <<<EOF
  25. <?xml version="1.0" ?>
  26. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
  27.  
  28. <svg xmlns="http://www.w3.org/2000/svg" width="{$this->_x}" height="{$this->_y}" x="0" y="0">
  29. EOF;
  30.         $this->_image .= "\n\n";
  31.     }
  32.     
  33.     public function setBackground(Image_3D_Color $color{
  34.         $this->_addPolygon(sprintf("\t<polygon id=\"background%d\" points=\"0,0 %d,0 %d,%d 0,%d\" style=\"%s\" />\n"
  35.             $this->_id++,
  36.             $this->_x,
  37.             $this->_x,
  38.             $this->_y,
  39.             $this->_y,
  40.             $this->_getStyle($color)));
  41.     }
  42.     
  43.     protected function _getStyle(Image_3D_Color $color{
  44.         $values $color->getValues();
  45.         
  46.         $values[0= (int) round($values[0* 255);
  47.         $values[1= (int) round($values[1* 255);
  48.         $values[2= (int) round($values[2* 255);
  49.         $values[3= 1 - $values[3];
  50.         
  51.         return sprintf('fill: #%02x%02x%02x; fill-opacity: %.2f; stroke: none;'$values[0]$values[1]$values[2]$values[3]);
  52.     }
  53.     
  54.     protected function _getStop(Image_3D_Color $color$offset = 0$alpha = null{
  55.         $values $color->getValues();
  56.         
  57.         $values[0= (int) round($values[0* 255);
  58.         $values[1= (int) round($values[1* 255);
  59.         $values[2= (int) round($values[2* 255);
  60.         if ($alpha === null{
  61.             $values[3= 1 - $values[3];
  62.         else {
  63.             $values[3= 1 - $alpha;
  64.         }
  65.         
  66.         return sprintf("\t\t\t<stop id=\"stop%d\" offset=\"%.1f\" style=\"stop-color: rgb(%d, %d, %d); stop-opacity: %.4f;\" />\n"$this->_id++$offset$values[0]$values[1]$values[2]$values[3]);
  67.     }
  68.     
  69.     protected function _addGradient($string{
  70.         $id 'linearGradient' $this->_id++;
  71.         $this->_gradients[str_replace('[id]'$id$string);
  72.         return $id;
  73.     }
  74.     
  75.     protected function _addPolygon($string{
  76.         $id 'polygon' $this->_id++;
  77.         $this->_polygones[str_replace('[id]'$id$string);
  78.         return $id;
  79.     }
  80.     
  81.     public function drawPolygon(Image_3D_Polygon $polygon{
  82.         
  83.         $list '';
  84.         $points $polygon->getPoints();
  85.         foreach ($points as $point{
  86.             $pointarray $point->getScreenCoordinates();
  87.             $list .= sprintf('%.2f,%.2f '$pointarray[0]$pointarray[1]);
  88.         }
  89.         
  90.         $this->_addPolygon(sprintf("\t<polygon points=\"%s\" style=\"%s\" />\n"
  91.             substr($list0-1)
  92.             $this->_getStyle($polygon->getColor())));
  93.     }
  94.     
  95.     public function drawGradientPolygon(Image_3D_Polygon $polygon{
  96.         $points $polygon->getPoints();
  97.         
  98.         $list '';
  99.         $pointarray = array();
  100.         foreach ($points as $nr => $point{
  101.             $pointarray[$nr$point->getScreenCoordinates();
  102.             $list .= sprintf('%.2f,%.2f '$pointarray[$nr][0]$pointarray[$nr][1]);
  103.         }
  104.         
  105.         // Groessen
  106.         $xOffset min($pointarray[0][0]$pointarray[1][0]$pointarray[2][0]);
  107.         $yOffset min($pointarray[0][1]$pointarray[1][1]$pointarray[2][1]);
  108.  
  109.         $xSize max(abs($pointarray[0][0$pointarray[1][0])abs($pointarray[0][0$pointarray[2][0])abs($pointarray[1][0$pointarray[2][0]));
  110.         $ySize max(abs($pointarray[0][1$pointarray[1][1])abs($pointarray[0][1$pointarray[2][1])abs($pointarray[1][1$pointarray[2][1]));
  111.         
  112.         // Base Polygon
  113.         $lg $this->_addGradient(sprintf("\t\t<linearGradient id=\"[id]\" x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\">\n%s\t\t</linearGradient>\n",
  114.             ($pointarray[0][0$xOffset$xSize,
  115.             ($pointarray[0][1$yOffset$ySize,
  116.             ($pointarray[1][0$xOffset$xSize,
  117.             ($pointarray[1][1$yOffset$ySize,
  118.             $this->_getStop($points[0]->getColor()) $this->_getStop($points[1]->getColor()1)));
  119.         
  120.         $this->_addPolygon(sprintf("\t<polygon id=\"[id]\" points=\"%s\" style=\"fill: url(#%s); stroke: none; fill-opacity: 1;\" />\n"$list$lg));
  121.         
  122.         // Overlay Polygon
  123.         $lg $this->_addGradient(sprintf("\t\t<linearGradient id=\"[id]\" x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\">\n%s\t\t</linearGradient>\n"
  124.             ($pointarray[2][0$xOffset$xSize,
  125.             ($pointarray[2][1$yOffset$ySize,
  126.             ((($pointarray[0][0$pointarray[1][0]/ 2$xOffset$xSize,
  127.             ((($pointarray[0][1$pointarray[1][1]/ 2$yOffset$ySize,
  128.             $this->_getStop($points[2]->getColor()) $this->_getStop($points[2]->getColor()11)));
  129.         
  130.         $this->_addPolygon(sprintf("\t<polygon id=\"[id]\" points=\"%s\" style=\"fill: url(#%s); stroke: none; fill-opacity: 1;\" />\n"$list$lg));
  131.     }
  132.     
  133.     public function save($file{
  134.         $this->_image .= sprintf("\t<defs id=\"defs%d\">\n"$this->_id++);
  135.         $this->_image .= implode(''$this->_gradients);
  136.         $this->_image .= sprintf("\t</defs>\n\n");
  137.  
  138.         $this->_image .= implode(''$this->_polygones);
  139.         $this->_image .= "</svg>\n";
  140.         file_put_contents($file$this->_image);
  141.     }
  142.  
  143.     public function getSupportedShading({
  144.         return array(Image_3D_Renderer::SHADE_NOImage_3D_Renderer::SHADE_FLATImage_3D_Renderer::SHADE_GAUROUD);
  145.     }
  146. }
  147.  
  148. ?>

Documentation generated on Mon, 11 Mar 2019 14:17:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.