Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
Route: allow different paddings for each side

CopyrightNotice: make background color and positioning configurable
  • Loading branch information
laufhannes committed Oct 10, 2018
1 parent f7457d1 commit b4dfd11
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
38 changes: 33 additions & 5 deletions src/Feature/CopyrightNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class CopyrightNotice implements FeatureInterface
/** @var callable */
protected $FontCallback;

/** @var string */
protected $BackgroundColor = 'rgba(255, 255, 255, 0.5)';

/** @var string */
protected $Position = 'bottom-right';

/** @var int [px] */
protected $OffsetX = 0;

/** @var int [px] */
protected $OffsetY = 0;

/**
* @param string $text
* @param null|callable(AbstractFont): void $fontCallback
Expand All @@ -37,20 +49,36 @@ public function __construct(string $text, callable $fontCallback = null)
};
}

public function setBackgroundColor(string $rgba): self
{
$this->BackgroundColor = $rgba;

return $this;
}

public function setPosition(string $position, int $offsetX = 0, int $offsetY = 0): self
{
$this->Position = $position;
$this->OffsetX = $offsetX;
$this->OffsetY = $offsetY;

return $this;
}

public function render(ImageManager $imageManager, Image $image, ViewportInterface $viewport)
{
$font = new \Intervention\Image\Gd\Font($this->Text);
$this->applyFontCallbacks($font);
$size = $font->getBoxSize();

$watermarkBackground = $imageManager->canvas($size['width'] + 10, $size['height'] + 5, 'rgba(255, 255, 255, 0.5)');
$watermark = $imageManager->canvas($size['width'] + 5, $size['height']);
$watermarkBackground = $imageManager->canvas($size['width'] + 10, $size['height'] + 5, $this->BackgroundColor);
$watermark = $imageManager->canvas($size['width'], $size['height']);

$font->applyToImage($watermark, 0, 2);

$font->applyToImage($watermark);
$watermarkBackground->insert($watermark, 'center');

$image->insert($watermarkBackground, 'bottom-right');
$image->insert($watermark, 'bottom-right');
$image->insert($watermarkBackground, $this->Position, $this->OffsetX, $this->OffsetY);
}

protected function applyFontCallbacks(AbstractFont $font)
Expand Down
18 changes: 11 additions & 7 deletions src/Feature/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(array $coordinates, $lineColor = '#000', int $lineWi
$this->LineColor = $this->getLineColorArray($lineColor);
}

public function enableLineSimplification(float $pixelTolerance = 10)
public function enableLineSimplification(float $pixelTolerance = 10.0)
{
$this->LineSimplificationTolerance = $pixelTolerance;
}
Expand Down Expand Up @@ -125,12 +125,16 @@ protected function getLineSegments(array $coordinates): array
return $coordinates;
}

public function getBoundingBox(float $paddingInPercent = 0.0): BoundingBoxInterface
public function getBoundingBox(float $paddingInPercent = 0.0, float $paddingRight = null, float $paddingBottom = null, float $paddingLeft = null): BoundingBoxInterface
{
$minLatitude = 90.0;
$maxLatitude = -90.0;
$minLongitude = 180.0;
$maxLongitude = -180.0;
$paddingTop = $paddingInPercent;
$paddingRight = null === $paddingRight ? $paddingInPercent : $paddingRight;
$paddingBottom = null === $paddingBottom ? $paddingInPercent : $paddingBottom;
$paddingLeft = null === $paddingLeft ? $paddingInPercent : $paddingLeft;

foreach ($this->LineSegments as $segment) {
foreach ($segment as $coordinates) {
Expand All @@ -152,14 +156,14 @@ public function getBoundingBox(float $paddingInPercent = 0.0): BoundingBoxInterf
}
}

if ($paddingInPercent > 0.0) {
if (0.0 < max($paddingTop, $paddingRight, $paddingBottom, $paddingLeft)) {
$deltaLatitude = $maxLatitude - $minLatitude;
$deltaLongitude = $maxLongitude - $minLongitude;

$minLatitude -= $deltaLatitude * $paddingInPercent / 100.0;
$maxLatitude += $deltaLatitude * $paddingInPercent / 100.0;
$minLongitude -= $deltaLongitude * $paddingInPercent / 100.0;
$maxLongitude += $deltaLongitude * $paddingInPercent / 100.0;
$minLatitude -= $deltaLatitude * $paddingBottom / 100.0;
$maxLatitude += $deltaLatitude * $paddingTop / 100.0;
$minLongitude -= $deltaLongitude * $paddingLeft / 100.0;
$maxLongitude += $deltaLongitude * $paddingRight / 100.0;
}

return new BoundingBox($minLatitude, $maxLatitude, $minLongitude, $maxLongitude);
Expand Down
60 changes: 60 additions & 0 deletions src/Tests/Feature/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the StaticMaps.
*
* (c) RUNALYZE <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Runalyze\StaticMaps\Tests\Feature;

use PHPUnit\Framework\TestCase;
use Runalyze\StaticMaps\Feature\Route;
use Runalyze\StaticMaps\Viewport\BoundingBox;
use Runalyze\StaticMaps\Viewport\BoundingBoxInterface;

class RouteTest extends TestCase
{
public function testEmptyRoute()
{
$route = new Route([]);

$this->assertTrue($route->isEmpty());
}

public function testSimpleBoundingBox()
{
$route = new Route([
[53.57532, 10.01534],
[52.520008, 13.404954],
[48.13743, 11.57549]
]);

$this->assertFalse($route->isEmpty());
$this->assertThatBoundingBoxIsEqual(new BoundingBox(48.13743, 53.57532, 10.01534, 13.404954), $route->getBoundingBox());
}

public function testBoundingBoxWithPadding()
{
$route = new Route([
[50.0, 10.0],
[51.0, 10.1]
]);

$this->assertFalse($route->isEmpty());
$this->assertThatBoundingBoxIsEqual(new BoundingBox(50.0, 51.0, 10.0, 10.1), $route->getBoundingBox());
$this->assertThatBoundingBoxIsEqual(new BoundingBox(49.9, 51.1, 9.99, 10.11), $route->getBoundingBox(10.0));
$this->assertThatBoundingBoxIsEqual(new BoundingBox(50.0, 51.1, 9.95, 10.1), $route->getBoundingBox(10.0, 0.0, 0.0, 50.0));
}

protected function assertThatBoundingBoxIsEqual(BoundingBoxInterface $expected, BoundingBoxInterface $actual)
{
$this->assertEquals($expected->getMinLatitude(), $actual->getMinLatitude());
$this->assertEquals($expected->getMaxLatitude(), $actual->getMaxLatitude());
$this->assertEquals($expected->getMinLongitude(), $actual->getMinLongitude());
$this->assertEquals($expected->getMaxLongitude(), $actual->getMaxLongitude());
}
}

0 comments on commit b4dfd11

Please sign in to comment.