1 <?php
2
3 4 5 6 7 8 9 10 11 12
13 class Image_ImageMagick_Driver extends Image_Driver {
14
15
16 protected $dir = '';
17
18
19 protected $ext = '';
20
21
22 protected $tmp_image;
23
24 25 26 27 28 29 30
31 public function __construct($config)
32 {
33 if (empty($config['directory']))
34 {
35
36 if ( ! is_file($path = exec('which convert')))
37 throw new CException('image imagemagick not_found');
38
39 $config['directory'] = dirname($path);
40 }
41
42
43 $this->ext = (PHP_SHLIB_SUFFIX === 'dll') ? '.exe' : '';
44
45
46 if ( ! is_file(realpath($config['directory']).'/convert'.$this->ext))
47 throw new CException('imagemagick not_found in '.$config['directory']);
48
49
50 $this->dir = str_replace('\\', '/', realpath($config['directory'])).'/';
51 }
52
53 54 55 56
57 public function process($image, $actions, $dir, $file, $render = FALSE)
58 {
59
60 $image = $image['file'];
61
62
63 $this->tmp_image = $dir.'k2img--'.sha1(time().$dir.$file).substr($file, strrpos($file, '.'));
64
65
66 copy($image, $this->tmp_image);
67
68
69 if(isset ($actions['quality'])) {
70 $quality = $actions['quality'];
71 unset($actions['quality']);
72 }
73
74
75 empty($quality) and $quality = 95;
76
77
78 $this->cmd_image = escapeshellarg($this->tmp_image);
79 $this->new_image = ($render)? $this->cmd_image : escapeshellarg($dir.$file);
80
81 if ($status = $this->execute($actions))
82 {
83
84
85
86 if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' -quality '.$quality.'% '.$this->cmd_image.' '.$this->new_image))
87 {
88 $this->errors[] = $error;
89 }
90 else
91 {
92
93 if ($render !== FALSE)
94 {
95 $contents = file_get_contents($this->tmp_image);
96 switch (substr($file, strrpos($file, '.') + 1))
97 {
98 case 'jpg':
99 case 'jpeg':
100 header('Content-Type: image/jpeg');
101 break;
102 case 'gif':
103 header('Content-Type: image/gif');
104 break;
105 case 'png':
106 header('Content-Type: image/png');
107 break;
108 }
109 echo $contents;
110 }
111 }
112 }
113
114
115 unlink($this->tmp_image);
116 $this->tmp_image = '';
117
118 return $status;
119 }
120
121 public function crop($prop)
122 {
123
124 $this->sanitize_geometry($prop);
125
126
127 $geometry = escapeshellarg($prop['width'].'x'.$prop['height'].'+'.$prop['left'].'+'.$prop['top']);
128
129 if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' -crop '.$geometry.' '.$this->cmd_image.' '.$this->cmd_image))
130 {
131 $this->errors[] = $error;
132 return FALSE;
133 }
134
135 return TRUE;
136 }
137
138 public function flip($dir)
139 {
140
141 $dir = ($dir === Image::HORIZONTAL) ? '-flop' : '-flip';
142
143 if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' '.$dir.' '.$this->cmd_image.' '.$this->cmd_image))
144 {
145 $this->errors[] = $error;
146 return FALSE;
147 }
148
149 return TRUE;
150 }
151
152 public function resize($prop)
153 {
154 switch ($prop['master'])
155 {
156 case Image::WIDTH:
157 $dim = escapeshellarg($prop['width'].'x');
158 break;
159 case Image::HEIGHT:
160 $dim = escapeshellarg('x'.$prop['height']);
161 break;
162 case Image::AUTO:
163 $dim = escapeshellarg($prop['width'].'x'.$prop['height']);
164 break;
165 case Image::NONE:
166 $dim = escapeshellarg($prop['width'].'x'.$prop['height'].'!');
167 break;
168 }
169
170
171 if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' -resize '.$dim.' '.$this->cmd_image.' '.$this->cmd_image))
172 {
173 $this->errors[] = $error;
174 return FALSE;
175 }
176
177 return TRUE;
178 }
179
180 public function rotate($amt)
181 {
182 if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' -rotate '.escapeshellarg($amt).' -background transparent '.$this->cmd_image.' '.$this->cmd_image))
183 {
184 $this->errors[] = $error;
185 return FALSE;
186 }
187
188 return TRUE;
189 }
190
191 public function sharpen($amount)
192 {
193
194
195 $sigma = 0.5;
196 $radius = $sigma * 2;
197 $amount = round(($amount / 80) * 3.14, 2);
198
199
200 $sharpen = escapeshellarg($radius.'x'.$sigma.'+'.$amount.'+0');
201
202 if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' -unsharp '.$sharpen.' '.$this->cmd_image.' '.$this->cmd_image))
203 {
204 $this->errors[] = $error;
205 return FALSE;
206 }
207
208 return TRUE;
209 }
210
211 protected function properties()
212 {
213 return array_slice(getimagesize($this->tmp_image), 0, 2, FALSE);
214 }
215
216 }