Completed
Push — master ( 2b43ba...9fe5da )
by smiley
02:42
created
src/Imagetiler.php 1 patch
Spacing   +35 added lines, -35 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(SettingsContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null){
42
+	public function __construct(SettingsContainerInterface $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
 	}
@@ -62,15 +62,15 @@  discard block
 block discarded – undo
62 62
 	public function setOptions(SettingsContainerInterface $options):Imagetiler{
63 63
 		$this->options = $options;
64 64
 
65
-		if(ini_set('memory_limit', $this->options->memory_limit) === false){
65
+		if (ini_set('memory_limit', $this->options->memory_limit) === false) {
66 66
 			throw new ImagetilerException('could not alter ini settings');
67 67
 		}
68 68
 
69
-		if(ini_get('memory_limit') !== (string)$this->options->memory_limit){
69
+		if (ini_get('memory_limit') !== (string)$this->options->memory_limit) {
70 70
 			throw new ImagetilerException('ini settings differ from options');
71 71
 		}
72 72
 
73
-		if($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)){
73
+		if ($this->options->imagick_tmp !== null && is_dir($this->options->imagick_tmp)) {
74 74
 			apache_setenv('MAGICK_TEMPORARY_PATH', $this->options->imagick_tmp);
75 75
 			putenv('MAGICK_TEMPORARY_PATH='.$this->options->imagick_tmp);
76 76
 		}
@@ -98,13 +98,13 @@  discard block
 block discarded – undo
98 98
 	 */
99 99
 	public function process(string $image_path, string $out_path):Imagetiler{
100 100
 
101
-		if(!is_file($image_path) || !is_readable($image_path)){
101
+		if (!is_file($image_path) || !is_readable($image_path)) {
102 102
 			throw new ImagetilerException('cannot read image '.$image_path);
103 103
 		}
104 104
 
105
-		if(!is_dir($out_path)|| !is_writable($out_path)){
105
+		if (!is_dir($out_path) || !is_writable($out_path)) {
106 106
 
107
-			if(!mkdir($out_path, 0755, true)){
107
+			if (!mkdir($out_path, 0755, true)) {
108 108
 				throw new ImagetilerException('output path is not writable');
109 109
 			}
110 110
 
@@ -114,12 +114,12 @@  discard block
 block discarded – undo
114 114
 		$this->prepareZoomBaseImages($image_path, $out_path);
115 115
 
116 116
 		// create the tiles
117
-		for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){
117
+		for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) {
118 118
 
119 119
 			$base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext;
120 120
 
121 121
 			//load image
122
-			if(!is_file($base_image) || !is_readable($base_image)){
122
+			if (!is_file($base_image) || !is_readable($base_image)) {
123 123
 				throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom);
124 124
 			}
125 125
 
@@ -127,13 +127,13 @@  discard block
 block discarded – undo
127 127
 		}
128 128
 
129 129
 		// clean up base images
130
-		if($this->options->clean_up){
130
+		if ($this->options->clean_up) {
131 131
 
132
-			for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){
132
+			for ($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++) {
133 133
 				$lvl_file = $out_path.'/'.$zoom.'.'.$this->options->tile_ext;
134 134
 
135
-				if(is_file($lvl_file)){
136
-					if(unlink($lvl_file)){
135
+				if (is_file($lvl_file)) {
136
+					if (unlink($lvl_file)) {
137 137
 						$this->logger->info('deleted base image for zoom level '.$zoom.': '.$lvl_file);
138 138
 					}
139 139
 				}
@@ -162,16 +162,16 @@  discard block
 block discarded – undo
162 162
 		$start = true;
163 163
 		$il    = null;
164 164
 
165
-		for($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--){
165
+		for ($zoom = $this->options->zoom_max; $zoom >= $this->options->zoom_min; $zoom--) {
166 166
 			$base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext;
167 167
 
168 168
 			// check if the base image already exists
169
-			if(!$this->options->overwrite_base_image && is_file($base_image)){
169
+			if (!$this->options->overwrite_base_image && is_file($base_image)) {
170 170
 				$this->logger->info('base image for zoom level '.$zoom.' already exists: '.$base_image);
171 171
 				continue;
172 172
 			}
173 173
 
174
-			[$w, $h] = $this->getSize($width, $height, $zoom);
174
+			[ $w, $h ] = $this->getSize($width, $height, $zoom);
175 175
 
176 176
 			// fit main image to current zoom level
177 177
 			$il = $start ? clone $im : $il;
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
 			// save without optimizing
186 186
 			$this->saveImage($il, $base_image, false);
187 187
 
188
-			if($start){
188
+			if ($start) {
189 189
 				$this->clearImage($im);
190 190
 			}
191 191
 
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
 		$y  = (int)ceil($h / $ts);
213 213
 
214 214
 		// width
215
-		for($ix = 0; $ix < $x; $ix++){
215
+		for ($ix = 0; $ix < $x; $ix++) {
216 216
 			$cx = $ix * $ts;
217 217
 
218 218
 			// create a stripe tile_size * height
@@ -220,11 +220,11 @@  discard block
 block discarded – undo
220 220
 			$ci->cropImage($ts, $h, $cx, 0);
221 221
 
222 222
 			// height
223
-			for($iy = 0; $iy < $y; $iy++){
223
+			for ($iy = 0; $iy < $y; $iy++) {
224 224
 				$tile = $out_path.'/'.sprintf($this->options->store_structure, $zoom, $ix, $iy).'.'.$this->options->tile_ext;
225 225
 
226 226
 				// check if tile already exists
227
-				if(!$this->options->overwrite_tile_image && is_file($tile)){
227
+				if (!$this->options->overwrite_tile_image && is_file($tile)) {
228 228
 					$this->logger->info('tile '.$zoom.'/'.$x.'/'.$y.' already exists: '.$tile);
229 229
 
230 230
 					continue;
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
 				$ti->cropImage($ts, $ts, 0, $cy);
241 241
 
242 242
 				// check if the current tile is smaller than the tile size (leftover edges on the input image)
243
-				if($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts){
243
+				if ($ti->getImageWidth() < $ts || $ti->getimageheight() < $ts) {
244 244
 
245 245
 					$th = $this->options->tms ? $h - $ts : 0;
246 246
 
@@ -273,22 +273,22 @@  discard block
 block discarded – undo
273 273
 	protected function saveImage(Imagick $image, string $dest, bool $optimize):void{
274 274
 		$dir = dirname($dest);
275 275
 
276
-		if(!is_dir($dir)){
277
-			if(!mkdir($dir, 0755, true)){
276
+		if (!is_dir($dir)) {
277
+			if (!mkdir($dir, 0755, true)) {
278 278
 				throw new ImagetilerException('cannot create folder '.$dir);
279 279
 			}
280 280
 		}
281 281
 
282
-		if($this->options->tile_format === 'jpeg'){
282
+		if ($this->options->tile_format === 'jpeg') {
283 283
 			$image->setCompression(Imagick::COMPRESSION_JPEG);
284 284
 			$image->setCompressionQuality($this->options->quality_jpeg);
285 285
 		}
286 286
 
287
-		if(!$image->writeImage($dest)){
287
+		if (!$image->writeImage($dest)) {
288 288
 			throw new ImagetilerException('cannot save image '.$dest);
289 289
 		}
290 290
 
291
-		if($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer){
291
+		if ($this->options->optimize_output && $optimize && $this->optimizer instanceof Optimizer) {
292 292
 			$this->optimizer->optimize($dest);
293 293
 		}
294 294
 
@@ -303,7 +303,7 @@  discard block
 block discarded – undo
303 303
 	 */
304 304
 	protected function clearImage(Imagick $image = null):bool{
305 305
 
306
-		if($image instanceof Imagick){
306
+		if ($image instanceof Imagick) {
307 307
 			$image->clear();
308 308
 
309 309
 			return $image->destroy();
@@ -323,19 +323,19 @@  discard block
 block discarded – undo
323 323
 	 */
324 324
 	protected function getSize(int $width, int $height, int $zoom):array{
325 325
 
326
-		if($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize){
326
+		if ($this->options->zoom_max > $this->options->zoom_normalize && $zoom > $this->options->zoom_normalize) {
327 327
 			$zd = 2 ** ($zoom - $this->options->zoom_normalize);
328 328
 
329
-			return [$zd * $width, $zd * $height];
329
+			return [ $zd * $width, $zd * $height ];
330 330
 		}
331 331
 
332
-		if($zoom < $this->options->zoom_normalize){
332
+		if ($zoom < $this->options->zoom_normalize) {
333 333
 			$zd = 2 ** ($this->options->zoom_normalize - $zoom);
334 334
 
335
-			return [(int)round($width / $zd), (int)round($height / $zd)];
335
+			return [ (int)round($width / $zd), (int)round($height / $zd) ];
336 336
 		}
337 337
 
338
-		return [$width, $height];
338
+		return [ $width, $height ];
339 339
 	}
340 340
 
341 341
 }
Please login to merge, or discard this patch.