diff --git a/src/Printer.php b/src/Printer.php index e8bb51f..b21a3b9 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -13,11 +13,12 @@ class Printer protected $tileResolver = null; protected $tileSize = 256; - protected $tileSrcUrl = array('mapnik' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png', - 'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png', - 'cycle' => 'http://a.tile.opencyclemap.org/cycle/{Z}/{X}/{Y}.png', - 'wikimedia-intl' => 'https://maps.wikimedia.org/osm-intl/{Z}/{X}/{Y}.png', - ); + protected $tileSrcUrl = [ + 'mapnik' => 'http://tile.openstreetmap.org/{z}/{x}/{y}.png', + 'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png', + 'cycle' => 'http://a.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png', + 'wikimedia-intl' => 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png', + ]; protected $tileDefaultSrc = 'mapnik'; protected $markerBaseDir = '../images/markers'; @@ -80,6 +81,7 @@ class Printer $this->maptype = $this->tileDefaultSrc; $this->tileResolver = new CachedTileResolver(); + $this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]); } public function addMarker(Marker $marker): Printer @@ -137,6 +139,8 @@ class Printer { $this->maptype = $mapType; + $this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]); + return $this; } @@ -165,8 +169,8 @@ class Printer for ($x = $startX; $x <= $endX; $x++) { for ($y = $startY; $y <= $endY; $y++) { - $url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]); - $tileData = $this->fetchTile($url); + $tileData = $this->tileResolver->fetch($this->zoom, $x, $y); + if ($tileData) { $tileImage = imagecreatefromstring($tileData); } else { @@ -309,11 +313,6 @@ class Printer return $this->mapCacheFile . "." . $this->mapCacheExtension; } - public function fetchTile($url) - { - return $this->tileResolver->fetch($url); - } - public function copyrightNotice() { $logoImg = imagecreatefrompng($this->osmLogo); diff --git a/src/TileResolver/CachedTileResolver.php b/src/TileResolver/CachedTileResolver.php index f4bec39..efe2bcd 100644 --- a/src/TileResolver/CachedTileResolver.php +++ b/src/TileResolver/CachedTileResolver.php @@ -6,15 +6,17 @@ class CachedTileResolver extends TileResolver { protected $tileCacheBaseDir = '../cache/tiles'; - public function fetch(string $url): string + public function fetch(int $zoom, int $x, int $y): string { + $url = $this->resolve($zoom, $x, $y); + $cachedTile = $this->checkTileCache($url); if ($cachedTile) { return $cachedTile; } - $tile = parent::fetch($url); + $tile = parent::fetch($zoom, $x, $y); if ($tile) { $this->writeTileToCache($url, $tile); diff --git a/src/TileResolver/TileResolver.php b/src/TileResolver/TileResolver.php index 48ac962..b98eb29 100644 --- a/src/TileResolver/TileResolver.php +++ b/src/TileResolver/TileResolver.php @@ -6,7 +6,7 @@ use Curl\Curl; class TileResolver { - protected $tileLayer = null; + protected $tileLayerUrl = null; protected $curl = null; @@ -15,8 +15,22 @@ class TileResolver $this->curl = new Curl(); } - public function fetch(string $url): string + public function setTileLayerUrl(string $tileLayerUrl): TileResolver { + $this->tileLayerUrl = $tileLayerUrl; + + return $this; + } + + public function resolve(int $zoom, int $x, int $y): string + { + return str_replace(['{z}', '{x}', '{y}'], [$zoom, $x, $y], $this->tileLayerUrl); + } + + public function fetch(int $zoom, int $x, int $y): string + { + $url = $this->resolve($zoom, $x, $y); + $this->curl->get($url); return $this->curl->response;