1<?php
2
3class Image_3D_Driver_GD extends Image_3D_Driver
4{
5
6    protected $_filetype;
7
8    public function __construct()
9    {
10        parent::__construct();
11
12        $this->_filetype = 'png';
13    }
14
15    public function createImage($x, $y)
16    {
17        $this->_image = imagecreatetruecolor($x, $y);
18    }
19
20    protected function _getColor(Image_3D_Color $color)
21    {
22        $values = $color->getValues();
23
24        $values[0] = (int) round($values[0] * 255);
25        $values[1] = (int) round($values[1] * 255);
26        $values[2] = (int) round($values[2] * 255);
27        $values[3] = (int) round($values[3] * 127);
28
29        if ($values[3] > 0) {
30            // Tranzparente Farbe allokieren
31            $color = imageColorExactAlpha($this->_image, $values[0], $values[1], $values[2], $values[3]);
32            if ($color === -1) {
33                // Wenn nicht Farbe neu alloziieren
34                $color = imageColorAllocateAlpha($this->_image, $values[0], $values[1], $values[2], $values[3]);
35            }
36        } else {
37            // Deckende Farbe allozieren
38            $color = imageColorExact($this->_image, $values[0], $values[1], $values[2]);
39            if ($color === -1) {
40                // Wenn nicht Farbe neu alloziieren
41                $color = imageColorAllocate($this->_image, $values[0], $values[1], $values[2]);
42            }
43        }
44
45        return $color;
46    }
47
48    public function setBackground(Image_3D_Color $color)
49    {
50        $bg = $this->_getColor($color);
51        imagefill($this->_image, 1, 1, $bg);
52    }
53
54    public function drawPolygon(Image_3D_Polygon $polygon)
55    {
56        // Get points
57        $points = $polygon->getPoints();
58        $coords = array();
59        foreach ($points as $point) {
60            $coords = array_merge($coords, $point->getScreenCoordinates());
61        }
62        $coordCount = (int) (count($coords) / 2);
63
64        if (true) {
65            imageFilledPolygon($this->_image, $coords, $coordCount, $this->_getColor($polygon->getColor()));
66        } else {
67            imagePolygon($this->_image, $coords, $coordCount, $this->_getColor($polygon->getColor()));
68        }
69
70    }
71
72    public function drawGradientPolygon(Image_3D_Polygon $polygon)
73    {
74        $this->drawPolygon($polygon);
75    }
76
77    public function setFiletye($type)
78    {
79        $type = strtolower($type);
80        if (in_array($type, array('png', 'jpeg'))) {
81            $this->_filetype = $type;
82            return true;
83        } else {
84            return false;
85        }
86    }
87
88    public function save($file)
89    {
90        switch ($this->_filetype) {
91        case 'png':
92            return imagepng($this->_image, $file);
93        case 'jpeg':
94            return imagejpeg($this->_image, $file);
95        }
96    }
97
98    public function getSupportedShading()
99    {
100        return array(Image_3D_Renderer::SHADE_NO, Image_3D_Renderer::SHADE_FLAT);
101    }
102}
103
104?>
105