Skip to content

Commit

Permalink
updates for intervention/image:^3
Browse files Browse the repository at this point in the history
This includes some backward incompatible changes, see changes in example files. TileProvider does now require a ClientInterface parameter to fetch tiles and font callbacks require a new typehint for FontFactory.
  • Loading branch information
laufhannes committed Nov 16, 2024
1 parent a63ab09 commit 7fc722a
Show file tree
Hide file tree
Showing 47 changed files with 284 additions and 401 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.lock
docs/examples/cache/
resources/font/
vendor/
vendor/
.phpunit.result.cache
10 changes: 0 additions & 10 deletions .scrutinizer.yml

This file was deleted.

24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

Library to create static images from various map tile providers.
Expand All @@ -26,21 +23,21 @@ $ composer require runalyze/static-maps
For a full list of required use statements, see [example-1.php](docs/examples/example-1.php):

``` php
$imageManager = new ImageManager(['driver' => 'gd']);
$tileService = new OpenStreetMap();
$tileCache = new FilesystemCache(new Filesystem(new Local(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, $tileCache);
$imageManager = ImageManager::gd();
$tileService = new OpenStreetMapDe();
$tileCache = new FilesystemCache(new Filesystem(new LocalFilesystemAdapter(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, new \GuzzleHttp\Client(), $tileCache);

$map = new Map(new Viewport(500, 350, new BoundingBox(53.40, 53.75, 9.90, 10.10), $tileService));
$map->addFeature(new TileMap($tileProvider));
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function($font){
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\Typography\FontFactory $font) {
$font->file('./resources/font/Roboto-Regular.ttf');
}));

$provider = new Renderer($imageManager);
$image = $provider->renderMap($map);

echo $image->response('png');
file_put_contents('example-1.png', $image->toPng());
```

![Example for static map][link-example-1]
Expand All @@ -65,15 +62,9 @@ The MIT License (MIT). Please see [License File](LICENSE) for more information.

[ico-version]: https://img.shields.io/packagist/v/runalyze/static-maps.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/runalyze/static-maps/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/runalyze/static-maps.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/runalyze/static-maps.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/runalyze/static-maps.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/runalyze/static-maps
[link-travis]: https://travis-ci.org/Runalyze/static-maps
[link-scrutinizer]: https://scrutinizer-ci.com/g/runalyze/static-maps/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/runalyze/static-maps
[link-downloads]: https://packagist.org/packages/runalyze/static-maps

[link-image]: https://github.com/Intervention/image
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "runalyze/static-maps",
"description": "Library to create static images from various map tile providers.",
"version": "0.4.2",
"version": "1.0.0",
"license": "MIT",
"require": {
"php": ">=8.0",
"php": ">=8.2",
"league/flysystem": "^3.12",
"intervention/image": "^2.6",
"psr/log": "^1.0"
"intervention/image": "^3.9",
"guzzlehttp/guzzle": "^7.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"ext-gd": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand All @@ -18,5 +20,9 @@
"psr-4": {
"Runalyze\\StaticMaps\\": "src/"
}
},
"scripts": {
"phpunit": "phpunit",
"php-cs-fixer": "php-cs-fixer fix"
}
}
16 changes: 8 additions & 8 deletions docs/examples/example-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@
require_once '../../vendor/autoload.php';

use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\Filesystem;
use Runalyze\StaticMaps\Cache\FilesystemCache;
use Runalyze\StaticMaps\Feature\CopyrightNotice;
use Runalyze\StaticMaps\Feature\TileMap;
use Runalyze\StaticMaps\Map\Map;
use Runalyze\StaticMaps\Renderer;
use Runalyze\StaticMaps\TileProvider;
use Runalyze\StaticMaps\TileService\OpenStreetMap;
use Runalyze\StaticMaps\TileService\OpenStreetMapDe;
use Runalyze\StaticMaps\Viewport\BoundingBox;
use Runalyze\StaticMaps\Viewport\Viewport;

