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

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