Completed
Push — master ( b8fb7e...15aea0 )
by Mark
18s queued 14s
created
StaticMap.php 1 patch
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -28,7 +28,7 @@  discard block
 block discarded – undo
28 28
 
29 29
 // phpcs:disable PSR1.Files.SideEffects
30 30
 // TODO resolve side effect
31
-require_once __DIR__ . '/../geophp/vendor/autoload.php';
31
+require_once __DIR__.'/../geophp/vendor/autoload.php';
32 32
 
33 33
 /**
34 34
  *
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
         $this->height = $height;
213 213
         // validate + set maptype
214 214
         $this->maptype = $this->tileDefaultSrc;
215
-        if(array_key_exists($maptype, $this->tileInfo)) {
215
+        if (array_key_exists($maptype, $this->tileInfo)) {
216 216
             $this->maptype = $maptype;
217 217
         }
218 218
         $this->markers          = $markers;
@@ -220,9 +220,9 @@  discard block
 block discarded – undo
220 220
         $this->gpxFileName      = $gpx;
221 221
         $this->geojsonFileName  = $geojson;
222 222
         $this->mediaBaseDir     = $mediaDir;
223
-        $this->tileCacheBaseDir = $tileCacheBaseDir . '/olmaptiles';
223
+        $this->tileCacheBaseDir = $tileCacheBaseDir.'/olmaptiles';
224 224
         $this->useTileCache     = $this->tileCacheBaseDir !== '';
225
-        $this->mapCacheBaseDir  = $mediaDir . '/olmapmaps';
225
+        $this->mapCacheBaseDir  = $mediaDir.'/olmapmaps';
226 226
         $this->autoZoomExtent   = $autoZoomExtent;
227 227
         $this->apikey           = $apikey;
228 228
     }
@@ -234,15 +234,15 @@  discard block
 block discarded – undo
234 234
      */