$imageManager = new ImageManager(['driver' => 'gd']);
$tileService = new OpenStreetMap();
$tileCache = new FilesystemCache(new Filesystem(new Local(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, $tileCache);
$imageManager = ImageManager::gd();
$tileService = new OpenStreetMapDe();
$tileCache = new FilesystemCache(new Filesystem(new LocalFilesystemAdapter(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, new \GuzzleHttp\Client(), $tileCache);

$viewport = new Viewport(500, 350, new BoundingBox(53.40, 53.75, 9.90, 10.10), $tileService);

$map = new Map($viewport);
$map->addFeature(new TileMap($tileProvider));
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\AbstractFont $font) {
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\Typography\FontFactory $font) {
$font->file('../../resources/font/Roboto-Regular.ttf');
}));

$provider = new Renderer($imageManager);
$image = $provider->renderMap($map);

echo $image->response('png');
file_put_contents('example-1.png', $image->toPng());
Binary file modified docs/examples/example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 8 additions & 9 deletions docs/examples/example-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require_once '../../vendor/autoload.php';

use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\Filesystem;
use Runalyze\StaticMaps\Cache\FilesystemCache;
use Runalyze\StaticMaps\Feature\CopyrightNotice;
Expand All @@ -21,26 +21,25 @@
use Runalyze\StaticMaps\Map\Map;
use Runalyze\StaticMaps\Renderer;
use Runalyze\StaticMaps\TileProvider;
use Runalyze\StaticMaps\TileService\OpenStreetMap;
use Runalyze\StaticMaps\Viewport\BoundingBox;
use Runalyze\StaticMaps\TileService\OpenStreetMapDe;
use Runalyze\StaticMaps\Viewport\Viewport;

$imageManager = new ImageManager(['driver' => 'gd']);
$tileService = new OpenStreetMap();
$tileCache = new FilesystemCache(new Filesystem(new Local(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, $tileCache);
$imageManager = ImageManager::gd();
$tileService = new OpenStreetMapDe();
$tileCache = new FilesystemCache(new Filesystem(new LocalFilesystemAdapter(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, new \GuzzleHttp\Client(), $tileCache);

$route = new Route([[53.57532, 10.01534], [52.520008, 13.404954], [48.13743, 11.57549]], '#ff5500', 5);
$viewport = new Viewport(500, 350, $route->getBoundingBox(), $tileService);

$map = new Map($viewport);
$map->addFeature(new TileMap($tileProvider));
$map->addFeature($route);
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\AbstractFont $font) {
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\Typography\FontFactory $font) {
$font->file('../../resources/font/Roboto-Regular.ttf');
}));

$provider = new Renderer($imageManager);
$image = $provider->renderMap($map);

echo $image->response('png');
file_put_contents('example-2.png', $image->toPng());
Binary file modified docs/examples/example-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 8 additions & 9 deletions docs/examples/example-3.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require_once '../../vendor/autoload.php';

use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\Filesystem;
use Runalyze\StaticMaps\Cache\FilesystemCache;
use Runalyze\StaticMaps\Feature\CopyrightNotice;
Expand All @@ -21,14 +21,13 @@
use Runalyze\StaticMaps\Map\Map;
use Runalyze\StaticMaps\Renderer;
use Runalyze\StaticMaps\TileProvider;
use Runalyze\StaticMaps\TileService\OpenStreetMap;
use Runalyze\StaticMaps\Viewport\BoundingBox;
use Runalyze\StaticMaps\TileService\OpenStreetMapDe;
use Runalyze\StaticMaps\Viewport\Viewport;

$imageManager = new ImageManager(['driver' => 'gd']);
$tileService = new OpenStreetMap();
$tileCache = new FilesystemCache(new Filesystem(new Local(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, $tileCache);
$imageManager = ImageManager::gd();
$tileService = new OpenStreetMapDe();
$tileCache = new FilesystemCache(new Filesystem(new LocalFilesystemAdapter(__DIR__.'/cache/tiles')), $imageManager);
$tileProvider = new TileProvider($tileService, $imageManager, new \GuzzleHttp\Client(), $tileCache);

$coordinates = include './example-3-route.php';
$route = new Route($coordinates, '#ff5500', 2);
Expand All @@ -38,12 +37,12 @@
$map = new Map($viewport);
$map->addFeature(new TileMap($tileProvider));
$map->addFeature($route);
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\AbstractFont $font) {
$map->addFeature(new CopyrightNotice($tileService->getAttributionText(), function (\Intervention\Image\Typography\FontFactory $font) {
$font->file('../../resources/font/Roboto-Regular.ttf');
$font->size(12);
}));

$provider = new Renderer($imageManager);
$image = $provider->renderMap($map);

echo $image->response('png');
file_put_contents('example-3.png', $image->toPng());
Binary file modified docs/examples/example-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 15 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Runalyze StaticMaps">
<directory suffix="Test.php">./src/Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<exclude>
<directory>./src/Tests/</directory>
</exclude>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src/</directory>
</include>
<exclude>
<directory>./src/Tests/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Runalyze StaticMaps">
<directory>./src/Tests/</directory>
</testsuite>
</testsuites>
</phpunit>
37 changes: 22 additions & 15 deletions src/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Runalyze\StaticMaps\Tile\TileImageInterface;

class FilesystemCache implements CacheInterface
{
/** @var FilesystemInterface */
protected $Filesystem;

/** @var ImageManager */
protected $ImageManager;

public function __construct(FilesystemInterface $filesystem, ImageManager $imageManager)
{
$this->Filesystem = $filesystem;
$this->ImageManager = $imageManager;
public function __construct(
protected FilesystemOperator $Filesystem,
protected ImageManager $ImageManager
) {
}

protected function getTilePath(TileImageInterface $tile): string
Expand All @@ -45,13 +40,19 @@ protected function getTilePath(TileImageInterface $tile): string

public function hasTile(TileImageInterface $tile): bool
{
return $this->Filesystem->has($this->getTilePath($tile));
return $this->Filesystem->fileExists($this->getTilePath($tile));
}

public function saveTile(TileImageInterface $tile): bool
{
if ($tile->hasImage()) {
return $this->Filesystem->put($this->getTilePath($tile), (string)$tile->getImage()->encode('png'));
try {
$this->Filesystem->write($this->getTilePath($tile), (string)$tile->getImage()->toPng());
} catch (FilesystemException $e) {
return false;
}

return true;
}

return false;
Expand All @@ -61,7 +62,7 @@ public function getTile(TileImageInterface $tile): Image
{
$contents = $this->Filesystem->read($this->getTilePath($tile));

$tile->setImage($this->ImageManager->make($contents));
$tile->setImage($this->ImageManager->read($contents));

if (!$tile->hasImage()) {
throw new \RuntimeException(sprintf('Tile cannot be found at %s.', $this->getTilePath($tile)));
Expand All @@ -72,6 +73,12 @@ public function getTile(TileImageInterface $tile): Image

public function deleteTile(TileImageInterface $tile): bool
{
return $this->Filesystem->delete($this->getTilePath($tile));
try {
$this->Filesystem->delete($this->getTilePath($tile));
} catch (FilesystemException $e) {
return false;
}

return true;
}
}
8 changes: 4 additions & 4 deletions src/Drawer/PainterAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
trait PainterAwareTrait
{
/** @var int[] [r, g, b] */
protected $PainterColor = [0, 0, 0];
protected array $PainterColor = [0, 0, 0];

/** @var float (0.0 .. 100.0) */
protected $PainterAlpha = 100.0;
protected float $PainterAlpha = 100.0;

/** @var int [px] */
protected $LineWidth = 1;
protected int $LineWidth = 1;

public function setPainter(int $r, int $g, int $b, float $alpha = 100.0, int $lineWidth = 1)
public function setPainter(int $r, int $g, int $b, float $alpha = 100.0, int $lineWidth = 1): void
{
$this->PainterColor = [$r, $g, $b];
$this->PainterAlpha = $alpha;
Expand Down
Loading

0 comments on commit 7fc722a

Please sign in to comment.