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; namespace StaticMapLite\Canvas;
class Canvas class Canvas implements CanvasInterface
{ {
protected $image = null; protected $image = null;
/** @var int $tileSize */
protected $tileSize = 256; protected $tileSize = 256;
/** @var int $width */
protected $width = 0; protected $width = 0;
/** @var int $height */
protected $height = 0; protected $height = 0;
/** @var float $centerX */
protected $centerX = 0; protected $centerX = 0;
/** @var float $centerY */
protected $centerY = 0; protected $centerY = 0;
/** @var int $zoom */
protected $zoom = 0; protected $zoom = 0;
public function __construct(int $width, int $height, int $zoom, float $centerX, float $centerY) 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\Canvas\Canvas;
use StaticMapLite\TileResolver\TileResolver; use StaticMapLite\TileResolver\TileResolver;
use StaticMapLite\TileResolver\TileResolverInterface;
class CanvasTilePainter class CanvasTilePainter
{ {
/** @var Canvas $canvas */ /** @var Canvas $canvas */
protected $canvas; protected $canvas;
/** @var TileResolver $tileResolver */ /** @var TileResolverInterface $tileResolver */
protected $tileResolver; protected $tileResolver;
public function __construct() public function __construct()
@ -51,11 +52,9 @@ class CanvasTilePainter
$tileData = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y); $tileData = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y);
if ($tileData) { if ($tileData) {
$tileImage = imagecreatefromstring($tileData); $tileImage = $this->createTile($tileData);
} else { } else {
$tileImage = imagecreate($this->canvas->getTileSize(), $this->canvas->getTileSize()); $tileImage = $this->createErrorTile();
$color = imagecolorallocate($tileImage, 255, 255, 255);
@imagestring($tileImage, 1, 127, 127, 'err', $color);
} }
$destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX; $destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX;
@ -67,4 +66,18 @@ class CanvasTilePainter
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;
}
}

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\Marker\AbstractMarker;
use StaticMapLite\Element\Polyline\Polyline; use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter; use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter;
use StaticMapLite\ElementPrinter\Marker\MarkerPrinter;
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter; use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
use StaticMapLite\TileResolver\CachedTileResolver; use StaticMapLite\TileResolver\CachedTileResolver;
class Printer class Printer
{ {
/** @var int $maxWidth */
protected $maxWidth = 1024; protected $maxWidth = 1024;
/** @var int $maxHeight */
protected $maxHeight = 1024; protected $maxHeight = 1024;
/** @var */
protected $tileResolver = null; protected $tileResolver = null;
/** @var Canvas $canvas */ /** @var Canvas $canvas */

View File

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

View File

@ -4,11 +4,13 @@ namespace StaticMapLite\TileResolver;
use Curl\Curl; 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() 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;
}