235 235
     public function getMap(): string {
236 236
         try {
237
-            if($this->autoZoomExtent) {
237
+            if ($this->autoZoomExtent) {
238 238
                 $this->autoZoom();
239 239
             }
240
-        } catch(Exception $e) {
240
+        } catch (Exception $e) {
241 241
             dbglog($e);
242 242
         }
243 243
 
244 244
         // use map cache, so check cache for map
245
-        if(!$this->checkMapCache()) {
245
+        if (!$this->checkMapCache()) {
246 246
             // map is not in cache, needs to be build
247 247
             $this->makeMap();
248 248
             $this->mkdirRecursive(dirname($this->mapCacheIDToFilename()), 0777);
@@ -262,37 +262,37 @@  discard block
 block discarded – undo
262 262
      */
263 263
     private function autoZoom(float $paddingFactor = 1.0): void {
264 264
         $geoms    = array();
265
-        $geoms [] = new Point ($this->lon, $this->lat);
266
-        if(!empty ($this->markers)) {
267
-            foreach($this->markers as $marker) {
268
-                $geoms [] = new Point ($marker ['lon'], $marker ['lat']);
265
+        $geoms [] = new Point($this->lon, $this->lat);
266
+        if (!empty ($this->markers)) {
267
+            foreach ($this->markers as $marker) {
268
+                $geoms [] = new Point($marker ['lon'], $marker ['lat']);
269 269
             }
270 270
         }
271
-        if(file_exists($this->kmlFileName)) {
271
+        if (file_exists($this->kmlFileName)) {
272 272
             $g = geoPHP::load(file_get_contents($this->kmlFileName), 'kml');
273
-            if($g !== false) {
273
+            if ($g !== false) {
274 274
                 $geoms [] = $g;
275 275
             }
276 276
         }
277
-        if(file_exists($this->gpxFileName)) {
277
+        if (file_exists($this->gpxFileName)) {
278 278
             $g = geoPHP::load(file_get_contents($this->gpxFileName), 'gpx');
279
-            if($g !== false) {
279
+            if ($g !== false) {
280 280
                 $geoms [] = $g;
281 281
             }
282 282
         }
283
-        if(file_exists($this->geojsonFileName)) {
283
+        if (file_exists($this->geojsonFileName)) {
284 284
             $g = geoPHP::load(file_get_contents($this->geojsonFileName), 'geojson');
285
-            if($g !== false) {
285
+            if ($g !== false) {
286 286
                 $geoms [] = $g;
287 287
             }
288 288
         }
289 289
 
290
-        if(count($geoms) <= 1) {
290
+        if (count($geoms) <= 1) {
291 291
             dbglog($geoms, "StaticMap::autoZoom: Skip setting autozoom options");
292 292
             return;
293 293
         }
294 294
 
295
-        $geom     = new GeometryCollection ($geoms);
295
+        $geom     = new GeometryCollection($geoms);
296 296
         $centroid = $geom->centroid();
297 297
         $bbox     = $geom->getBBox();
298 298
 
@@ -301,7 +301,7 @@  discard block
 block discarded – undo
301 301
         $vy0 = log(tan(M_PI * (0.25 + $bbox ['miny'] / 360)));
302 302
         $vy1 = log(tan(M_PI * (0.25 + $bbox ['maxy'] / 360)));
303 303
         dbglog("StaticMap::autoZoom: vertical resolution: $vy0, $vy1");
304
-        if ($vy1 - $vy0 === 0.0){
304
+        if ($vy1 - $vy0 === 0.0) {
305 305
             $resolutionVertical = 0;
306 306
             dbglog("StaticMap::autoZoom: using $resolutionVertical");
307 307
         } else {
@@ -313,11 +313,11 @@  discard block
 block discarded – undo
313 313
         dbglog("StaticMap::autoZoom: using $resolutionHorizontal");
314 314
         $resolution           = max($resolutionHorizontal, $resolutionVertical) * $paddingFactor;
315 315
         $zoom                 = $this->zoom;
316
-        if ($resolution > 0){
316
+        if ($resolution > 0) {
317 317
             $zoom             = log(360 / ($resolution * $this->tileSize), 2);
318 318
         }
319 319
 
320
-        if(is_finite($zoom) && $zoom < 15 && $zoom > 2) {
320
+        if (is_finite($zoom) && $zoom < 15 && $zoom > 2) {
321 321
             $this->zoom = floor($zoom);
322 322
         }
323 323
         $this->lon = $centroid->getX();
@@ -350,12 +350,12 @@  discard block
 block discarded – undo
350 350
     }
351 351
 
352 352
     public function mapCacheIDToFilename(): string {
353
-        if(!$this->mapCacheFile) {
354
-            $this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_"
355
-                . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2)
356
-                . "/" . substr($this->mapCacheID, 4);
353
+        if (!$this->mapCacheFile) {
354
+            $this->mapCacheFile = $this->mapCacheBaseDir."/".$this->maptype."/".$this->zoom."/cache_"
355
+                . substr($this->mapCacheID, 0, 2)."/".substr($this->mapCacheID, 2, 2)
356
+                . "/".substr($this->mapCacheID, 4);
357 357
         }
358
-        return $this->mapCacheFile . "." . $this->mapCacheExtension;
358
+        return $this->mapCacheFile.".".$this->mapCacheExtension;
359 359
     }
360 360
 
361 361
     /**
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
     public function makeMap(): void {
365 365
         $this->initCoords();
366 366
         $this->createBaseMap();
367
-        if(!empty ($this->markers)) {
367
+        if (!empty ($this->markers)) {
368 368
             $this->placeMarkers();
369 369
         }
370 370
         if (file_exists($this->kmlFileName)) {
@@ -437,8 +437,8 @@  discard block
 block discarded – undo
437 437
         $this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
438 438
         $this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;
439 439
 
440
-        for($x = $startX; $x <= $endX; $x++) {
441
-            for($y = $startY; $y <= $endY; $y++) {
440
+        for ($x = $startX; $x <= $endX; $x++) {
441
+            for ($y = $startY; $y <= $endY; $y++) {
442 442
                 $url = str_replace(
443 443
                     array(
444 444
                         '{Z}',
@@ -452,7 +452,7 @@  discard block
 block discarded – undo
452 452
                 );
453 453
 
454 454
                 $tileData = $this->fetchTile($url);
455
-                if($tileData) {
455
+                if ($tileData) {
456 456
                     $tileImage = imagecreatefromstring($tileData);
457 457
                 } else {
458 458
                     $tileImage = imagecreate($this->tileSize, $this->tileSize);
@@ -478,17 +478,17 @@  discard block
 block discarded – undo
478 478
      *          for better proxy handling...
479 479
      */
480 480
     public function fetchTile(string $url) {
481
-        if($this->useTileCache && ($cached = $this->checkTileCache($url)))
481
+        if ($this->useTileCache && ($cached = $this->checkTileCache($url)))
482 482
             return $cached;
483 483
 
484
-        $_UA = 'Mozilla/4.0 (compatible; DokuWikiSpatial HTTP Client; ' . PHP_OS . ')';
485
-        if(function_exists("curl_init")) {
484
+        $_UA = 'Mozilla/4.0 (compatible; DokuWikiSpatial HTTP Client; '.PHP_OS.')';
485
+        if (function_exists("curl_init")) {
486 486
             // use cUrl
487 487
             $ch = curl_init();
488 488
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
489 489
             curl_setopt($ch, CURLOPT_USERAGENT, $_UA);
490 490
             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
491
-            curl_setopt($ch, CURLOPT_URL, $url . $this->apikey);
491
+            curl_setopt($ch, CURLOPT_URL, $url.$this->apikey);
492 492
             dbglog("StaticMap::fetchTile: getting: $url using curl_exec");
493 493
             $tile = curl_exec($ch);
494 494
             curl_close($ch);
@@ -498,21 +498,21 @@  discard block
 block discarded – undo
498 498
             $opts = array(
499 499
                 'http' => array(
500 500
                     'method'          => "GET",
501
-                    'header'          => "Accept-language: en\r\n" . "User-Agent: $_UA\r\n" . "accept: image/png\r\n",
501
+                    'header'          => "Accept-language: en\r\n"."User-Agent: $_UA\r\n"."accept: image/png\r\n",
502 502
                     'request_fulluri' => true
503 503
                 )
504 504
             );
505
-            if(isset($conf['proxy']['host'], $conf['proxy']['port'])
505
+            if (isset($conf['proxy']['host'], $conf['proxy']['port'])
506 506
                 && $conf['proxy']['host'] !== ''
507 507
                 && $conf['proxy']['port'] !== '') {
508
-                $opts['http'] += ['proxy' => "tcp://" . $conf['proxy']['host'] . ":" . $conf['proxy']['port']];
508
+                $opts['http'] += ['proxy' => "tcp://".$conf['proxy']['host'].":".$conf['proxy']['port']];
509 509
             }
510 510
 
511 511
             $context = stream_context_create($opts);
512 512
             dbglog("StaticMap::fetchTile: getting: $url . $this->apikey using file_get_contents and options $opts");
513
-            $tile = file_get_contents($url . $this->apikey, false, $context);
513
+            $tile = file_get_contents($url.$this->apikey, false, $context);
514 514
         }
515
-        if($tile && $this->useTileCache) {
515
+        if ($tile && $this->useTileCache) {
516 516
             $this->writeTileToCache($url, $tile);
517 517
         }
518 518
         return $tile;
@@ -525,7 +525,7 @@  discard block
 block discarded – undo
525 525
      */
526 526
     public function checkTileCache(string $url) {
527 527
         $filename = $this->tileUrlToFilename($url);
528
-        if(file_exists($filename)) {
528
+        if (file_exists($filename)) {
529 529
             return file_get_contents($filename);
530 530
         }
531 531
         return false;
@@ -537,7 +537,7 @@  discard block
 block discarded – undo
537 537
      * @return string
538 538
      */
539 539
     public function tileUrlToFilename(string $url): string {
540
-        return $this->tileCacheBaseDir . "/" . substr($url, strpos($url, '/') + 1);
540
+        return $this->tileCacheBaseDir."/".substr($url, strpos($url, '/') + 1);
541 541
     }
542 542
 
543 543
     /**
@@ -572,14 +572,14 @@  discard block
 block discarded – undo
572 572
         $count         = 0;
573 573
         $color         = imagecolorallocate($this->image, 0, 0, 0);
574 574
         $bgcolor       = imagecolorallocate($this->image, 200, 200, 200);
575
-        $markerBaseDir = __DIR__ . '/icons';
575
+        $markerBaseDir = __DIR__.'/icons';
576 576
         $markerImageOffsetX  = 0;
577 577
         $markerImageOffsetY  = 0;
578 578
         $markerShadowOffsetX = 0;
579 579
         $markerShadowOffsetY = 0;
580 580
         $markerShadowImg     = null;
581 581
         // loop thru marker array
582
-        foreach($this->markers as $marker) {
582
+        foreach ($this->markers as $marker) {
583 583
             // set some local variables
584 584
             $markerLat  = $marker ['lat'];
585 585
             $markerLon  = $marker ['lon'];
@@ -589,18 +589,18 @@  discard block
 block discarded – undo
589 589
             $markerShadow   = '';
590 590
             $matches        = false;
591 591
             // check for marker type, get settings from markerPrototypes
592
-            if($markerType) {
593
-                foreach($this->markerPrototypes as $markerPrototype) {
594
-                    if(preg_match($markerPrototype ['regex'], $markerType, $matches)) {
595
-                        $markerFilename = $matches [0] . $markerPrototype ['extension'];
596
-                        if($markerPrototype ['offsetImage']) {
592
+            if ($markerType) {
593
+                foreach ($this->markerPrototypes as $markerPrototype) {
594
+                    if (preg_match($markerPrototype ['regex'], $markerType, $matches)) {
595
+                        $markerFilename = $matches [0].$markerPrototype ['extension'];
596
+                        if ($markerPrototype ['offsetImage']) {
597 597
                             list ($markerImageOffsetX, $markerImageOffsetY) = explode(
598 598
                                 ",",
599 599
                                 $markerPrototype ['offsetImage']
600 600
                             );
601 601
                         }
602 602
                         $markerShadow = $markerPrototype ['shadow'];
603
-                        if($markerShadow) {
603
+                        if ($markerShadow) {
604 604
                             list ($markerShadowOffsetX, $markerShadowOffsetY) = explode(
605 605
                                 ",",
606 606
                                 $markerPrototype ['offsetShadow']
@@ -610,14 +610,14 @@  discard block
 block discarded – undo
610 610
                 }
611 611
             }
612 612
             // create img resource
613
-            if(file_exists($markerBaseDir . '/' . $markerFilename)) {
614
-                $markerImg = imagecreatefrompng($markerBaseDir . '/' . $markerFilename);
613
+            if (file_exists($markerBaseDir.'/'.$markerFilename)) {
614
+                $markerImg = imagecreatefrompng($markerBaseDir.'/'.$markerFilename);
615 615
             } else {
616
-                $markerImg = imagecreatefrompng($markerBaseDir . '/marker.png');
616
+                $markerImg = imagecreatefrompng($markerBaseDir.'/marker.png');
617 617
             }
618 618
             // check for shadow + create shadow recource
619
-            if($markerShadow && file_exists($markerBaseDir . '/' . $markerShadow)) {
620
-                $markerShadowImg = imagecreatefrompng($markerBaseDir . '/' . $markerShadow);
619
+            if ($markerShadow && file_exists($markerBaseDir.'/'.$markerShadow)) {
620
+                $markerShadowImg = imagecreatefrompng($markerBaseDir.'/'.$markerShadow);
621 621
             }
622 622
             // calc position
623 623
             $destX = floor(
@@ -629,7 +629,7 @@  discard block
 block discarded – undo
629 629
                 $this->tileSize * ($this->centerY - $this->latToTile($markerLat, $this->zoom))
630 630
             );
631 631
             // copy shadow on basemap
632
-            if($markerShadow && $markerShadowImg) {
632
+            if ($markerShadow && $markerShadowImg) {
633 633
                 imagecopy(
634 634
                     $this->image,
635 635
                     $markerShadowImg,
@@ -691,14 +691,14 @@  discard block
 block discarded – undo
691 691
      *            drawing colour
692 692
      */
693 693
     private function drawGeometry(Geometry $geom, int $colour): void {
694
-        if(empty($geom)) {
694
+        if (empty($geom)) {
695 695
             return;
696 696
         }
697 697
 
698
-        switch($geom->geometryType()) {
698
+        switch ($geom->geometryType()) {
699 699
             case 'GeometryCollection' :
700 700
                 // recursively draw part of the collection
701
-                for($i = 1; $i < $geom->numGeometries() + 1; $i++) {
701
+                for ($i = 1; $i < $geom->numGeometries() + 1; $i++) {
702 702
                     $_geom = $geom->geometryN($i);
703 703
                     $this->drawGeometry($_geom, $colour);
704 704
                 }
@@ -740,7 +740,7 @@  discard block
 block discarded – undo
740 740
         // extring is a linestring actually..
741 741
         $extRing = $polygon->exteriorRing();
742 742
 
743
-        for($i = 1; $i < $extRing->numGeometries(); $i++) {
743
+        for ($i = 1; $i < $extRing->numGeometries(); $i++) {
744 744
             $p1           = $extRing->geometryN($i);
745 745
             $x            = floor(
746 746
                 ($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($p1->x(), $this->zoom))
@@ -766,7 +766,7 @@  discard block
 block discarded – undo
766 766
      */
767 767
     private function drawLineString($line, $colour) {
768 768
         imagesetthickness($this->image, 2);
769
-        for($p = 1; $p < $line->numGeometries(); $p++) {
769
+        for ($p = 1; $p < $line->numGeometries(); $p++) {
770 770
             // get first pair of points
771 771
             $p1 = $line->geometryN($p);
772 772
             $p2 = $line->geometryN($p + 1);
@@ -839,8 +839,8 @@  discard block
 block discarded – undo
839 839
      * add copyright and origin notice and icons to the map.
840 840
      */
841 841
     public function drawCopyright() {
842
-        $logoBaseDir = dirname(__FILE__) . '/' . 'logo/';
843
-        $logoImg     = imagecreatefrompng($logoBaseDir . $this->tileInfo ['openstreetmap'] ['logo']);
842
+        $logoBaseDir = dirname(__FILE__).'/'.'logo/';
843
+        $logoImg     = imagecreatefrompng($logoBaseDir.$this->tileInfo ['openstreetmap'] ['logo']);
844 844
         $textcolor   = imagecolorallocate($this->image, 0, 0, 0);
845 845
         $bgcolor     = imagecolorallocate($this->image, 200, 200, 200);
846 846
 
@@ -873,11 +873,11 @@  discard block
 block discarded – undo
873 873
 
874 874
         // additional tile source info, ie. who created/hosted the tiles
875 875
         $xIconOffset = 0;
876
-        if($this->maptype === 'openstreetmap') {
876
+        if ($this->maptype === 'openstreetmap') {
877 877
             $mapAuthor = "(c) OpenStreetMap maps/CC BY-SA";
878 878
         } else {
879 879
             $mapAuthor   = $this->tileInfo [$this->maptype] ['txt'];
880
-            $iconImg     = imagecreatefrompng($logoBaseDir . $this->tileInfo [$this->maptype] ['logo']);
880
+            $iconImg     = imagecreatefrompng($logoBaseDir.$this->tileInfo [$this->maptype] ['logo']);
881 881
             $xIconOffset = imagesx($iconImg);
882 882
             imagecopy(
883 883
                 $this->image,
Please login to merge, or discard this patch.