diff --git a/src/CanvasTilePainter/CanvasTilePainter.php b/src/CanvasTilePainter/CanvasTilePainter.php index 6460a1f..5b26e87 100644 --- a/src/CanvasTilePainter/CanvasTilePainter.php +++ b/src/CanvasTilePainter/CanvasTilePainter.php @@ -2,8 +2,8 @@ namespace StaticMapLite\CanvasTilePainter; +use Imagine\Image\Point; use StaticMapLite\Canvas\Canvas; -use StaticMapLite\TileResolver\TileResolver; use StaticMapLite\TileResolver\TileResolverInterface; class CanvasTilePainter @@ -26,7 +26,7 @@ class CanvasTilePainter return $this; } - public function setTileResolver(TileResolver $tileResolver): CanvasTilePainter + public function setTileResolver(TileResolverInterface $tileResolver): CanvasTilePainter { $this->tileResolver = $tileResolver; @@ -49,35 +49,16 @@ class CanvasTilePainter for ($x = $startX; $x <= $endX; $x++) { for ($y = $startY; $y <= $endY; $y++) { - $tileData = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y); - - if ($tileData) { - $tileImage = $this->createTile($tileData); - } else { - $tileImage = $this->createErrorTile(); - } + $tileImage = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y); $destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX; $destY = ($y - $startY) * $this->canvas->getTileSize() + $offsetY; - imagecopy($this->canvas->getImage(), $tileImage, $destX, $destY, 0, 0, $this->canvas->getTileSize(), $this->canvas->getTileSize()); + $point = new Point($destX, $destY); + $tileImage->paste($this->canvas->getImage(), $point); } } return $this; } - - protected function createTile(string $tileData) - { - return imagecreatefromstring($tileData); - } - - protected function createErrorTile() - { - $tileImage = imagecreate($this->canvas->getTileSize(), $this->canvas->getTileSize()); - $color = imagecolorallocate($tileImage, 255, 255, 255); - @imagestring($tileImage, 1, 127, 127, 'err', $color); - - return $tileImage; - } } diff --git a/src/TileResolver/AbstractTileResolver.php b/src/TileResolver/AbstractTileResolver.php new file mode 100644 index 0000000..c48c691 --- /dev/null +++ b/src/TileResolver/AbstractTileResolver.php @@ -0,0 +1,32 @@ +curl = new Curl(); + $this->imagine = new Imagine(); + } + + public function setTileLayerUrl(string $tileLayerUrl): TileResolverInterface + { + $this->tileLayerUrl = $tileLayerUrl; + + return $this; + } +} diff --git a/src/TileResolver/CachedTileResolver.php b/src/TileResolver/CachedTileResolver.php index bdbc3bd..b2a1226 100644 --- a/src/TileResolver/CachedTileResolver.php +++ b/src/TileResolver/CachedTileResolver.php @@ -2,12 +2,14 @@ namespace StaticMapLite\TileResolver; +use Imagine\Image\ImageInterface; + class CachedTileResolver extends TileResolver { /** @var string $tileCacheBaseDir */ protected $tileCacheBaseDir = '../cache/tiles'; - public function fetch(int $zoom, int $x, int $y): string + public function fetch(int $zoom, int $x, int $y): ImageInterface { $url = $this->resolve($zoom, $x, $y); @@ -31,24 +33,26 @@ class CachedTileResolver extends TileResolver return $this->tileCacheBaseDir . '/' . str_replace(['http://', 'https://'], '', $url); } - protected function checkTileCache(string $url) + protected function checkTileCache(string $url): ?ImageInterface { $filename = $this->tileUrlToFilename($url); if (file_exists($filename)) { - return file_get_contents($filename); + $tileImagine = $this->imagine->open($filename); + + return $tileImagine; } - return false; + return null; } - protected function writeTileToCache($url, $data): CachedTileResolver + protected function writeTileToCache($url, ImageInterface $tileImage): CachedTileResolver { $filename = $this->tileUrlToFilename($url); $this->mkdir_recursive(dirname($filename), 0777); - file_put_contents($filename, $data); + $tileImage->save($filename); return $this; } @@ -56,6 +60,7 @@ class CachedTileResolver extends TileResolver protected function mkdir_recursive($pathname, $mode): bool { is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); + return is_dir($pathname) || @mkdir($pathname, $mode); } } diff --git a/src/TileResolver/TileResolver.php b/src/TileResolver/TileResolver.php index 4944439..27da539 100644 --- a/src/TileResolver/TileResolver.php +++ b/src/TileResolver/TileResolver.php @@ -2,39 +2,26 @@ namespace StaticMapLite\TileResolver; -use Curl\Curl; +use Imagine\Image\ImageInterface; -class TileResolver implements TileResolverInterface +class TileResolver extends AbstractTileResolver { - /** @var string $tileLayerUrl */ - protected $tileLayerUrl; - - /** @var Curl $curl */ - protected $curl; - - public function __construct() - { - $this->curl = new Curl(); - } - - 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 + public function fetch(int $zoom, int $x, int $y): ImageInterface { $url = $this->resolve($zoom, $x, $y); $this->curl->get($url); - return $this->curl->response; + $image = $this + ->imagine + ->load($this->curl->response) + ; + + return $image; } } diff --git a/src/TileResolver/TileResolverInterface.php b/src/TileResolver/TileResolverInterface.php index c4a45b6..22eb00f 100644 --- a/src/TileResolver/TileResolverInterface.php +++ b/src/TileResolver/TileResolverInterface.php @@ -2,9 +2,11 @@ namespace StaticMapLite\TileResolver; +use Imagine\Image\ImageInterface; + interface TileResolverInterface { - public function setTileLayerUrl(string $tileLayerUrl): TileResolver; + public function setTileLayerUrl(string $tileLayerUrl): TileResolverInterface; public function resolve(int $zoom, int $x, int $y): string; - public function fetch(int $zoom, int $x, int $y): string; + public function fetch(int $zoom, int $x, int $y): ImageInterface; }