Much Tile wip.

This commit is contained in:
Malte Hübner 2018-02-14 20:15:01 +01:00
parent 05fffecdd6
commit 197108e74a
5 changed files with 61 additions and 54 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace StaticMapLite\TileResolver;
use Curl\Curl;
use Imagine\Gd\Imagine;
use Imagine\Image\ImagineInterface;
abstract class AbstractTileResolver implements TileResolverInterface
{
/** @var string $tileLayerUrl */
protected $tileLayerUrl;
/** @var Curl $curl */
protected $curl;
/** @var ImagineInterface $imagine */
protected $imagine;
public function __construct()
{
$this->curl = new Curl();
$this->imagine = new Imagine();
}
public function setTileLayerUrl(string $tileLayerUrl): TileResolverInterface
{
$this->tileLayerUrl = $tileLayerUrl;
return $this;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}