@@ -12,6 +12,6 @@ |
||
| 12 | 12 | |
| 13 | 13 | namespace chillerlan\Imagetiler; |
| 14 | 14 | |
| 15 | -class ImagetilerException extends \Exception{ |
|
| 15 | +class ImagetilerException extends \Exception { |
|
| 16 | 16 | |
| 17 | 17 | } |
@@ -38,6 +38,6 @@ |
||
| 38 | 38 | * @property bool $optimize_output |
| 39 | 39 | * @property bool $no_temp_baseimages |
| 40 | 40 | */ |
| 41 | -class ImagetilerOptions extends SettingsContainerAbstract{ |
|
| 41 | +class ImagetilerOptions extends SettingsContainerAbstract { |
|
| 42 | 42 | use ImagetilerOptionsTrait; |
| 43 | 43 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | use function in_array, max, strtolower; |
| 18 | 18 | |
| 19 | -trait ImagetilerOptionsTrait{ |
|
| 19 | +trait ImagetilerOptionsTrait { |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * width/height of a single tile |
@@ -213,11 +213,11 @@ discard block |
||
| 213 | 213 | protected function getExtension(string $format):string{ |
| 214 | 214 | $format = strtolower($format); |
| 215 | 215 | |
| 216 | - if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr'], true)){ |
|
| 216 | + if (in_array($format, [ 'jpeg', 'jp2', 'jpc', 'jxr' ], true)) { |
|
| 217 | 217 | return 'jpg'; |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64'], true)){ |
|
| 220 | + if (in_array($format, [ 'png', 'png00', 'png8', 'png24', 'png32', 'png64' ], true)) { |
|
| 221 | 221 | return 'png'; |
| 222 | 222 | } |
| 223 | 223 | |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | use function ceil, dirname, extension_loaded, function_exists, ini_get, ini_set, is_dir, |
| 21 | 21 | is_file, is_readable, is_writable, mkdir, putenv, round, sprintf, unlink; |
| 22 | 22 | |
| 23 | -class Imagetiler implements LoggerAwareInterface{ |
|
| 23 | +class Imagetiler implements LoggerAwareInterface { |
|
| 24 | 24 | use LoggerAwareTrait; |
| 25 | 25 | |
| 26 | 26 | /** |
@@ -42,16 +42,16 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @throws \chillerlan\Imagetiler\ImagetilerException |
| 44 | 44 | */ |
| 45 | - public function __construct(SettingsContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null){ |
|
| 45 | + public function __construct(SettingsContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null) { |
|
| 46 | 46 | |
| 47 | - if(!extension_loaded('imagick')){ |
|
| 47 | + if (!extension_loaded('imagick')) { |
|
| 48 | 48 | throw new ImagetilerException('Imagick extension is not available'); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | $this->setOptions($options ?? new ImagetilerOptions); |
| 52 | 52 | $this->setLogger($logger ?? new NullLogger); |
| 53 | 53 | |
| 54 | - if($optimizer instanceof Optimizer){ |
|
| 54 | + if ($optimizer instanceof Optimizer) { |
|
| 55 | 55 | $this->setOptimizer($optimizer); |
| 56 | 56 | } |
| 57 | 57 | } |
@@ -65,11 +65,11 @@ discard block |
||
| 65 | 65 | public function setOptions(SettingsContainerInterface $options):Imagetiler{ |
| 66 | 66 | $this->options = $options; |
| 67 | 67 | |
| 68 | - if(ini_set('memory_limit', $this->options->memory_limit) === false){ |
|
| 68 | + if (ini_set('memory_limit', $this->options->memory_limit) === false) { |
|
| 69 | 69 | throw new ImagetilerException('could not alter ini settings'); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - if(ini_get('memory_limit') !== (string)$this->options->memory_limit){ |
|
| 72 | + if (ini_get('memory_limit') !== (string)$this->options->memory_limit) { |
|
| 73 | 73 | throw new ImagetilerException('ini settings differ from options'); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | */ |
| 97 | 97 | public function process(string $image_path, string $out_path):Imagetiler{ |
| 98 | 98 | |
| 99 | - if(!is_file($image_path) || !is_readable($image_path)){ |
|
| 99 | + if (!is_file($image_path) || !is_readable($image_path)) { |
|
| 100 | 100 | throw new ImagetilerException('cannot read image '.$image_path); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - if(!is_dir($out_path) || !is_writable($out_path)){ |
|
| 103 | + if (!is_dir($out_path) || !is_writable($out_path)) { |
|
| 104 | 104 | |
| 105 | - if(!mkdir($out_path, 0755, true)){ |
|
| 105 | + if (!mkdir($out_path, 0755, true)) { |
|
| 106 | 106 | throw new ImagetilerException('output path is not writable'); |
| 107 | 107 | } |
| 108 | 108 | |
@@ -113,15 +113,15 @@ discard block |
||
| 113 | 113 | // prepare the zoom base images |
| 114 | 114 | $base_images = $this->prepareZoomBaseImages($image_path, $out_path); |
| 115 | 115 | |
| 116 | - if($this->options->no_temp_baseimages === true){ |
|
| 116 | + if ($this->options->no_temp_baseimages === true) { |
|
| 117 | 117 | return $this; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // create the tiles |
| 121 | - foreach($base_images as $zoom => $base_image){ |
|
| 121 | + foreach ($base_images as $zoom => $base_image) { |
|
| 122 | 122 | |
| 123 | 123 | //load image |
| 124 | - if(!is_file($base_image) || !is_readable($base_image)){ |
|
| 124 | + if (!is_file($base_image) || !is_readable($base_image)) { |
|
| 125 | 125 | throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom); |
| 126 | 126 | } |
| 127 | 127 | |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // clean up base images |
| 132 | - if($this->options->clean_up){ |
|
| 132 | + if ($this->options->clean_up) { |
|
| 133 | 133 | |
| 134 | - for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){ |
|
| 134 | + for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) { |
|
| 135 | 135 | $lvl_file = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 136 | 136 | |
| 137 | - if(is_file($lvl_file)){ |
|
| 138 | - if(unlink($lvl_file)){ |
|
| 137 | + if (is_file($lvl_file)) { |
|
| 138 | + if (unlink($lvl_file)) { |
|
| 139 | 139 | $this->logger->info('deleted base image for zoom level '.$zoom.': '.$lvl_file); |
| 140 | 140 | } |
| 141 | 141 | } |
@@ -155,23 +155,23 @@ discard block |
||
| 155 | 155 | * @return array |
| 156 | 156 | */ |
| 157 | 157 | protected function prepareZoomBaseImages(string $image_path, string $out_path):array{ |
| 158 | - $base_images = []; |
|
| 158 | + $base_images = [ ]; |
|
| 159 | 159 | |
| 160 | 160 | // create base image file names |
| 161 | - for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){ |
|
| 161 | + for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) { |
|
| 162 | 162 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 163 | 163 | // check if the base image already exists |
| 164 | - if(!$this->options->overwrite_base_image && is_file($base_image)){ |
|
| 164 | + if (!$this->options->overwrite_base_image && is_file($base_image)) { |
|
| 165 | 165 | $this->logger->info('base image for zoom level '.$zoom.' already exists: '.$base_image); |
| 166 | 166 | |
| 167 | 167 | continue; |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - $base_images[$zoom] = $base_image; |
|
| 170 | + $base_images[ $zoom ] = $base_image; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - if(empty($base_images)){ |
|
| 174 | - return []; |
|
| 173 | + if (empty($base_images)) { |
|
| 174 | + return [ ]; |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | $im = new Imagick($image_path); |
@@ -183,16 +183,16 @@ discard block |
||
| 183 | 183 | |
| 184 | 184 | $this->logger->info('input image loaded: ['.$width.'x'.$height.'] '.$image_path); |
| 185 | 185 | |
| 186 | - foreach($base_images as $zoom => $base_image){ |
|
| 187 | - [$w, $h] = $this->getSize($width, $height, $zoom); |
|
| 186 | + foreach ($base_images as $zoom => $base_image) { |
|
| 187 | + [ $w, $h ] = $this->getSize($width, $height, $zoom); |
|
| 188 | 188 | |
| 189 | 189 | // clone the original image and fit it to the current zoom level |
| 190 | 190 | $il = clone $im; |
| 191 | 191 | |
| 192 | - if($zoom > $this->options->zoom_normalize){ |
|
| 192 | + if ($zoom > $this->options->zoom_normalize) { |
|
| 193 | 193 | $this->scale($il, $w, $h, $this->options->fast_resize_upsample, $this->options->resize_filter_upsample, $this->options->resize_blur_upsample); |
| 194 | 194 | } |
| 195 | - elseif($zoom < $this->options->zoom_normalize){ |
|
| 195 | + elseif ($zoom < $this->options->zoom_normalize) { |
|
| 196 | 196 | $this->scale($il, $w, $h, $this->options->fast_resize_downsample, $this->options->resize_filter_downsample, $this->options->resize_blur_downsample); |
| 197 | 197 | } |
| 198 | 198 | |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | $y = (int)ceil($h / $ts); |
| 246 | 246 | |
| 247 | 247 | // width |
| 248 | - for($ix = 0; $ix < $x; $ix++){ |
|
| 248 | + for ($ix = 0; $ix < $x; $ix++) { |
|
| 249 | 249 | $cx = $ix * $ts; |
| 250 | 250 | |
| 251 | 251 | // create a stripe tile_size * height |
@@ -253,11 +253,11 @@ discard block |
||
| 253 | 253 | $ci->cropImage($ts, $h, $cx, 0); |
| 254 | 254 | |
| 255 | 255 | // height |
| 256 | - for($iy = 0; $iy < $y; $iy++){ |
|
| 256 | + for ($iy = 0; $iy < $y; $iy++) { |
|
| 257 | 257 | $tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext; |
| 258 | 258 | |
| 259 | 259 | // check if tile already exists |
| 260 | - if(!$this->options->overwrite_tile_image && is_file($tile)){ |
|
| 260 | + if (!$this->options->overwrite_tile_image && is_file($tile)) { |
|
| 261 | 261 | $this->logger->info('tile '.$zoom.'/'.$ix.'/'.$iy.' already exists: '.$tile); |
| 262 | 262 | |
| 263 | 263 | continue; |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | $ti->cropImage($ts, $ts, 0, $cy); |
| 274 | 274 | |
| 275 | 275 | // check if the current tile is smaller than the tile size (leftover edges on the input image) |
| 276 | - if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){ |
|
| 276 | + if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) { |
|
| 277 | 277 | |
| 278 | 278 | $th = $this->options->tms ? $h - $ts : 0; |
| 279 | 279 | |
@@ -306,22 +306,22 @@ discard block |
||
| 306 | 306 | protected function saveImage(Imagick $image, string $dest, bool $optimize):void{ |
| 307 | 307 | $dir = dirname($dest); |
| 308 | 308 | |
| 309 | - if(!is_dir($dir)){ |
|
| 310 | - if(!mkdir($dir, 0755, true)){ |
|
| 309 | + if (!is_dir($dir)) { |
|
| 310 | + if (!mkdir($dir, 0755, true)) { |
|
| 311 | 311 | throw new ImagetilerException('cannot create folder '.$dir); |
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | - if($this->options->tile_format === 'jpeg'){ |
|
| 315 | + if ($this->options->tile_format === 'jpeg') { |
|
| 316 | 316 | $image->setCompression(Imagick::COMPRESSION_JPEG2000); |
| 317 | 317 | $image->setCompressionQuality($this->options->quality_jpeg); |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - if(!$image->writeImage($dest)){ |
|
| 320 | + if (!$image->writeImage($dest)) { |
|
| 321 | 321 | throw new ImagetilerException('cannot save image '.$dest); |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | - if($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer){ |
|
| 324 | + if ($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer) { |
|
| 325 | 325 | $this->optimizer->optimize($dest); |
| 326 | 326 | } |
| 327 | 327 | |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | */ |
| 337 | 337 | protected function clearImage(Imagick $image = null):bool{ |
| 338 | 338 | |
| 339 | - if($image instanceof Imagick){ |
|
| 339 | + if ($image instanceof Imagick) { |
|
| 340 | 340 | $image->clear(); |
| 341 | 341 | |
| 342 | 342 | return $image->destroy(); |
@@ -357,19 +357,19 @@ discard block |
||
| 357 | 357 | protected function getSize(int $width, int $height, int $zoom):array{ |
| 358 | 358 | $zoom_normalize = $this->options->zoom_normalize ?? $this->options->zoom_max; |
| 359 | 359 | |
| 360 | - if($this->options->zoom_max > $zoom_normalize && $zoom > $zoom_normalize){ |
|
| 360 | + if ($this->options->zoom_max > $zoom_normalize && $zoom > $zoom_normalize) { |
|
| 361 | 361 | $zd = 2 ** ($zoom - $zoom_normalize); |
| 362 | 362 | |
| 363 | - return [$zd * $width, $zd * $height]; |
|
| 363 | + return [ $zd * $width, $zd * $height ]; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | - if($zoom < $zoom_normalize){ |
|
| 366 | + if ($zoom < $zoom_normalize) { |
|
| 367 | 367 | $zd = 2 ** ($zoom_normalize - $zoom); |
| 368 | 368 | |
| 369 | - return [(int)round($width / $zd), (int)round($height / $zd)]; |
|
| 369 | + return [ (int)round($width / $zd), (int)round($height / $zd) ]; |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | - return [$width, $height]; |
|
| 372 | + return [ $width, $height ]; |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | } |
@@ -191,8 +191,7 @@ |
||
| 191 | 191 | |
| 192 | 192 | if($zoom > $this->options->zoom_normalize){ |
| 193 | 193 | $this->scale($il, $w, $h, $this->options->fast_resize_upsample, $this->options->resize_filter_upsample, $this->options->resize_blur_upsample); |
| 194 | - } |
|
| 195 | - elseif($zoom < $this->options->zoom_normalize){ |
|
| 194 | + } elseif($zoom < $this->options->zoom_normalize){ |
|
| 196 | 195 | $this->scale($il, $w, $h, $this->options->fast_resize_downsample, $this->options->resize_filter_downsample, $this->options->resize_blur_downsample); |
| 197 | 196 | } |
| 198 | 197 | |