1<?php
2
3class Image_3D_Driver_ImageMagick extends Image_3D_Driver
4{
5
6    /**
7     * Array of parameter strings passed to 'convert' binary.
8     *
9     * @var array
10     * @access protected
11     */
12    protected $_commandQueue = array();
13
14    public function __construct()
15    {
16        parent::__construct();
17    }
18
19    public function createImage($x, $y)
20    {
21        $this->_image = tempnam();
22
23        $this->_dimensions = array('x' => $x, 'y' => $y);
24
25        $this->_commandQueue[] = "-size {$x}x{$y} xc:transparent";
26    }
27
28    protected function _getColor(Image_3D_Color $color)
29    {
30        $values = $color->getValues();
31
32        $values[0] = (int) round($values[0] * 255);
33        $values[1] = (int) round($values[1] * 255);
34        $values[2] = (int) round($values[2] * 255);
35        $values[3] = (int) round($values[3] * 127);
36
37        $color = '';
38        if ($values[3] > 0) {
39            $color = 'rgba('.implode(',', $values).')';
40        } else {
41            unset($values[3]);
42            $color = 'rgb('.implode(',', $values).')';
43        }
44
45        return $color;
46    }
47
48    public function setBackground(Image_3D_Color $color)
49    {
50        $colorString = $this->_getColor($color);
51        array_splice($this->_commandQueue,
52                     1,
53                     0,
54                     '-fill '.escapeshellarg($colorString).' '.
55                     '-draw '.escapeshellarg('rectangle 0,0 '.implode(',', $this->_dimensions)));
56    }
57
58    public function drawPolygon(Image_3D_Polygon $polygon)
59    {
60        // Get points
61        $points = $polygon->getPoints();
62        $coords = array();
63
64        $coords = '';
65        foreach ($points as $point) {
66            $pointCoords = $point->getScreenCoordinates();
67
68            $coords .= (int)$pointCoords[0].','.(int)$pointCoords[1].' ';
69        }
70
71        $command  = '-fill '.escapeshellarg($this->_getColor($polygon->getColor()));
72        $command .= ' -draw '.escapeshellarg('polygon '.trim($coords));
73
74        $this->_commandQueue[] = $command;
75    }
76
77    public function drawGradientPolygon(Image_3D_Polygon $polygon)
78    {
79        $this->drawPolygon($polygon);
80    }
81
82    public function setFiletye($type)
83    {
84        $type = strtolower($type);
85        if (in_array($type, array('png', 'jpeg'))) {
86            $this->_filetype = $type;
87            return true;
88        } else {
89            return false;
90        }
91    }
92
93    public function save($file)
94    {
95        $command      = '';
96        $commandCount = 1;
97        $firstRun     = true;
98
99        for ($i = 0; $i < count($this->_commandQueue); $i++) {
100            $command .= ' '.$this->_commandQueue[$i].' ';
101            if (strlen($command) > 1000 || $i == count($this->_commandQueue) - 1) {
102                $firstRun === false ? $command = $file . ' ' . $command : $firstRun = false;
103
104                $command =  'convert ' . $command . ' ' . $file;
105                // echo "Excuting command run <".$commandCount++.">\n";
106                shell_exec($command);
107                $command = '';
108            }
109        }
110        shell_exec($command);
111    }
112
113    public function getSupportedShading()
114    {
115        return array(Image_3D_Renderer::SHADE_NO, Image_3D_Renderer::SHADE_FLAT);
116    }
117}
118
119?>
120