@@ -32,17 +32,17 @@ |
||
| 32 | 32 | 'minLogLevel' => 'debug', |
| 33 | 33 | ]; |
| 34 | 34 | |
| 35 | -$options = new class($options) extends ContainerAbstract{ |
|
| 35 | +$options = new class($options) extends ContainerAbstract { |
|
| 36 | 36 | use ImagetilerOptionsTrait, LogOptionsTrait; |
| 37 | 37 | }; |
| 38 | 38 | |
| 39 | 39 | $logger = (new Log)->addInstance(new ConsoleLog($options), 'console'); |
| 40 | 40 | $tiler = new Imagetiler($options, $logger); |
| 41 | 41 | |
| 42 | -try{ |
|
| 42 | +try { |
|
| 43 | 43 | $tiler->process($input, __DIR__); |
| 44 | 44 | } |
| 45 | -catch(ImagetilerException $e){ |
|
| 45 | +catch (ImagetilerException $e) { |
|
| 46 | 46 | echo $e->getMessage(); |
| 47 | 47 | echo $e->getTraceAsString(); |
| 48 | 48 | } |
@@ -41,8 +41,7 @@ |
||
| 41 | 41 | |
| 42 | 42 | try{ |
| 43 | 43 | $tiler->process($input, __DIR__); |
| 44 | -} |
|
| 45 | -catch(ImagetilerException $e){ |
|
| 44 | +} catch(ImagetilerException $e){ |
|
| 46 | 45 | echo $e->getMessage(); |
| 47 | 46 | echo $e->getTraceAsString(); |
| 48 | 47 | } |
@@ -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 |
@@ -33,6 +33,6 @@ |
||
| 33 | 33 | * @property bool $overwrite_tile_image |
| 34 | 34 | * @property bool $clean_up |
| 35 | 35 | */ |
| 36 | -class ImagetilerOptions extends ContainerAbstract{ |
|
| 36 | +class ImagetilerOptions extends ContainerAbstract { |
|
| 37 | 37 | use ImagetilerOptionsTrait; |
| 38 | 38 | } |
@@ -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 | } |
@@ -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 | /** |
@@ -37,9 +37,9 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @throws \chillerlan\Imagetiler\ImagetilerException |
| 39 | 39 | */ |
| 40 | - public function __construct(ContainerInterface $options = null, LoggerInterface $logger = null){ |
|
| 40 | + public function __construct(ContainerInterface $options = null, LoggerInterface $logger = null) { |
|
| 41 | 41 | |
| 42 | - if(!extension_loaded('imagick')){ |
|
| 42 | + if (!extension_loaded('imagick')) { |
|
| 43 | 43 | throw new ImagetilerException('Imagick extension is not available'); |
| 44 | 44 | } |
| 45 | 45 | |
@@ -60,21 +60,21 @@ discard block |
||
| 60 | 60 | $options->zoom_min = max(0, $options->zoom_min); |
| 61 | 61 | $options->zoom_max = max(1, $options->zoom_max); |
| 62 | 62 | |
| 63 | - if($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize){ |
|
| 63 | + if ($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize) { |
|
| 64 | 64 | $options->zoom_normalize = $options->zoom_max; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | $this->options = $options; |
| 68 | 68 | |
| 69 | - if(ini_set('memory_limit', $this->options->memory_limit) === false){ |
|
| 69 | + if (ini_set('memory_limit', $this->options->memory_limit) === false) { |
|
| 70 | 70 | throw new ImagetilerException('could not alter ini settings'); |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - if(ini_get('memory_limit') !== (string)$this->options->memory_limit){ |
|
| 73 | + if (ini_get('memory_limit') !== (string)$this->options->memory_limit) { |
|
| 74 | 74 | throw new ImagetilerException('ini settings differ from options'); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - if($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)){ |
|
| 77 | + if ($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)) { |
|
| 78 | 78 | apache_setenv('MAGICK_TEMPORARY_PATH', $this->options->imagick_tmp); |
| 79 | 79 | putenv('MAGICK_TEMPORARY_PATH='.$this->options->imagick_tmp); |
| 80 | 80 | } |
@@ -93,11 +93,11 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | public function process(string $image_path, string $out_path):Imagetiler{ |
| 95 | 95 | |
| 96 | - if(!is_file($image_path) || !is_readable($image_path)){ |
|
| 96 | + if (!is_file($image_path) || !is_readable($image_path)) { |
|
| 97 | 97 | throw new ImagetilerException('cannot read image '.$image_path); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - if(!is_dir($out_path) || !is_writable($out_path)){ |
|
| 100 | + if (!is_dir($out_path) || !is_writable($out_path)) { |
|
| 101 | 101 | throw new ImagetilerException('output path is not writable'); |
| 102 | 102 | } |
| 103 | 103 | |
@@ -105,12 +105,12 @@ discard block |
||
| 105 | 105 | $this->prepareZoomBaseImages($image_path, $out_path); |
| 106 | 106 | |
| 107 | 107 | // create tiles for each zoom level |
| 108 | - for($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++){ |
|
| 108 | + for ($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++) { |
|
| 109 | 109 | $this->createTilesForZoom($image_path, $out_path, $i); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // clean up base images |
| 113 | - if($this->options->clean_up){ |
|
| 113 | + if ($this->options->clean_up) { |
|
| 114 | 114 | $this->removeZoomBaseImages($out_path); |
| 115 | 115 | } |
| 116 | 116 | |
@@ -139,16 +139,16 @@ discard block |
||
| 139 | 139 | $start = true; |
| 140 | 140 | $il = null; |
| 141 | 141 | |
| 142 | - for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){ |
|
| 142 | + for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) { |
|
| 143 | 143 | $file = $out_path.'/'.$zoom.'.'.$this->ext; |
| 144 | 144 | |
| 145 | 145 | //check if already exist |
| 146 | - if(!$this->options->overwrite_base_image && is_file($file)){ |
|
| 146 | + if (!$this->options->overwrite_base_image && is_file($file)) { |
|
| 147 | 147 | $this->logger->info('base image for zoom level '.$zoom.' already exists: '.$file); |
| 148 | 148 | continue; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - [$w, $h] = $this->getSize($width, $height, $zoom); |
|
| 151 | + [ $w, $h ] = $this->getSize($width, $height, $zoom); |
|
| 152 | 152 | |
| 153 | 153 | //fit main image to current zoom lvl |
| 154 | 154 | $il = $start ? clone $im : $il; |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | $this->imageSave($il, $file); |
| 164 | 164 | |
| 165 | 165 | //clear |
| 166 | - if($start){ |
|
| 166 | + if ($start) { |
|
| 167 | 167 | $im->clear(); |
| 168 | 168 | $im->destroy(); |
| 169 | 169 | } |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | //free resurce, destroy imagick object |
| 176 | - if($il instanceof Imagick){ |
|
| 176 | + if ($il instanceof Imagick) { |
|
| 177 | 177 | $il->clear(); |
| 178 | 178 | $il->destroy(); |
| 179 | 179 | } |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | $base_image = $out_path.'/'.$zoom.'.'.$this->ext; |
| 192 | 192 | |
| 193 | 193 | //load image |
| 194 | - if(!is_file($base_image) || !is_readable($base_image)){ |
|
| 194 | + if (!is_file($base_image) || !is_readable($base_image)) { |
|
| 195 | 195 | throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom); |
| 196 | 196 | } |
| 197 | 197 | |
@@ -207,15 +207,15 @@ discard block |
||
| 207 | 207 | $y = (int)ceil($image_h / $ts); |
| 208 | 208 | |
| 209 | 209 | // width |
| 210 | - for($ix = 0; $ix < $x; $ix++){ |
|
| 210 | + for ($ix = 0; $ix < $x; $ix++) { |
|
| 211 | 211 | $cx = $ix * $ts; |
| 212 | 212 | |
| 213 | 213 | // height |
| 214 | - for($iy = 0; $iy < $y; $iy++){ |
|
| 214 | + for ($iy = 0; $iy < $y; $iy++) { |
|
| 215 | 215 | $tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->ext; |
| 216 | 216 | |
| 217 | 217 | // check if tile already exists |
| 218 | - if(!$this->options->overwrite_tile_image && is_file($tile)){ |
|
| 218 | + if (!$this->options->overwrite_tile_image && is_file($tile)) { |
|
| 219 | 219 | $this->logger->info('tile '.$zoom.'/'.$x.'/'.$y.' already exists: '.$tile); |
| 220 | 220 | |
| 221 | 221 | continue; |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | $ti->cropImage($ts, $ts, $cx, $cy); |
| 231 | 231 | |
| 232 | 232 | // check if the current tile is smaller than the tile size (leftover edges on the input image) |
| 233 | - if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){ |
|
| 233 | + if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) { |
|
| 234 | 234 | $this->fillFreeSpace($ti, $ts, $ts); |
| 235 | 235 | } |
| 236 | 236 | |
@@ -259,11 +259,11 @@ discard block |
||
| 259 | 259 | protected function removeZoomBaseImages(string $out_path):void{ |
| 260 | 260 | |
| 261 | 261 | //remove |
| 262 | - for($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++){ |
|
| 262 | + for ($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++) { |
|
| 263 | 263 | $lvl_file = $out_path.'/'.$i.'.'.$this->ext; |
| 264 | 264 | |
| 265 | - if(is_file($lvl_file)){ |
|
| 266 | - if(unlink($lvl_file)){ |
|
| 265 | + if (is_file($lvl_file)) { |
|
| 266 | + if (unlink($lvl_file)) { |
|
| 267 | 267 | $this->logger->info('deleted base image for zoom level '.$i.': '.$lvl_file); |
| 268 | 268 | } |
| 269 | 269 | } |
@@ -303,20 +303,20 @@ discard block |
||
| 303 | 303 | //prepare folder |
| 304 | 304 | $dir = dirname($dest); |
| 305 | 305 | |
| 306 | - if(!is_dir($dir)){ |
|
| 307 | - if(!mkdir($dir, 0777, true)){ |
|
| 306 | + if (!is_dir($dir)) { |
|
| 307 | + if (!mkdir($dir, 0777, true)) { |
|
| 308 | 308 | throw new ImagetilerException('cannot crate folder '.$dir); |
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | //prepare to save |
| 313 | - if($this->options->tile_format === 'jpeg'){ |
|
| 313 | + if ($this->options->tile_format === 'jpeg') { |
|
| 314 | 314 | $image->setCompression(Imagick::COMPRESSION_JPEG); |
| 315 | 315 | $image->setCompressionQuality($this->options->quality_jpeg); |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | //save image |
| 319 | - if(!$image->writeImage($dest)){ |
|
| 319 | + if (!$image->writeImage($dest)) { |
|
| 320 | 320 | throw new ImagetilerException('cannot save image '.$dest); |
| 321 | 321 | } |
| 322 | 322 | |
@@ -332,19 +332,19 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | protected function getSize(int $width, int $height, int $zoom):array{ |
| 334 | 334 | |
| 335 | - if($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize){ |
|
| 335 | + if ($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize) { |
|
| 336 | 336 | $zd = 2 ** ($zoom - $this->options->zoom_normalize); |
| 337 | 337 | |
| 338 | - return [$zd * $width, $zd * $height]; |
|
| 338 | + return [ $zd * $width, $zd * $height ]; |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - if($zoom < $this->options->zoom_normalize){ |
|
| 341 | + if ($zoom < $this->options->zoom_normalize) { |
|
| 342 | 342 | $zd = 2 ** ($this->options->zoom_normalize - $zoom); |
| 343 | 343 | |
| 344 | - return [(int)round($width / $zd), (int)round($height / $zd)]; |
|
| 344 | + return [ (int)round($width / $zd), (int)round($height / $zd) ]; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | - return [$width, $height]; |
|
| 347 | + return [ $width, $height ]; |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | /** |
@@ -356,12 +356,12 @@ discard block |
||
| 356 | 356 | protected function getExtension():string{ |
| 357 | 357 | $fmt = strtolower($this->options->tile_format); |
| 358 | 358 | |
| 359 | - if(in_array($fmt, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){ |
|
| 359 | + if (in_array($fmt, [ 'jpeg', 'jp2', 'jpc', 'jxr', ], true)) { |
|
| 360 | 360 | return 'jpg'; |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - if(in_array( |
|
| 364 | - $fmt, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){ |
|
| 363 | + if (in_array( |
|
| 364 | + $fmt, [ 'png', 'png00', 'png8', 'png24', 'png32', 'png64', ], true)) { |
|
| 365 | 365 | return 'png'; |
| 366 | 366 | } |
| 367 | 367 | |