This commit is contained in:
Malte Hübner 2018-02-11 18:54:14 +01:00
parent 15264d0abd
commit 10eb77712c
8 changed files with 76 additions and 15 deletions

View File

@ -2,18 +2,26 @@
namespace StaticMapLite\Canvas;
class Canvas
class Canvas implements CanvasInterface
{
protected $image = null;
/** @var int $tileSize */
protected $tileSize = 256;
/** @var int $width */
protected $width = 0;
/** @var int $height */
protected $height = 0;
/** @var float $centerX */
protected $centerX = 0;
/** @var float $centerY */
protected $centerY = 0;
/** @var int $zoom */
protected $zoom = 0;
public function __construct(int $width, int $height, int $zoom, float $centerX, float $centerY)

View File

@ -0,0 +1,14 @@
<?php
namespace StaticMapLite\Canvas;
interface CanvasInterface
{
public function getImage();
public function getWidth(): int;
public function getHeight(): int;
public function getCenterX(): float;
public function getCenterY(): float;
public function getZoom(): int;
public function getTileSize(): int;
}

View File

@ -4,13 +4,14 @@ namespace StaticMapLite\CanvasTilePainter;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\TileResolver\TileResolver;
use StaticMapLite\TileResolver\TileResolverInterface;
class CanvasTilePainter
{
/** @var Canvas $canvas */
protected $canvas;
/** @var TileResolver $tileResolver */
/** @var TileResolverInterface $tileResolver */
protected $tileResolver;
public function __construct()
@ -51,11 +52,9 @@ class CanvasTilePainter
$tileData = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y);
if ($tileData) {
$tileImage = imagecreatefromstring($tileData);
$tileImage = $this->createTile($tileData);
} else {
$tileImage = imagecreate($this->canvas->getTileSize(), $this->canvas->getTileSize());
$color = imagecolorallocate($tileImage, 255, 255, 255);
@imagestring($tileImage, 1, 127, 127, 'err', $color);
$tileImage = $this->createErrorTile();
}
$destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX;
@ -67,4 +66,18 @@ class CanvasTilePainter
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,10 @@
<?php
namespace StaticMapLite\CanvasTilePainter;
interface CanvasTilePainterInterface
{
public function setCanvas(Canvas $canvas): CanvasTilePainterInterface;
public function setTileResolver(TileResolver $tileResolver): CanvasTilePainterInterface;
public function paint(): CanvasTilePainterInterface;
}

View File

@ -7,15 +7,18 @@ use StaticMapLite\CanvasTilePainter\CanvasTilePainter;
use StaticMapLite\Element\Marker\AbstractMarker;
use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter;
use StaticMapLite\ElementPrinter\Marker\MarkerPrinter;
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
use StaticMapLite\TileResolver\CachedTileResolver;
class Printer
{
/** @var int $maxWidth */
protected $maxWidth = 1024;
/** @var int $maxHeight */
protected $maxHeight = 1024;
/** @var */
protected $tileResolver = null;
/** @var Canvas $canvas */

View File

@ -4,6 +4,7 @@ namespace StaticMapLite\TileResolver;
class CachedTileResolver extends TileResolver
{
/** @var string $tileCacheBaseDir */
protected $tileCacheBaseDir = '../cache/tiles';
public function fetch(int $zoom, int $x, int $y): string
@ -25,12 +26,12 @@ class CachedTileResolver extends TileResolver
return $tile;
}
public function tileUrlToFilename(string $url): string
protected function tileUrlToFilename(string $url): string
{
return $this->tileCacheBaseDir . '/' . str_replace(['http://', 'https://'], '', $url);
}
public function checkTileCache(string $url)
protected function checkTileCache(string $url)
{
$filename = $this->tileUrlToFilename($url);
@ -41,7 +42,7 @@ class CachedTileResolver extends TileResolver
return false;
}
public function writeTileToCache($url, $data): CachedTileResolver
protected function writeTileToCache($url, $data): CachedTileResolver
{
$filename = $this->tileUrlToFilename($url);
@ -52,7 +53,7 @@ class CachedTileResolver extends TileResolver
return $this;
}
public function mkdir_recursive($pathname, $mode): bool
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

@ -4,11 +4,13 @@ namespace StaticMapLite\TileResolver;
use Curl\Curl;
class TileResolver
class TileResolver implements TileResolverInterface
{
protected $tileLayerUrl = null;
/** @var string $tileLayerUrl */
protected $tileLayerUrl;
protected $curl = null;
/** @var Curl $curl */
protected $curl;
public function __construct()
{

View File

@ -0,0 +1,10 @@
<?php
namespace StaticMapLite\TileResolver;
interface TileResolverInterface
{
public function setTileLayerUrl(string $tileLayerUrl): TileResolver;
public function resolve(int $zoom, int $x, int $y): string;
public function fetch(int $zoom, int $x, int $y): string;
}