Completed
Push — master ( 3f2aa7...277f2b )
by Mark
25s queued 11s
created
StaticMap.php 1 patch
Spacing   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 
22 22
 // phpcs:disable PSR1.Files.SideEffects
23 23
 // TODO resolve side effect
24
-include_once(realpath(__DIR__) . '/../geophp/geoPHP/geoPHP.inc');
24
+include_once(realpath(__DIR__).'/../geophp/geoPHP/geoPHP.inc');
25 25
 
26 26
 /**
27 27
  *
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
         $this->height = $height;
206 206
         // validate + set maptype
207 207
         $this->maptype = $this->tileDefaultSrc;
208
-        if(array_key_exists($maptype, $this->tileInfo)) {
208
+        if (array_key_exists($maptype, $this->tileInfo)) {
209 209
             $this->maptype = $maptype;
210 210
         }
211 211
         $this->markers          = $markers;
@@ -213,9 +213,9 @@  discard block
 block discarded – undo
213 213
         $this->gpxFileName      = $gpx;
214 214
         $this->geojsonFileName  = $geojson;
215 215
         $this->mediaBaseDir     = $mediaDir;
216
-        $this->tileCacheBaseDir = $tileCacheBaseDir . '/olmaptiles';
216
+        $this->tileCacheBaseDir = $tileCacheBaseDir.'/olmaptiles';
217 217
         $this->useTileCache     = $this->tileCacheBaseDir !== '';
218
-        $this->mapCacheBaseDir  = $mediaDir . '/olmapmaps';
218
+        $this->mapCacheBaseDir  = $mediaDir.'/olmapmaps';
219 219
         $this->autoZoomExtent   = $autoZoomExtent;
220 220
         $this->apikey           = $apikey;
221 221
     }
@@ -227,15 +227,15 @@  discard block
 block discarded – undo
227 227
      */
