Much Tile wip.
This commit is contained in:
parent
05fffecdd6
commit
197108e74a
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace StaticMapLite\CanvasTilePainter;
|
namespace StaticMapLite\CanvasTilePainter;
|
||||||
|
|
||||||
|
use Imagine\Image\Point;
|
||||||
use StaticMapLite\Canvas\Canvas;
|
use StaticMapLite\Canvas\Canvas;
|
||||||
use StaticMapLite\TileResolver\TileResolver;
|
|
||||||
use StaticMapLite\TileResolver\TileResolverInterface;
|
use StaticMapLite\TileResolver\TileResolverInterface;
|
||||||
|
|
||||||
class CanvasTilePainter
|
class CanvasTilePainter
|
||||||
@ -26,7 +26,7 @@ class CanvasTilePainter
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTileResolver(TileResolver $tileResolver): CanvasTilePainter
|
public function setTileResolver(TileResolverInterface $tileResolver): CanvasTilePainter
|
||||||
{
|
{
|
||||||
$this->tileResolver = $tileResolver;
|
$this->tileResolver = $tileResolver;
|
||||||
|
|
||||||
@ -49,35 +49,16 @@ class CanvasTilePainter
|
|||||||
|
|
||||||
for ($x = $startX; $x <= $endX; $x++) {
|
for ($x = $startX; $x <= $endX; $x++) {
|
||||||
for ($y = $startY; $y <= $endY; $y++) {
|
for ($y = $startY; $y <= $endY; $y++) {
|
||||||
$tileData = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y);
|
$tileImage = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y);
|
||||||
|
|
||||||
if ($tileData) {
|
|
||||||
$tileImage = $this->createTile($tileData);
|
|
||||||
} else {
|
|
||||||
$tileImage = $this->createErrorTile();
|
|
||||||
}
|
|
||||||
|
|
||||||
$destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX;
|
$destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX;
|
||||||
$destY = ($y - $startY) * $this->canvas->getTileSize() + $offsetY;
|
$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;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
32
src/TileResolver/AbstractTileResolver.php
Normal file
32
src/TileResolver/AbstractTileResolver.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace StaticMapLite\TileResolver;
|
namespace StaticMapLite\TileResolver;
|
||||||
|
|
||||||
|
use Imagine\Image\ImageInterface;
|
||||||
|
|
||||||
class CachedTileResolver extends TileResolver
|
class CachedTileResolver extends TileResolver
|
||||||
{
|
{
|
||||||
/** @var string $tileCacheBaseDir */
|
/** @var string $tileCacheBaseDir */
|
||||||
protected $tileCacheBaseDir = '../cache/tiles';
|
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);
|
$url = $this->resolve($zoom, $x, $y);
|
||||||
|
|
||||||
@ -31,24 +33,26 @@ class CachedTileResolver extends TileResolver
|
|||||||
return $this->tileCacheBaseDir . '/' . str_replace(['http://', 'https://'], '', $url);
|
return $this->tileCacheBaseDir . '/' . str_replace(['http://', 'https://'], '', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkTileCache(string $url)
|
protected function checkTileCache(string $url): ?ImageInterface
|
||||||
{
|
{
|
||||||
$filename = $this->tileUrlToFilename($url);
|
$filename = $this->tileUrlToFilename($url);
|
||||||
|
|
||||||
if (file_exists($filename)) {
|
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);
|
$filename = $this->tileUrlToFilename($url);
|
||||||
|
|
||||||
$this->mkdir_recursive(dirname($filename), 0777);
|
$this->mkdir_recursive(dirname($filename), 0777);
|
||||||
|
|
||||||
file_put_contents($filename, $data);
|
$tileImage->save($filename);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -56,6 +60,7 @@ class CachedTileResolver extends TileResolver
|
|||||||
protected function mkdir_recursive($pathname, $mode): bool
|
protected function mkdir_recursive($pathname, $mode): bool
|
||||||
{
|
{
|
||||||
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
|
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
|
||||||
|
|
||||||
return is_dir($pathname) || @mkdir($pathname, $mode);
|
return is_dir($pathname) || @mkdir($pathname, $mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,39 +2,26 @@
|
|||||||
|
|
||||||
namespace StaticMapLite\TileResolver;
|
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
|
public function resolve(int $zoom, int $x, int $y): string
|
||||||
{
|
{
|
||||||
return str_replace(['{z}', '{x}', '{y}'], [$zoom, $x, $y], $this->tileLayerUrl);
|
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);
|
$url = $this->resolve($zoom, $x, $y);
|
||||||
|
|
||||||
$this->curl->get($url);
|
$this->curl->get($url);
|
||||||
|
|
||||||
return $this->curl->response;
|
$image = $this
|
||||||
|
->imagine
|
||||||
|
->load($this->curl->response)
|
||||||
|
;
|
||||||
|
|
||||||
|
return $image;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
namespace StaticMapLite\TileResolver;
|
namespace StaticMapLite\TileResolver;
|
||||||
|
|
||||||
|
use Imagine\Image\ImageInterface;
|
||||||
|
|
||||||
interface TileResolverInterface
|
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 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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user