@@ -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 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | use Imagick; |
| 19 | 19 | use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger}; |
| 20 | 20 | |
| 21 | -class Imagetiler implements LoggerAwareInterface{ |
|
| 21 | +class Imagetiler implements LoggerAwareInterface { |
|
| 22 | 22 | use LoggerAwareTrait; |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -34,9 +34,9 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @throws \chillerlan\Imagetiler\ImagetilerException |
| 36 | 36 | */ |
| 37 | - public function __construct(ContainerInterface $options = null, LoggerInterface $logger = null){ |
|
| 37 | + public function __construct(ContainerInterface $options = null, LoggerInterface $logger = null) { |
|
| 38 | 38 | |
| 39 | - if(!extension_loaded('imagick')){ |
|
| 39 | + if (!extension_loaded('imagick')) { |
|
| 40 | 40 | throw new ImagetilerException('Imagick extension is not available'); |
| 41 | 41 | } |
| 42 | 42 | |
@@ -54,25 +54,25 @@ discard block |
||
| 54 | 54 | $options->zoom_min = max(0, $options->zoom_min); |
| 55 | 55 | $options->zoom_max = max(1, $options->zoom_max); |
| 56 | 56 | |
| 57 | - if($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize){ |
|
| 57 | + if ($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize) { |
|
| 58 | 58 | $options->zoom_normalize = $options->zoom_max; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - if($options->tile_ext === null){ |
|
| 61 | + if ($options->tile_ext === null) { |
|
| 62 | 62 | $options->tile_ext = $this->getExtension($options->tile_format); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | $this->options = $options; |
| 66 | 66 | |
| 67 | - if(ini_set('memory_limit', $this->options->memory_limit) === false){ |
|
| 67 | + if (ini_set('memory_limit', $this->options->memory_limit) === false) { |
|
| 68 | 68 | throw new ImagetilerException('could not alter ini settings'); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - if(ini_get('memory_limit') !== (string)$this->options->memory_limit){ |
|
| 71 | + if (ini_get('memory_limit') !== (string)$this->options->memory_limit) { |
|
| 72 | 72 | throw new ImagetilerException('ini settings differ from options'); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - if($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)){ |
|
| 75 | + if ($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)) { |
|
| 76 | 76 | apache_setenv('MAGICK_TEMPORARY_PATH', $this->options->imagick_tmp); |
| 77 | 77 | putenv('MAGICK_TEMPORARY_PATH='.$this->options->imagick_tmp); |
| 78 | 78 | } |
@@ -90,13 +90,13 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public function process(string $image_path, string $out_path):Imagetiler{ |
| 92 | 92 | |
| 93 | - if(!is_file($image_path) || !is_readable($image_path)){ |
|
| 93 | + if (!is_file($image_path) || !is_readable($image_path)) { |
|
| 94 | 94 | throw new ImagetilerException('cannot read image '.$image_path); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if(!is_dir($out_path)|| !is_writable($out_path)){ |
|
| 97 | + if (!is_dir($out_path) || !is_writable($out_path)) { |
|
| 98 | 98 | |
| 99 | - if(!mkdir($out_path, 0755, true)){ |
|
| 99 | + if (!mkdir($out_path, 0755, true)) { |
|
| 100 | 100 | throw new ImagetilerException('output path is not writable'); |
| 101 | 101 | } |
| 102 | 102 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | // load the optional image optimizer |
| 106 | 106 | $optimizer = null; |
| 107 | 107 | |
| 108 | - if($this->options->optimize_output){ |
|
| 108 | + if ($this->options->optimize_output) { |
|
| 109 | 109 | $optimizer = (new OptimizerFactory($this->options->optimizer_settings, $this->logger))->get(); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -113,12 +113,12 @@ discard block |
||
| 113 | 113 | $this->prepareZoomBaseImages($image_path, $out_path); |
| 114 | 114 | |
| 115 | 115 | // create the tiles |
| 116 | - for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){ |
|
| 116 | + for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) { |
|
| 117 | 117 | |
| 118 | 118 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 119 | 119 | |
| 120 | 120 | //load image |
| 121 | - if(!is_file($base_image) || !is_readable($base_image)){ |
|
| 121 | + if (!is_file($base_image) || !is_readable($base_image)) { |
|
| 122 | 122 | throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom); |
| 123 | 123 | } |
| 124 | 124 | |
@@ -126,13 +126,13 @@ discard block |
||
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // clean up base images |
| 129 | - if($this->options->clean_up){ |
|
| 129 | + if ($this->options->clean_up) { |
|
| 130 | 130 | |
| 131 | - for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){ |
|
| 131 | + for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) { |
|
| 132 | 132 | $lvl_file = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 133 | 133 | |
| 134 | - if(is_file($lvl_file)){ |
|
| 135 | - if(unlink($lvl_file)){ |
|
| 134 | + if (is_file($lvl_file)) { |
|
| 135 | + if (unlink($lvl_file)) { |
|
| 136 | 136 | $this->logger->info('deleted base image for zoom level '.$zoom.': '.$lvl_file); |
| 137 | 137 | } |
| 138 | 138 | } |
@@ -161,16 +161,16 @@ discard block |
||
| 161 | 161 | $start = true; |
| 162 | 162 | $il = null; |
| 163 | 163 | |
| 164 | - for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){ |
|
| 164 | + for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) { |
|
| 165 | 165 | $base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext; |
| 166 | 166 | |
| 167 | 167 | // check if the base image already exists |
| 168 | - if(!$this->options->overwrite_base_image && is_file($base_image)){ |
|
| 168 | + if (!$this->options->overwrite_base_image && is_file($base_image)) { |
|
| 169 | 169 | $this->logger->info('base image for zoom level '.$zoom.' already exists: '.$base_image); |
| 170 | 170 | continue; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - [$w, $h] = $this->getSize($width, $height, $zoom); |
|
| 173 | + [ $w, $h ] = $this->getSize($width, $height, $zoom); |
|
| 174 | 174 | |
| 175 | 175 | // fit main image to current zoom level |
| 176 | 176 | $il = $start ? clone $im : $il; |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | |
| 184 | 184 | $this->saveImage($il, $base_image); |
| 185 | 185 | |
| 186 | - if($start){ |
|
| 186 | + if ($start) { |
|
| 187 | 187 | $this->clearImage($im); |
| 188 | 188 | } |
| 189 | 189 | |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | $y = (int)ceil($h / $ts); |
| 215 | 215 | |
| 216 | 216 | // width |
| 217 | - for($ix = 0; $ix < $x; $ix++){ |
|
| 217 | + for ($ix = 0; $ix < $x; $ix++) { |
|
| 218 | 218 | $cx = $ix * $ts; |
| 219 | 219 | |
| 220 | 220 | // create a stripe tile_size * height |
@@ -222,11 +222,11 @@ discard block |
||
| 222 | 222 | $ci->cropImage($ts, $h, $cx, 0); |
| 223 | 223 | |
| 224 | 224 | // height |
| 225 | - for($iy = 0; $iy < $y; $iy++){ |
|
| 225 | + for ($iy = 0; $iy < $y; $iy++) { |
|
| 226 | 226 | $tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext; |
| 227 | 227 | |
| 228 | 228 | // check if tile already exists |
| 229 | - if(!$this->options->overwrite_tile_image && is_file($tile)){ |
|
| 229 | + if (!$this->options->overwrite_tile_image && is_file($tile)) { |
|
| 230 | 230 | $this->logger->info('tile '.$zoom.'/'.$x.'/'.$y.' already exists: '.$tile); |
| 231 | 231 | |
| 232 | 232 | continue; |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | $ti->cropImage($ts, $ts, 0, $cy); |
| 243 | 243 | |
| 244 | 244 | // check if the current tile is smaller than the tile size (leftover edges on the input image) |
| 245 | - if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){ |
|
| 245 | + if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) { |
|
| 246 | 246 | |
| 247 | 247 | $th = $this->options->tms |
| 248 | 248 | ? $im->getImageHeight() - $ts |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | $this->clearImage($ci); |
| 260 | - $this->logger->info('created column '.($ix+1).', x = '.$cx); |
|
| 260 | + $this->logger->info('created column '.($ix + 1).', x = '.$cx); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | $this->clearImage($im); |
@@ -277,22 +277,22 @@ discard block |
||
| 277 | 277 | protected function saveImage(Imagick $image, string $dest, Optimizer $optimizer = null):void{ |
| 278 | 278 | $dir = dirname($dest); |
| 279 | 279 | |
| 280 | - if(!is_dir($dir)){ |
|
| 281 | - if(!mkdir($dir, 0755, true)){ |
|
| 280 | + if (!is_dir($dir)) { |
|
| 281 | + if (!mkdir($dir, 0755, true)) { |
|
| 282 | 282 | throw new ImagetilerException('cannot crate folder '.$dir); |
| 283 | 283 | } |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - if($this->options->tile_format === 'jpeg'){ |
|
| 286 | + if ($this->options->tile_format === 'jpeg') { |
|
| 287 | 287 | $image->setCompression(Imagick::COMPRESSION_JPEG); |
| 288 | 288 | $image->setCompressionQuality($this->options->quality_jpeg); |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | - if(!$image->writeImage($dest)){ |
|
| 291 | + if (!$image->writeImage($dest)) { |
|
| 292 | 292 | throw new ImagetilerException('cannot save image '.$dest); |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - if($optimizer instanceof Optimizer){ |
|
| 295 | + if ($optimizer instanceof Optimizer) { |
|
| 296 | 296 | $optimizer->optimize($dest); |
| 297 | 297 | } |
| 298 | 298 | |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | */ |
| 308 | 308 | protected function clearImage(Imagick $image = null):bool{ |
| 309 | 309 | |
| 310 | - if($image instanceof Imagick){ |
|
| 310 | + if ($image instanceof Imagick) { |
|
| 311 | 311 | $image->clear(); |
| 312 | 312 | |
| 313 | 313 | return $image->destroy(); |
@@ -327,19 +327,19 @@ discard block |
||
| 327 | 327 | */ |
| 328 | 328 | protected function getSize(int $width, int $height, int $zoom):array{ |
| 329 | 329 | |
| 330 | - if($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize){ |
|
| 330 | + if ($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize) { |
|
| 331 | 331 | $zd = 2 ** ($zoom - $this->options->zoom_normalize); |
| 332 | 332 | |
| 333 | - return [$zd * $width, $zd * $height]; |
|
| 333 | + return [ $zd * $width, $zd * $height ]; |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | - if($zoom < $this->options->zoom_normalize){ |
|
| 336 | + if ($zoom < $this->options->zoom_normalize) { |
|
| 337 | 337 | $zd = 2 ** ($this->options->zoom_normalize - $zoom); |
| 338 | 338 | |
| 339 | - return [(int)round($width / $zd), (int)round($height / $zd)]; |
|
| 339 | + return [ (int)round($width / $zd), (int)round($height / $zd) ]; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - return [$width, $height]; |
|
| 342 | + return [ $width, $height ]; |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | /** |
@@ -352,11 +352,11 @@ discard block |
||
| 352 | 352 | */ |
| 353 | 353 | protected function getExtension(string $format):string{ |
| 354 | 354 | |
| 355 | - if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){ |
|
| 355 | + if (in_array($format, [ 'jpeg', 'jp2', 'jpc', 'jxr', ], true)) { |
|
| 356 | 356 | return 'jpg'; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){ |
|
| 359 | + if (in_array($format, [ 'png', 'png00', 'png8', 'png24', 'png32', 'png64', ], true)) { |
|
| 360 | 360 | return 'png'; |
| 361 | 361 | } |
| 362 | 362 | |
@@ -14,7 +14,7 @@ discard block |
||
| 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 |
@@ -155,5 +155,5 @@ discard block |
||
| 155 | 155 | /** |
| 156 | 156 | * @var array |
| 157 | 157 | */ |
| 158 | - protected $optimizer_settings = []; |
|
| 158 | + protected $optimizer_settings = [ ]; |
|
| 159 | 159 | } |
@@ -36,6 +36,6 @@ |
||
| 36 | 36 | * @property bool $optimize_output |
| 37 | 37 | * @property array $optimizer_settings |
| 38 | 38 | */ |
| 39 | -class ImagetilerOptions extends ContainerAbstract{ |
|
| 39 | +class ImagetilerOptions extends ContainerAbstract { |
|
| 40 | 40 | use ImagetilerOptionsTrait; |
| 41 | 41 | } |