228 228
     public function getMap(): string {
229 229
         try {
230
-            if($this->autoZoomExtent) {
230
+            if ($this->autoZoomExtent) {
231 231
                 $this->autoZoom();
232 232
             }
233
-        } catch(Exception $e) {
233
+        } catch (Exception $e) {
234 234
             dbglog($e);
235 235
         }
236 236
 
237 237
         // use map cache, so check cache for map
238
-        if(!$this->checkMapCache()) {
238
+        if (!$this->checkMapCache()) {
239 239
             // map is not in cache, needs to be build
240 240
             $this->makeMap();
241 241
             $this->mkdirRecursive(dirname($this->mapCacheIDToFilename()), 0777);
@@ -258,37 +258,37 @@  discard block
 block discarded – undo
258 258
      */
259 259
     private function autoZoom(float $paddingFactor = 1.0): void {
260 260
         $geoms    = array();
261
-        $geoms [] = new Point ($this->lon, $this->lat);
262
-        if(!empty ($this->markers)) {
263
-            foreach($this->markers as $marker) {
264
-                $geoms [] = new Point ($marker ['lon'], $marker ['lat']);
261
+        $geoms [] = new Point($this->lon, $this->lat);
262
+        if (!empty ($this->markers)) {
263
+            foreach ($this->markers as $marker) {
264
+                $geoms [] = new Point($marker ['lon'], $marker ['lat']);
265 265
             }
266 266
         }
267
-        if(file_exists($this->kmlFileName)) {
267
+        if (file_exists($this->kmlFileName)) {
268 268
             $g = geoPHP::load(file_get_contents($this->kmlFileName), 'kml');
269
-            if($g !== false) {
269
+            if ($g !== false) {
270 270
                 $geoms [] = $g;
271 271
             }
272 272
         }
273
-        if(file_exists($this->gpxFileName)) {
273
+        if (file_exists($this->gpxFileName)) {
274 274
             $g = geoPHP::load(file_get_contents($this->gpxFileName), 'gpx');
275
-            if($g !== false) {
275
+            if ($g !== false) {
276 276
                 $geoms [] = $g;
277 277
             }
278 278
         }
279
-        if(file_exists($this->geojsonFileName)) {
279
+        if (file_exists($this->geojsonFileName)) {
280 280
             $g = geoPHP::load(file_get_contents($this->geojsonFileName), 'geojson');
281
-            if($g !== false) {
281
+            if ($g !== false) {
282 282
                 $geoms [] = $g;
283 283
             }
284 284
         }
285 285
 
286
-        if(count($geoms) <= 1) {
286
+        if (count($geoms) <= 1) {
287 287
             dbglog($geoms, "StaticMap::autoZoom: Skip setting autozoom options");
288 288
             return;
289 289
         }
290 290
 
291
-        $geom     = new GeometryCollection ($geoms);
291
+        $geom     = new GeometryCollection($geoms);
292 292
         $centroid = $geom->centroid();
293 293
         $bbox     = $geom->getBBox();
294 294
 
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
         $resolution           = max($resolutionHorizontal, $resolutionVertical) * $paddingFactor;
305 305
         $zoom                 = log(360 / ($resolution * $this->tileSize), 2);
306 306
 
307
-        if(is_finite($zoom) && $zoom < 15 && $zoom > 2) {
307
+        if (is_finite($zoom) && $zoom < 15 && $zoom > 2) {
308 308
             $this->zoom = floor($zoom);
309 309
         }
310 310
         $this->lon = $centroid->getX();
@@ -337,12 +337,12 @@  discard block
 block discarded – undo
337 337
     }
338 338
 
339 339
     public function mapCacheIDToFilename(): string {
340
-        if(!$this->mapCacheFile) {
341
-            $this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_"
342
-                . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2)
343
-                . "/" . substr($this->mapCacheID, 4);
340
+        if (!$this->mapCacheFile) {
341
+            $this->mapCacheFile = $this->mapCacheBaseDir."/".$this->maptype."/".$this->zoom."/cache_"
342
+                . substr($this->mapCacheID, 0, 2)."/".substr($this->mapCacheID, 2, 2)
343
+                . "/".substr($this->mapCacheID, 4);
344 344
         }
345
-        return $this->mapCacheFile . "." . $this->mapCacheExtension;
345
+        return $this->mapCacheFile.".".$this->mapCacheExtension;
346 346
     }
347 347
 
348 348
     /**
@@ -351,13 +351,13 @@  discard block
 block discarded – undo
351 351
     public function makeMap(): void {
352 352
         $this->initCoords();
353 353
         $this->createBaseMap();
354
-        if(!empty ($this->markers))
354
+        if (!empty ($this->markers))
355 355
             $this->placeMarkers();
356
-        if(file_exists($this->kmlFileName))
356
+        if (file_exists($this->kmlFileName))
357 357
             $this->drawKML();
358
-        if(file_exists($this->gpxFileName))
358
+        if (file_exists($this->gpxFileName))
359 359
             $this->drawGPX();
360
-        if(file_exists($this->geojsonFileName))
360
+        if (file_exists($this->geojsonFileName))
361 361
             $this->drawGeojson();
362 362
 
363 363
         $this->drawCopyright();
@@ -408,8 +408,8 @@  discard block
 block discarded – undo
408 408
         $this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
409 409
         $this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;
410 410
 
411
-        for($x = $startX; $x <= $endX; $x++) {
412
-            for($y = $startY; $y <= $endY; $y++) {
411
+        for ($x = $startX; $x <= $endX; $x++) {
412
+            for ($y = $startY; $y <= $endY; $y++) {
413 413
                 $url = str_replace(
414 414
                     array(
415 415
                         '{Z}',
@@ -423,7 +423,7 @@  discard block
 block discarded – undo
423 423
                 );
424 424
 
425 425
                 $tileData = $this->fetchTile($url);
426
-                if($tileData) {
426
+                if ($tileData) {
427 427
                     $tileImage = imagecreatefromstring($tileData);
428 428
                 } else {
429 429
                     $tileImage = imagecreate($this->tileSize, $this->tileSize);
@@ -449,17 +449,17 @@  discard block
 block discarded – undo
449 449
      *          for better proxy handling...
450 450
      */
451 451
     public function fetchTile(string $url) {
452
-        if($this->useTileCache && ($cached = $this->checkTileCache($url)))
452
+        if ($this->useTileCache && ($cached = $this->checkTileCache($url)))
453 453
             return $cached;
454 454
 
455
-        $_UA = 'Mozilla/4.0 (compatible; DokuWikiSpatial HTTP Client; ' . PHP_OS . ')';
456
-        if(function_exists("curl_init")) {
455
+        $_UA = 'Mozilla/4.0 (compatible; DokuWikiSpatial HTTP Client; '.PHP_OS.')';
456
+        if (function_exists("curl_init")) {
457 457
             // use cUrl
458 458
             $ch = curl_init();
459 459
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
460 460
             curl_setopt($ch, CURLOPT_USERAGENT, $_UA);
461 461
             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
462
-            curl_setopt($ch, CURLOPT_URL, $url . $this->apikey);
462
+            curl_setopt($ch, CURLOPT_URL, $url.$this->apikey);
463 463
             dbglog("StaticMap::fetchTile: getting: $url using curl_exec");
464 464
             $tile = curl_exec($ch);
465 465
             curl_close($ch);
@@ -469,21 +469,21 @@  discard block
 block discarded – undo
469 469
             $opts = array(
470 470
                 'http' => array(
471 471
                     'method'          => "GET",
472
-                    'header'          => "Accept-language: en\r\n" . "User-Agent: $_UA\r\n" . "accept: image/png\r\n",
472
+                    'header'          => "Accept-language: en\r\n"."User-Agent: $_UA\r\n"."accept: image/png\r\n",
473 473
                     'request_fulluri' => true
474 474
                 )
475 475
             );
476
-            if(isset($conf['proxy']['host'], $conf['proxy']['port'])
476
+            if (isset($conf['proxy']['host'], $conf['proxy']['port'])
477 477
                 && $conf['proxy']['host'] !== ''
478 478
                 && $conf['proxy']['port'] !== '') {
479
-                $opts['http'] += ['proxy' => "tcp://" . $conf['proxy']['host'] . ":" . $conf['proxy']['port']];
479
+                $opts['http'] += ['proxy' => "tcp://".$conf['proxy']['host'].":".$conf['proxy']['port']];
480 480
             }
481 481
 
482 482
             $context = stream_context_create($opts);
483 483
             dbglog("StaticMap::fetchTile: getting: $url . $this->apikey using file_get_contents and options $opts");
484
-            $tile = file_get_contents($url . $this->apikey, false, $context);
484
+            $tile = file_get_contents($url.$this->apikey, false, $context);
485 485
         }
486
-        if($tile && $this->useTileCache) {
486
+        if ($tile && $this->useTileCache) {
487 487
             $this->writeTileToCache($url, $tile);
488 488
         }
489 489
         return $tile;
@@ -496,7 +496,7 @@  discard block
 block discarded – undo
496 496
      */
497 497
     public function checkTileCache(string $url) {
498 498
         $filename = $this->tileUrlToFilename($url);
499
-        if(file_exists($filename)) {
499
+        if (file_exists($filename)) {
500 500
             return file_get_contents($filename);
501 501
         }
502 502
         return false;
@@ -508,7 +508,7 @@  discard block
 block discarded – undo
508 508
      * @return string
509 509
      */
510 510
     public function tileUrlToFilename(string $url): string {
511
-        return $this->tileCacheBaseDir . "/" . substr($url, strpos($url, '/') + 1);
511
+        return $this->tileCacheBaseDir."/".substr($url, strpos($url, '/') + 1);
512 512
     }
513 513
 
514 514
     /**
@@ -543,9 +543,9 @@  discard block
 block discarded – undo
543 543
         $count         = 0;
544 544
         $color         = imagecolorallocate($this->image, 0, 0, 0);
545 545
         $bgcolor       = imagecolorallocate($this->image, 200, 200, 200);
546
-        $markerBaseDir = __DIR__ . '/icons';
546
+        $markerBaseDir = __DIR__.'/icons';
547 547
         // loop thru marker array
548
-        foreach($this->markers as $marker) {
548
+        foreach ($this->markers as $marker) {
549 549
             // set some local variables
550 550
             $markerLat  = $marker ['lat'];
551 551
             $markerLon  = $marker ['lon'];
@@ -555,18 +555,18 @@  discard block
 block discarded – undo
555 555
             $markerShadow   = '';
556 556
             $matches        = false;
557 557
             // check for marker type, get settings from markerPrototypes
558
-            if($markerType) {
559
-                foreach($this->markerPrototypes as $markerPrototype) {
560
-                    if(preg_match($markerPrototype ['regex'], $markerType, $matches)) {
561
-                        $markerFilename = $matches [0] . $markerPrototype ['extension'];
562
-                        if($markerPrototype ['offsetImage']) {
558
+            if ($markerType) {
559
+                foreach ($this->markerPrototypes as $markerPrototype) {
560
+                    if (preg_match($markerPrototype ['regex'], $markerType, $matches)) {
561
+                        $markerFilename = $matches [0].$markerPrototype ['extension'];
562
+                        if ($markerPrototype ['offsetImage']) {
563 563
                             list ($markerImageOffsetX, $markerImageOffsetY) = explode(
564 564
                                 ",",
565 565
                                 $markerPrototype ['offsetImage']
566 566
                             );
567 567
                         }
568 568
                         $markerShadow = $markerPrototype ['shadow'];
569
-                        if($markerShadow) {
569
+                        if ($markerShadow) {
570 570
                             list ($markerShadowOffsetX, $markerShadowOffsetY) = explode(
571 571
                                 ",",
572 572
                                 $markerPrototype ['offsetShadow']
@@ -576,14 +576,14 @@  discard block
 block discarded – undo
576 576
                 }
577 577
             }
578 578
             // create img resource
579
-            if(file_exists($markerBaseDir . '/' . $markerFilename)) {
580
-                $markerImg = imagecreatefrompng($markerBaseDir . '/' . $markerFilename);
579
+            if (file_exists($markerBaseDir.'/'.$markerFilename)) {
580
+                $markerImg = imagecreatefrompng($markerBaseDir.'/'.$markerFilename);
581 581
             } else {
582
-                $markerImg = imagecreatefrompng($markerBaseDir . '/marker.png');
582
+                $markerImg = imagecreatefrompng($markerBaseDir.'/marker.png');
583 583
             }
584 584
             // check for shadow + create shadow recource
585
-            if($markerShadow && file_exists($markerBaseDir . '/' . $markerShadow)) {
586
-                $markerShadowImg = imagecreatefrompng($markerBaseDir . '/' . $markerShadow);
585
+            if ($markerShadow && file_exists($markerBaseDir.'/'.$markerShadow)) {
586
+                $markerShadowImg = imagecreatefrompng($markerBaseDir.'/'.$markerShadow);
587 587
             }
588 588
             // calc position
589 589
             $destX = floor(
@@ -595,7 +595,7 @@  discard block
 block discarded – undo
595 595
                 $this->tileSize * ($this->centerY - $this->latToTile($markerLat, $this->zoom))
596 596
             );
597 597
             // copy shadow on basemap
598
-            if($markerShadow && $markerShadowImg) {
598
+            if ($markerShadow && $markerShadowImg) {
599 599
                 imagecopy(
600 600
                     $this->image,
601 601
                     $markerShadowImg,
@@ -656,14 +656,14 @@  discard block
 block discarded – undo
656 656
      *            drawing colour
657 657
      */
658 658
     private function drawGeometry(Geometry $geom, int $colour): void {
659
-        if(empty($geom)) {
659
+        if (empty($geom)) {
660 660
             return;
661 661
         }
662 662
 
663
-        switch($geom->geometryType()) {
663
+        switch ($geom->geometryType()) {
664 664
             case 'GeometryCollection' :
665 665
                 // recursively draw part of the collection
666
-                for($i = 1; $i < $geom->numGeometries() + 1; $i++) {
666
+                for ($i = 1; $i < $geom->numGeometries() + 1; $i++) {
667 667
                     $_geom = $geom->geometryN($i);
668 668
                     $this->drawGeometry($_geom, $colour);
669 669
                 }
@@ -705,7 +705,7 @@  discard block
 block discarded – undo
705 705
         // extring is a linestring actually..
706 706
         $extRing = $polygon->exteriorRing();
707 707
 
708
-        for($i = 1; $i < $extRing->numGeometries(); $i++) {
708
+        for ($i = 1; $i < $extRing->numGeometries(); $i++) {
709 709
             $p1           = $extRing->geometryN($i);
710 710
             $x            = floor(
711 711
                 ($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($p1->x(), $this->zoom))
@@ -731,7 +731,7 @@  discard block
 block discarded – undo
731 731
      */
732 732
     private function drawLineString($line, $colour) {
733 733
         imagesetthickness($this->image, 2);
734
-        for($p = 1; $p < $line->numGeometries(); $p++) {
734
+        for ($p = 1; $p < $line->numGeometries(); $p++) {
735 735
             // get first pair of points
736 736
             $p1 = $line->geometryN($p);
737 737
             $p2 = $line->geometryN($p + 1);
@@ -802,8 +802,8 @@  discard block
 block discarded – undo
802 802
      * add copyright and origin notice and icons to the map.
803 803
      */
804 804
     public function drawCopyright() {
805
-        $logoBaseDir = dirname(__FILE__) . '/' . 'logo/';
806
-        $logoImg     = imagecreatefrompng($logoBaseDir . $this->tileInfo ['openstreetmap'] ['logo']);
805
+        $logoBaseDir = dirname(__FILE__).'/'.'logo/';
806
+        $logoImg     = imagecreatefrompng($logoBaseDir.$this->tileInfo ['openstreetmap'] ['logo']);
807 807
         $textcolor   = imagecolorallocate($this->image, 0, 0, 0);
808 808
         $bgcolor     = imagecolorallocate($this->image, 200, 200, 200);
809 809
 
@@ -836,11 +836,11 @@  discard block
 block discarded – undo
836 836
 
837 837
         // additional tile source info, ie. who created/hosted the tiles
838 838
         $xIconOffset = 0;
839
-        if($this->maptype === 'openstreetmap') {
839
+        if ($this->maptype === 'openstreetmap') {
840 840
             $mapAuthor = "(c) OpenStreetMap maps/CC BY-SA";
841 841
         } else {
842 842
             $mapAuthor   = $this->tileInfo [$this->maptype] ['txt'];
843
-            $iconImg     = imagecreatefrompng($logoBaseDir . $this->tileInfo [$this->maptype] ['logo']);
843
+            $iconImg     = imagecreatefrompng($logoBaseDir.$this->tileInfo [$this->maptype] ['logo']);
844 844
             $xIconOffset = imagesx($iconImg);
845 845
             imagecopy(
846 846
                 $this->image,
Please login to merge, or discard this patch.