Completed
Push — master ( 3d683b...c546ba )
by smiley
02:34
created
src/Imagetiler.php 1 patch
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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;
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 			// save without optimizing
198 198
 			$this->saveImage($il, $base_image, false);
199 199
 
200
-			if($start){
200
+			if ($start) {
201 201
 				$this->clearImage($im);
202 202
 			}
203 203
 
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
 		$y  = (int)ceil($h / $ts);
225 225
 
226 226
 		// width
227
-		for($ix = 0; $ix < $x; $ix++){
227
+		for ($ix = 0; $ix < $x; $ix++) {
228 228
 			$cx = $ix * $ts;
229 229
 
230 230
 			// create a stripe tile_size * height
@@ -232,11 +232,11 @@  discard block
 block discarded – undo
232 232
 			$ci->cropImage($ts, $h, $cx, 0);
233 233
 
234 234
 			// height
235
-			for($iy = 0; $iy < $y; $iy++){
235
+			for ($iy = 0; $iy < $y; $iy++) {
236 236
 				$tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext;
237 237
 
238 238
 				// check if tile already exists
239
-				if(!$this->options->overwrite_tile_image && is_file($tile)){
239
+				if (!$this->options->overwrite_tile_image && is_file($tile)) {
240 240
 					$this->logger->info('tile '.$zoom.'/'.$x.'/'.$y.' already exists: '.$tile);
241 241
 
242 242
 					continue;
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 				$ti->cropImage($ts, $ts, 0, $cy);
253 253
 
254 254
 				// check if the current tile is smaller than the tile size (leftover edges on the input image)
255
-				if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){
255
+				if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) {
256 256
 
257 257
 					$th = $this->options->tms ? $h - $ts : 0;
258 258
 
@@ -285,22 +285,22 @@  discard block
 block discarded – undo
285 285
 	protected function saveImage(Imagick $image, string $dest, bool $optimize):void{
286 286
 		$dir = dirname($dest);
287 287
 
288
-		if(!is_dir($dir)){
289
-			if(!mkdir($dir, 0755, true)){
288
+		if (!is_dir($dir)) {
289
+			if (!mkdir($dir, 0755, true)) {
290 290
 				throw new ImagetilerException('cannot crate folder '.$dir);
291 291
 			}
292 292
 		}
293 293
 
294
-		if($this->options->tile_format === 'jpeg'){
294
+		if ($this->options->tile_format === 'jpeg') {
295 295
 			$image->setCompression(Imagick::COMPRESSION_JPEG);
296 296
 			$image->setCompressionQuality($this->options->quality_jpeg);
297 297
 		}
298 298
 
299
-		if(!$image->writeImage($dest)){
299
+		if (!$image->writeImage($dest)) {
300 300
 			throw new ImagetilerException('cannot save image '.$dest);
301 301
 		}
302 302
 
303
-		if($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer){
303
+		if ($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer) {
304 304
 			$this->optimizer->optimize($dest);
305 305
 		}
306 306
 
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
 	 */
316 316
 	protected function clearImage(Imagick $image = null):bool{
317 317
 
318
-		if($image instanceof Imagick){
318
+		if ($image instanceof Imagick) {
319 319
 			$image->clear();
320 320
 
321 321
 			return $image->destroy();
@@ -335,19 +335,19 @@  discard block
 block discarded – undo
335 335
 	 */
336 336
 	protected function getSize(int $width, int $height, int $zoom):array{
337 337
 
338
-		if($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize){
338
+		if ($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize) {
339 339
 			$zd = 2 ** ($zoom - $this->options->zoom_normalize);
340 340
 
341
-			return [$zd * $width, $zd * $height];
341
+			return [ $zd * $width, $zd * $height ];
342 342
 		}
343 343
 
344
-		if($zoom < $this->options->zoom_normalize){
344
+		if ($zoom < $this->options->zoom_normalize) {
345 345
 			$zd = 2 ** ($this->options->zoom_normalize - $zoom);
346 346
 
347
-			return [(int)round($width / $zd), (int)round($height / $zd)];
347
+			return [ (int)round($width / $zd), (int)round($height / $zd) ];
348 348
 		}
349 349
 
350
-		return [$width, $height];
350
+		return [ $width, $height ];
351 351
 	}
352 352
 
353 353
 	/**
@@ -360,11 +360,11 @@  discard block
 block discarded – undo
360 360
 	 */
361 361
 	protected function getExtension(string $format):string{
362 362
 
363
-		if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){
363
+		if (in_array($format, [ 'jpeg', 'jp2', 'jpc', 'jxr', ], true)) {
364 364
 			return 'jpg';
365 365
 		}
366 366
 
367
-		if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){
367
+		if (in_array($format, [ 'png', 'png00', 'png8', 'png24', 'png32', 'png64', ], true)) {
368 368
 			return 'png';
369 369
 		}
370 370
 
Please login to merge, or discard this patch.