@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | use Imagick; |
17 | 17 | use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger}; |
18 | 18 | |
19 | -class Imagetiler implements LoggerAwareInterface{ |
|
19 | +class Imagetiler implements LoggerAwareInterface { |
|
20 | 20 | use LoggerAwareTrait; |
21 | 21 | |
22 | 22 | /** |
@@ -32,9 +32,9 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @throws \chillerlan\Imagetiler\ImagetilerException |
34 | 34 | */ |
35 | - public function __construct(ContainerInterface $options = null, LoggerInterface $logger = null){ |
|
35 | + public function __construct(ContainerInterface $options = null, LoggerInterface $logger = null) { |
|
36 | 36 | |
37 | - if(!extension_loaded('imagick')){ |
|
37 | + if (!extension_loaded('imagick')) { |
|
38 | 38 | throw new ImagetilerException('Imagick extension is not available'); |
39 | 39 | } |
40 | 40 | |
@@ -52,25 +52,25 @@ discard block |
||
52 | 52 | $options->zoom_min = max(0, $options->zoom_min); |
53 | 53 | $options->zoom_max = max(1, $options->zoom_max); |
54 | 54 | |
55 | - if($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize){ |
|
55 | + if ($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize) { |
|
56 | 56 | $options->zoom_normalize = $options->zoom_max; |
57 | 57 | } |
58 | 58 | |
59 | - if($options->tile_ext === null){ |
|
59 | + if ($options->tile_ext === null) { |
|
60 | 60 | $options->tile_ext = $this->getExtension($options->tile_format); |
61 | 61 | } |
62 | 62 | |
63 | 63 | $this->options = $options; |
64 | 64 | |
65 | - if(ini_set('memory_limit', $this->options->memory_limit) === false){ |
|
65 | + if (ini_set('memory_limit', $this->options->memory_limit) === false) { |
|
66 | 66 | throw new ImagetilerException('could not alter ini settings'); |
67 | 67 | } |
68 | 68 | |
69 | - if(ini_get('memory_limit') !== (string)$this->options->memory_limit){ |
|
69 | + if (ini_get('memory_limit') !== (string)$this->options->memory_limit) { |
|
70 | 70 | throw new ImagetilerException('ini settings differ from options'); |
71 | 71 | } |
72 | 72 | |
73 | - if($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)){ |
|
73 | + if ($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)) { |
|
74 | 74 | apache_setenv('MAGICK_TEMPORARY_PATH', $this->options->imagick_tmp); |
75 | 75 | putenv('MAGICK_TEMPORARY_PATH='.$this->options->imagick_tmp); |
76 | 76 | } |
@@ -88,13 +88,13 @@ discard block |
||
88 | 88 | */ |
89 | 89 | public function process(string $image_path, string $out_path):Imagetiler{ |
90 | 90 | |
91 | - if(!is_file($image_path) || !is_readable($image_path)){ |
|
91 | + if (!is_file($image_path) || !is_readable($image_path)) { |
|
92 | 92 | throw new ImagetilerException('cannot read image '.$image_path); |
93 | 93 | } |
94 | 94 | |
95 | - if(!is_dir($out_path)|| !is_writable($out_path)){ |
|
95 | + if (!is_dir($out_path) || !is_writable($out_path)) { |
|
96 | 96 | |
97 | - if(!mkdir($out_path, 0755, true)){ |
|
97 | + if (!mkdir($out_path, 0755, true)) { |
|
98 | 98 | throw new ImagetilerException('output path is not writable'); |
99 | 99 | } |
100 | 100 | |
@@ -102,18 +102,18 @@ discard block |
||
102 | 102 | |
103 | 103 | $this->prepareZoomBaseImages($image_path, $out_path); |
104 | 104 | |
105 | - for($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++){ |
|
105 | + for ($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++) { |
|
106 | 106 | $this->createTilesForZoom($out_path, $i); |
107 | 107 | } |
108 | 108 | |
109 | 109 | // clean up base images |
110 | - if($this->options->clean_up){ |
|
110 | + if ($this->options->clean_up) { |
|
111 | 111 | |
112 | - for($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++){ |
|
112 | + for ($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++) { |
|
113 | 113 | $lvl_file = $out_path.'/'.$i.'.'.$this->options->tile_ext; |
114 | 114 | |
115 | - if(is_file($lvl_file)){ |
|
116 | - if(unlink($lvl_file)){ |
|
115 | + if (is_file($lvl_file)) { |
|
116 | + if (unlink($lvl_file)) { |
|
117 | 117 | $this->logger->info('deleted base image for zoom level '.$i.': '.$lvl_file); |
118 | 118 | } |
119 | 119 | } |
@@ -142,16 +142,16 @@ discard block |
||
142 | 142 | $start = true; |
143 | 143 | $il = null; |
144 | 144 | |
145 | - for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){ |
|
145 | + for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) { |
|
146 | 146 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
147 | 147 | |
148 | 148 | // check if the base image already exists |
149 | - if(!$this->options->overwrite_base_image && is_file($base_image)){ |
|
149 | + if (!$this->options->overwrite_base_image && is_file($base_image)) { |
|
150 | 150 | $this->logger->info('base image for zoom level '.$zoom.' already exists: '.$base_image); |
151 | 151 | continue; |
152 | 152 | } |
153 | 153 | |
154 | - [$w, $h] = $this->getSize($width, $height, $zoom); |
|
154 | + [ $w, $h ] = $this->getSize($width, $height, $zoom); |
|
155 | 155 | |
156 | 156 | // fit main image to current zoom level |
157 | 157 | $il = $start ? clone $im : $il; |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | |
165 | 165 | $this->imageSave($il, $base_image); |
166 | 166 | |
167 | - if($start){ |
|
167 | + if ($start) { |
|
168 | 168 | $this->clearImage($im); |
169 | 169 | } |
170 | 170 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
189 | 189 | |
190 | 190 | //load image |
191 | - if(!is_file($base_image) || !is_readable($base_image)){ |
|
191 | + if (!is_file($base_image) || !is_readable($base_image)) { |
|
192 | 192 | throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom); |
193 | 193 | } |
194 | 194 | |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | $y = (int)ceil($h / $ts); |
205 | 205 | |
206 | 206 | // width |
207 | - for($ix = 0; $ix < $x; $ix++){ |
|
207 | + for ($ix = 0; $ix < $x; $ix++) { |
|
208 | 208 | $cx = $ix * $ts; |
209 | 209 | |
210 | 210 | // create a stripe tile_size * height |
@@ -212,11 +212,11 @@ discard block |
||
212 | 212 | $ci->cropImage($ts, $h, $cx, 0); |
213 | 213 | |
214 | 214 | // height |
215 | - for($iy = 0; $iy < $y; $iy++){ |
|
215 | + for ($iy = 0; $iy < $y; $iy++) { |
|
216 | 216 | $tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext; |
217 | 217 | |
218 | 218 | // check if tile already exists |
219 | - if(!$this->options->overwrite_tile_image && is_file($tile)){ |
|
219 | + if (!$this->options->overwrite_tile_image && is_file($tile)) { |
|
220 | 220 | $this->logger->info('tile '.$zoom.'/'.$x.'/'.$y.' already exists: '.$tile); |
221 | 221 | |
222 | 222 | continue; |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | $ti->cropImage($ts, $ts, 0, $cy); |
233 | 233 | |
234 | 234 | // check if the current tile is smaller than the tile size (leftover edges on the input image) |
235 | - if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){ |
|
235 | + if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) { |
|
236 | 236 | |
237 | 237 | $th = $this->options->tms |
238 | 238 | ? $im->getImageHeight() - $ts |
@@ -266,18 +266,18 @@ discard block |
||
266 | 266 | protected function imageSave(Imagick $image, string $dest):void{ |
267 | 267 | $dir = dirname($dest); |
268 | 268 | |
269 | - if(!is_dir($dir)){ |
|
270 | - if(!mkdir($dir, 0755, true)){ |
|
269 | + if (!is_dir($dir)) { |
|
270 | + if (!mkdir($dir, 0755, true)) { |
|
271 | 271 | throw new ImagetilerException('cannot crate folder '.$dir); |
272 | 272 | } |
273 | 273 | } |
274 | 274 | |
275 | - if($this->options->tile_format === 'jpeg'){ |
|
275 | + if ($this->options->tile_format === 'jpeg') { |
|
276 | 276 | $image->setCompression(Imagick::COMPRESSION_JPEG); |
277 | 277 | $image->setCompressionQuality($this->options->quality_jpeg); |
278 | 278 | } |
279 | 279 | |
280 | - if(!$image->writeImage($dest)){ |
|
280 | + if (!$image->writeImage($dest)) { |
|
281 | 281 | throw new ImagetilerException('cannot save image '.$dest); |
282 | 282 | } |
283 | 283 | |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | */ |
293 | 293 | protected function clearImage(Imagick $image = null):bool{ |
294 | 294 | |
295 | - if($image instanceof Imagick){ |
|
295 | + if ($image instanceof Imagick) { |
|
296 | 296 | $image->clear(); |
297 | 297 | |
298 | 298 | return $image->destroy(); |
@@ -312,19 +312,19 @@ discard block |
||
312 | 312 | */ |
313 | 313 | protected function getSize(int $width, int $height, int $zoom):array{ |
314 | 314 | |
315 | - if($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize){ |
|
315 | + if ($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize) { |
|
316 | 316 | $zd = 2 ** ($zoom - $this->options->zoom_normalize); |
317 | 317 | |
318 | - return [$zd * $width, $zd * $height]; |
|
318 | + return [ $zd * $width, $zd * $height ]; |
|
319 | 319 | } |
320 | 320 | |
321 | - if($zoom < $this->options->zoom_normalize){ |
|
321 | + if ($zoom < $this->options->zoom_normalize) { |
|
322 | 322 | $zd = 2 ** ($this->options->zoom_normalize - $zoom); |
323 | 323 | |
324 | - return [(int)round($width / $zd), (int)round($height / $zd)]; |
|
324 | + return [ (int)round($width / $zd), (int)round($height / $zd) ]; |
|
325 | 325 | } |
326 | 326 | |
327 | - return [$width, $height]; |
|
327 | + return [ $width, $height ]; |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | /** |
@@ -337,11 +337,11 @@ discard block |
||
337 | 337 | */ |
338 | 338 | protected function getExtension(string $format):string{ |
339 | 339 | |
340 | - if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){ |
|
340 | + if (in_array($format, [ 'jpeg', 'jp2', 'jpc', 'jxr', ], true)) { |
|
341 | 341 | return 'jpg'; |
342 | 342 | } |
343 | 343 | |
344 | - if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){ |
|
344 | + if (in_array($format, [ 'png', 'png00', 'png8', 'png24', 'png32', 'png64', ], true)) { |
|
345 | 345 | return 'png'; |
346 | 346 | } |
347 | 347 |