@@ -14,7 +14,7 @@ |
||
14 | 14 | |
15 | 15 | use Imagick; |
16 | 16 | |
17 | -trait ImagetilerOptionsTrait{ |
|
17 | +trait ImagetilerOptionsTrait { |
|
18 | 18 | |
19 | 19 | /** |
20 | 20 | * width/height of a single tile |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | use Imagick; |
18 | 18 | use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger}; |
19 | 19 | |
20 | -class Imagetiler implements LoggerAwareInterface{ |
|
20 | +class Imagetiler implements LoggerAwareInterface { |
|
21 | 21 | use LoggerAwareTrait; |
22 | 22 | |
23 | 23 | /** |
@@ -39,16 +39,16 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @throws \chillerlan\Imagetiler\ImagetilerException |
41 | 41 | */ |
42 | - public function __construct(ContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null){ |
|
42 | + public function __construct(ContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null) { |
|
43 | 43 | |
44 | - if(!extension_loaded('imagick')){ |
|
44 | + if (!extension_loaded('imagick')) { |
|
45 | 45 | throw new ImagetilerException('Imagick extension is not available'); |
46 | 46 | } |
47 | 47 | |
48 | 48 | $this->setOptions($options ?? new ImagetilerOptions); |
49 | 49 | $this->setLogger($logger ?? new NullLogger); |
50 | 50 | |
51 | - if($optimizer instanceof Optimizer){ |
|
51 | + if ($optimizer instanceof Optimizer) { |
|
52 | 52 | $this->setOptimizer($optimizer); |
53 | 53 | } |
54 | 54 | } |
@@ -63,25 +63,25 @@ discard block |
||
63 | 63 | $options->zoom_min = max(0, $options->zoom_min); |
64 | 64 | $options->zoom_max = max(1, $options->zoom_max); |
65 | 65 | |
66 | - if($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize){ |
|
66 | + if ($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize) { |
|
67 | 67 | $options->zoom_normalize = $options->zoom_max; |
68 | 68 | } |
69 | 69 | |
70 | - if($options->tile_ext === null){ |
|
70 | + if ($options->tile_ext === null) { |
|
71 | 71 | $options->tile_ext = $this->getExtension($options->tile_format); |
72 | 72 | } |
73 | 73 | |
74 | 74 | $this->options = $options; |
75 | 75 | |
76 | - if(ini_set('memory_limit', $this->options->memory_limit) === false){ |
|
76 | + if (ini_set('memory_limit', $this->options->memory_limit) === false) { |
|
77 | 77 | throw new ImagetilerException('could not alter ini settings'); |
78 | 78 | } |
79 | 79 | |
80 | - if(ini_get('memory_limit') !== (string)$this->options->memory_limit){ |
|
80 | + if (ini_get('memory_limit') !== (string)$this->options->memory_limit) { |
|
81 | 81 | throw new ImagetilerException('ini settings differ from options'); |
82 | 82 | } |
83 | 83 | |
84 | - if($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)){ |
|
84 | + if ($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)) { |
|
85 | 85 | apache_setenv('MAGICK_TEMPORARY_PATH', $this->options->imagick_tmp); |
86 | 86 | putenv('MAGICK_TEMPORARY_PATH='.$this->options->imagick_tmp); |
87 | 87 | } |
@@ -110,13 +110,13 @@ discard block |
||
110 | 110 | */ |
111 | 111 | public function process(string $image_path, string $out_path):Imagetiler{ |
112 | 112 | |
113 | - if(!is_file($image_path) || !is_readable($image_path)){ |
|
113 | + if (!is_file($image_path) || !is_readable($image_path)) { |
|
114 | 114 | throw new ImagetilerException('cannot read image '.$image_path); |
115 | 115 | } |
116 | 116 | |
117 | - if(!is_dir($out_path)|| !is_writable($out_path)){ |
|
117 | + if (!is_dir($out_path) || !is_writable($out_path)) { |
|
118 | 118 | |
119 | - if(!mkdir($out_path, 0755, true)){ |
|
119 | + if (!mkdir($out_path, 0755, true)) { |
|
120 | 120 | throw new ImagetilerException('output path is not writable'); |
121 | 121 | } |
122 | 122 | |
@@ -126,12 +126,12 @@ discard block |
||
126 | 126 | $this->prepareZoomBaseImages($image_path, $out_path); |
127 | 127 | |
128 | 128 | // create the tiles |
129 | - for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){ |
|
129 | + for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) { |
|
130 | 130 | |
131 | 131 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
132 | 132 | |
133 | 133 | //load image |
134 | - if(!is_file($base_image) || !is_readable($base_image)){ |
|
134 | + if (!is_file($base_image) || !is_readable($base_image)) { |
|
135 | 135 | throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom); |
136 | 136 | } |
137 | 137 | |
@@ -139,13 +139,13 @@ discard block |
||
139 | 139 | } |
140 | 140 | |
141 | 141 | // clean up base images |
142 | - if($this->options->clean_up){ |
|
142 | + if ($this->options->clean_up) { |
|
143 | 143 | |
144 | - for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){ |
|
144 | + for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) { |
|
145 | 145 | $lvl_file = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
146 | 146 | |
147 | - if(is_file($lvl_file)){ |
|
148 | - if(unlink($lvl_file)){ |
|
147 | + if (is_file($lvl_file)) { |
|
148 | + if (unlink($lvl_file)) { |
|
149 | 149 | $this->logger->info('deleted base image for zoom level '.$zoom.': '.$lvl_file); |
150 | 150 | } |
151 | 151 | } |
@@ -174,16 +174,16 @@ discard block |
||
174 | 174 | $start = true; |
175 | 175 | $il = null; |
176 | 176 | |
177 | - for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){ |
|
177 | + for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) { |
|
178 | 178 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
179 | 179 | |
180 | 180 | // check if the base image already exists |
181 | - if(!$this->options->overwrite_base_image && is_file($base_image)){ |
|
181 | + if (!$this->options->overwrite_base_image && is_file($base_image)) { |
|
182 | 182 | $this->logger->info('base image for zoom level '.$zoom.' already exists: '.$base_image); |
183 | 183 | continue; |
184 | 184 | } |
185 | 185 | |
186 | - [$w, $h] = $this->getSize($width, $height, $zoom); |
|
186 | + [ $w, $h ] = $this->getSize($width, $height, $zoom); |
|
187 | 187 | |
188 | 188 | // fit main image to current zoom level |
189 | 189 | $il = $start ? clone $im : $il; |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | $this->saveImage($il, $base_image); |
198 | 198 | |
199 | - if($start){ |
|
199 | + if ($start) { |
|
200 | 200 | $this->clearImage($im); |
201 | 201 | } |
202 | 202 | |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | $y = (int)ceil($h / $ts); |
228 | 228 | |
229 | 229 | // width |
230 | - for($ix = 0; $ix < $x; $ix++){ |
|
230 | + for ($ix = 0; $ix < $x; $ix++) { |
|
231 | 231 | $cx = $ix * $ts; |
232 | 232 | |
233 | 233 | // create a stripe tile_size * height |
@@ -235,11 +235,11 @@ discard block |
||
235 | 235 | $ci->cropImage($ts, $h, $cx, 0); |
236 | 236 | |
237 | 237 | // height |
238 | - for($iy = 0; $iy < $y; $iy++){ |
|
238 | + for ($iy = 0; $iy < $y; $iy++) { |
|
239 | 239 | $tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext; |
240 | 240 | |
241 | 241 | // check if tile already exists |
242 | - if(!$this->options->overwrite_tile_image && is_file($tile)){ |
|
242 | + if (!$this->options->overwrite_tile_image && is_file($tile)) { |
|
243 | 243 | $this->logger->info('tile '.$zoom.'/'.$x.'/'.$y.' already exists: '.$tile); |
244 | 244 | |
245 | 245 | continue; |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | $ti->cropImage($ts, $ts, 0, $cy); |
256 | 256 | |
257 | 257 | // check if the current tile is smaller than the tile size (leftover edges on the input image) |
258 | - if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){ |
|
258 | + if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) { |
|
259 | 259 | |
260 | 260 | $th = $this->options->tms |
261 | 261 | ? $im->getImageHeight() - $ts |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | } |
271 | 271 | |
272 | 272 | $this->clearImage($ci); |
273 | - $this->logger->info('created column '.($ix+1).', x = '.$cx); |
|
273 | + $this->logger->info('created column '.($ix + 1).', x = '.$cx); |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | $this->clearImage($im); |
@@ -289,22 +289,22 @@ discard block |
||
289 | 289 | protected function saveImage(Imagick $image, string $dest):void{ |
290 | 290 | $dir = dirname($dest); |
291 | 291 | |
292 | - if(!is_dir($dir)){ |
|
293 | - if(!mkdir($dir, 0755, true)){ |
|
292 | + if (!is_dir($dir)) { |
|
293 | + if (!mkdir($dir, 0755, true)) { |
|
294 | 294 | throw new ImagetilerException('cannot crate folder '.$dir); |
295 | 295 | } |
296 | 296 | } |
297 | 297 | |
298 | - if($this->options->tile_format === 'jpeg'){ |
|
298 | + if ($this->options->tile_format === 'jpeg') { |
|
299 | 299 | $image->setCompression(Imagick::COMPRESSION_JPEG); |
300 | 300 | $image->setCompressionQuality($this->options->quality_jpeg); |
301 | 301 | } |
302 | 302 | |
303 | - if(!$image->writeImage($dest)){ |
|
303 | + if (!$image->writeImage($dest)) { |
|
304 | 304 | throw new ImagetilerException('cannot save image '.$dest); |
305 | 305 | } |
306 | 306 | |
307 | - if($this->options->optimize_output && $this->optimizer instanceof Optimizer){ |
|
307 | + if ($this->options->optimize_output && $this->optimizer instanceof Optimizer) { |
|
308 | 308 | $this->optimizer->optimize($dest); |
309 | 309 | } |
310 | 310 | |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | */ |
320 | 320 | protected function clearImage(Imagick $image = null):bool{ |
321 | 321 | |
322 | - if($image instanceof Imagick){ |
|
322 | + if ($image instanceof Imagick) { |
|
323 | 323 | $image->clear(); |
324 | 324 | |
325 | 325 | return $image->destroy(); |
@@ -339,19 +339,19 @@ discard block |
||
339 | 339 | */ |
340 | 340 | protected function getSize(int $width, int $height, int $zoom):array{ |
341 | 341 | |
342 | - if($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize){ |
|
342 | + if ($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize) { |
|
343 | 343 | $zd = 2 ** ($zoom - $this->options->zoom_normalize); |
344 | 344 | |
345 | - return [$zd * $width, $zd * $height]; |
|
345 | + return [ $zd * $width, $zd * $height ]; |
|
346 | 346 | } |
347 | 347 | |
348 | - if($zoom < $this->options->zoom_normalize){ |
|
348 | + if ($zoom < $this->options->zoom_normalize) { |
|
349 | 349 | $zd = 2 ** ($this->options->zoom_normalize - $zoom); |
350 | 350 | |
351 | - return [(int)round($width / $zd), (int)round($height / $zd)]; |
|
351 | + return [ (int)round($width / $zd), (int)round($height / $zd) ]; |
|
352 | 352 | } |
353 | 353 | |
354 | - return [$width, $height]; |
|
354 | + return [ $width, $height ]; |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
@@ -364,11 +364,11 @@ discard block |
||
364 | 364 | */ |
365 | 365 | protected function getExtension(string $format):string{ |
366 | 366 | |
367 | - if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){ |
|
367 | + if (in_array($format, [ 'jpeg', 'jp2', 'jpc', 'jxr', ], true)) { |
|
368 | 368 | return 'jpg'; |
369 | 369 | } |
370 | 370 | |
371 | - if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){ |
|
371 | + if (in_array($format, [ 'png', 'png00', 'png8', 'png24', 'png32', 'png64', ], true)) { |
|
372 | 372 | return 'png'; |
373 | 373 | } |
374 | 374 |
@@ -35,6 +35,6 @@ |
||
35 | 35 | * @property bool $clean_up |
36 | 36 | * @property bool $optimize_output |
37 | 37 | */ |
38 | -class ImagetilerOptions extends ContainerAbstract{ |
|
38 | +class ImagetilerOptions extends ContainerAbstract { |
|
39 | 39 | use ImagetilerOptionsTrait; |
40 | 40 | } |