@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | use function ceil, dirname, extension_loaded, file_exists, ini_get, ini_set, is_dir, |
| 19 | 19 | is_file, is_readable, is_writable, mkdir, round, sprintf, unlink; |
| 20 | 20 | |
| 21 | -class Imagetiler implements LoggerAwareInterface{ |
|
| 21 | +class Imagetiler implements LoggerAwareInterface { |
|
| 22 | 22 | use LoggerAwareTrait; |
| 23 | 23 | |
| 24 | 24 | /** @var \chillerlan\Imagetiler\ImagetilerOptions */ |
@@ -30,16 +30,16 @@ discard block |
||
| 30 | 30 | * |
| 31 | 31 | * @throws \chillerlan\Imagetiler\ImagetilerException |
| 32 | 32 | */ |
| 33 | - public function __construct(SettingsContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null){ |
|
| 33 | + public function __construct(SettingsContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null) { |
|
| 34 | 34 | |
| 35 | - if(!extension_loaded('imagick')){ |
|
| 35 | + if (!extension_loaded('imagick')) { |
|
| 36 | 36 | throw new ImagetilerException('Imagick extension is not available'); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | $this->setOptions($options ?? new ImagetilerOptions); |
| 40 | 40 | $this->setLogger($logger ?? new NullLogger); |
| 41 | 41 | |
| 42 | - if($optimizer instanceof Optimizer){ |
|
| 42 | + if ($optimizer instanceof Optimizer) { |
|
| 43 | 43 | $this->setOptimizer($optimizer); |
| 44 | 44 | } |
| 45 | 45 | } |
@@ -50,11 +50,11 @@ discard block |
||
| 50 | 50 | public function setOptions(SettingsContainerInterface $options):Imagetiler{ |
| 51 | 51 | $this->options = $options; |
| 52 | 52 | |
| 53 | - if(ini_set('memory_limit', $this->options->memory_limit) === false){ |
|
| 53 | + if (ini_set('memory_limit', $this->options->memory_limit) === false) { |
|
| 54 | 54 | throw new ImagetilerException('could not alter ini settings'); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if(ini_get('memory_limit') !== $this->options->memory_limit){ |
|
| 57 | + if (ini_get('memory_limit') !== $this->options->memory_limit) { |
|
| 58 | 58 | throw new ImagetilerException('ini settings differ from options'); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | public function process(string $image_path, string $out_path):Imagetiler{ |
| 76 | 76 | |
| 77 | - if(!file_exists($image_path) || !is_file($image_path) || !is_readable($image_path)){ |
|
| 77 | + if (!file_exists($image_path) || !is_file($image_path) || !is_readable($image_path)) { |
|
| 78 | 78 | throw new ImagetilerException('cannot read image '.$image_path); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - if(!file_exists($out_path) || !is_dir($out_path) || !is_writable($out_path)){ |
|
| 81 | + if (!file_exists($out_path) || !is_dir($out_path) || !is_writable($out_path)) { |
|
| 82 | 82 | |
| 83 | - if(!mkdir($out_path, 0755, true)){ |
|
| 83 | + if (!mkdir($out_path, 0755, true)) { |
|
| 84 | 84 | throw new ImagetilerException('output path is not writable'); |
| 85 | 85 | } |
| 86 | 86 | |
@@ -92,15 +92,15 @@ discard block |
||
| 92 | 92 | $base_images = $this->prepareZoomBaseImages($image_path, $out_path); |
| 93 | 93 | |
| 94 | 94 | // if no temp images are requested, exit here as the processing was handled already |
| 95 | - if($this->options->no_temp_baseimages === true){ |
|
| 95 | + if ($this->options->no_temp_baseimages === true) { |
|
| 96 | 96 | return $this; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // create the tiles |
| 100 | - foreach($base_images as $zoom => $base_image){ |
|
| 100 | + foreach ($base_images as $zoom => $base_image) { |
|
| 101 | 101 | |
| 102 | 102 | //load image |
| 103 | - if(!is_file($base_image) || !is_readable($base_image)){ |
|
| 103 | + if (!is_file($base_image) || !is_readable($base_image)) { |
|
| 104 | 104 | throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom); |
| 105 | 105 | } |
| 106 | 106 | |
@@ -108,13 +108,13 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // clean up base images |
| 111 | - if($this->options->clean_up){ |
|
| 111 | + if ($this->options->clean_up) { |
|
| 112 | 112 | |
| 113 | - for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){ |
|
| 113 | + for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) { |
|
| 114 | 114 | $lvl_file = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 115 | 115 | |
| 116 | - if(is_file($lvl_file)){ |
|
| 117 | - if(unlink($lvl_file)){ |
|
| 116 | + if (is_file($lvl_file)) { |
|
| 117 | + if (unlink($lvl_file)) { |
|
| 118 | 118 | $this->logger->info('deleted base image for zoom level '.$zoom.': '.$lvl_file); |
| 119 | 119 | } |
| 120 | 120 | } |
@@ -129,23 +129,23 @@ discard block |
||
| 129 | 129 | * prepare base images for each zoom level |
| 130 | 130 | */ |
| 131 | 131 | protected function prepareZoomBaseImages(string $image_path, string $out_path):array{ |
| 132 | - $base_images = []; |
|
| 132 | + $base_images = [ ]; |
|
| 133 | 133 | |
| 134 | 134 | // create base image file names |
| 135 | - for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){ |
|
| 135 | + for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) { |
|
| 136 | 136 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 137 | 137 | // check if the base image already exists |
| 138 | - if(!$this->options->overwrite_base_image && is_file($base_image)){ |
|
| 138 | + if (!$this->options->overwrite_base_image && is_file($base_image)) { |
|
| 139 | 139 | $this->logger->info('base image for zoom level '.$zoom.' already exists: '.$base_image); |
| 140 | 140 | |
| 141 | 141 | continue; |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - $base_images[$zoom] = $base_image; |
|
| 144 | + $base_images[ $zoom ] = $base_image; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - if(empty($base_images)){ |
|
| 148 | - return []; |
|
| 147 | + if (empty($base_images)) { |
|
| 148 | + return [ ]; |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | $im = new Imagick($image_path); |
@@ -157,16 +157,16 @@ discard block |
||
| 157 | 157 | |
| 158 | 158 | $this->logger->info('input image loaded: ['.$width.'x'.$height.'] '.$image_path); |
| 159 | 159 | |
| 160 | - foreach($base_images as $zoom => $base_image){ |
|
| 161 | - [$w, $h] = $this->getSize($width, $height, $zoom); |
|
| 160 | + foreach ($base_images as $zoom => $base_image) { |
|
| 161 | + [ $w, $h ] = $this->getSize($width, $height, $zoom); |
|
| 162 | 162 | |
| 163 | 163 | // clone the original image and fit it to the current zoom level |
| 164 | 164 | $il = clone $im; |
| 165 | 165 | |
| 166 | - if($zoom > $this->options->zoom_normalize){ |
|
| 166 | + if ($zoom > $this->options->zoom_normalize) { |
|
| 167 | 167 | $this->scale($il, $w, $h, $this->options->fast_resize_upsample, $this->options->resize_filter_upsample, $this->options->resize_blur_upsample); |
| 168 | 168 | } |
| 169 | - elseif($zoom < $this->options->zoom_normalize){ |
|
| 169 | + elseif ($zoom < $this->options->zoom_normalize) { |
|
| 170 | 170 | $this->scale($il, $w, $h, $this->options->fast_resize_downsample, $this->options->resize_filter_downsample, $this->options->resize_blur_downsample); |
| 171 | 171 | } |
| 172 | 172 | |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | $y = (int)ceil($h / $ts); |
| 207 | 207 | |
| 208 | 208 | // width |
| 209 | - for($ix = 0; $ix < $x; $ix++){ |
|
| 209 | + for ($ix = 0; $ix < $x; $ix++) { |
|
| 210 | 210 | $cx = $ix * $ts; |
| 211 | 211 | |
| 212 | 212 | // create a stripe tile_size * height |
@@ -214,11 +214,11 @@ discard block |
||
| 214 | 214 | $ci->cropImage($ts, $h, $cx, 0); |
| 215 | 215 | |
| 216 | 216 | // height |
| 217 | - for($iy = 0; $iy < $y; $iy++){ |
|
| 217 | + for ($iy = 0; $iy < $y; $iy++) { |
|
| 218 | 218 | $tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext; |
| 219 | 219 | |
| 220 | 220 | // check if tile already exists |
| 221 | - if(!$this->options->overwrite_tile_image && is_file($tile)){ |
|
| 221 | + if (!$this->options->overwrite_tile_image && is_file($tile)) { |
|
| 222 | 222 | $this->logger->info('tile '.$zoom.'/'.$ix.'/'.$iy.' already exists: '.$tile); |
| 223 | 223 | |
| 224 | 224 | continue; |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | $ti->cropImage($ts, $ts, 0, $cy); |
| 235 | 235 | |
| 236 | 236 | // check if the current tile is smaller than the tile size (leftover edges on the input image) |
| 237 | - if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){ |
|
| 237 | + if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) { |
|
| 238 | 238 | |
| 239 | 239 | $th = $this->options->tms ? $h - $ts : 0; |
| 240 | 240 | |
@@ -262,22 +262,22 @@ discard block |
||
| 262 | 262 | protected function saveImage(Imagick $image, string $dest, bool $optimize):void{ |
| 263 | 263 | $dir = dirname($dest); |
| 264 | 264 | |
| 265 | - if(!file_exists($dir) || !is_dir($dir)){ |
|
| 266 | - if(!mkdir($dir, 0755, true)){ |
|
| 265 | + if (!file_exists($dir) || !is_dir($dir)) { |
|
| 266 | + if (!mkdir($dir, 0755, true)) { |
|
| 267 | 267 | throw new ImagetilerException('cannot create folder '.$dir); |
| 268 | 268 | } |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - if($this->options->tile_format === 'jpeg'){ |
|
| 271 | + if ($this->options->tile_format === 'jpeg') { |
|
| 272 | 272 | $image->setCompression(Imagick::COMPRESSION_JPEG2000); |
| 273 | 273 | $image->setCompressionQuality($this->options->quality_jpeg); |
| 274 | 274 | } |
| 275 | 275 | |
| 276 | - if(!$image->writeImage($dest)){ |
|
| 276 | + if (!$image->writeImage($dest)) { |
|
| 277 | 277 | throw new ImagetilerException('cannot save image '.$dest); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - if($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer){ |
|
| 280 | + if ($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer) { |
|
| 281 | 281 | $this->optimizer->optimize($dest); |
| 282 | 282 | } |
| 283 | 283 | |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | */ |
| 289 | 289 | protected function clearImage(Imagick $image = null):bool{ |
| 290 | 290 | |
| 291 | - if($image instanceof Imagick){ |
|
| 291 | + if ($image instanceof Imagick) { |
|
| 292 | 292 | $image->clear(); |
| 293 | 293 | |
| 294 | 294 | return $image->destroy(); |
@@ -305,19 +305,19 @@ discard block |
||
| 305 | 305 | protected function getSize(int $width, int $height, int $zoom):array{ |
| 306 | 306 | $zoom_normalize = $this->options->zoom_normalize ?? $this->options->zoom_max; |
| 307 | 307 | |
| 308 | - if($this->options->zoom_max > $zoom_normalize && $zoom > $zoom_normalize){ |
|
| 308 | + if ($this->options->zoom_max > $zoom_normalize && $zoom > $zoom_normalize) { |
|
| 309 | 309 | $zd = 2 ** ($zoom - $zoom_normalize); |
| 310 | 310 | |
| 311 | - return [$zd * $width, $zd * $height]; |
|
| 311 | + return [ $zd * $width, $zd * $height ]; |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | - if($zoom < $zoom_normalize){ |
|
| 314 | + if ($zoom < $zoom_normalize) { |
|
| 315 | 315 | $zd = 2 ** ($zoom_normalize - $zoom); |
| 316 | 316 | |
| 317 | - return [(int)round($width / $zd), (int)round($height / $zd)]; |
|
| 317 | + return [ (int)round($width / $zd), (int)round($height / $zd) ]; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - return [$width, $height]; |
|
| 320 | + return [ $width, $height ]; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | } |