Skip to content

Commit

Permalink
Merge pull request #24 from simonhamp/picture-placements
Browse files Browse the repository at this point in the history
Picture placements
  • Loading branch information
simonhamp authored Jan 16, 2024
2 parents c2e4338 + bc6409d commit 847573d
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 25 deletions.
17 changes: 13 additions & 4 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use SimonHamp\TheOg\Layout\Layouts\Standard;
use SimonHamp\TheOg\Theme as BuiltInTheme;
use SimonHamp\TheOg\Theme\BackgroundPlacement;
use SimonHamp\TheOg\Theme\Picture;

class Image
{
Expand All @@ -22,11 +23,11 @@ class Image

public readonly string $callToAction;
public readonly string $description;
public readonly string $picture;
public readonly Picture $picture;
public readonly string $title;

public readonly string $url;
public readonly string $watermark;
public readonly Picture $watermark;

public function __construct()
{
Expand Down Expand Up @@ -55,8 +56,12 @@ public function description(string $description): self
/**
* The picture to display
*/
public function picture(string $picture): self
public function picture(string|Picture $picture): self
{
if (is_string($picture)) {
$picture = new Picture($picture);
}

$this->picture = $picture;
return $this;
}
Expand All @@ -82,8 +87,12 @@ public function url(string $url): self
/**
* The watermark image
*/
public function watermark(string $watermark, ?float $opacity = 1.0): self
public function watermark(string|Picture $watermark): self
{
if (is_string($watermark)) {
$watermark = new Picture($watermark);
}

$this->watermark = $watermark;
return $this;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Interfaces/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SimonHamp\TheOg\Border;
use SimonHamp\TheOg\Image as Config;
use SimonHamp\TheOg\Layout\TextBox;
use SimonHamp\TheOg\Theme\Picture;

interface Layout
{
Expand All @@ -17,13 +18,13 @@ public function description(): ?string;

public function features(): void;

public function picture(): ?string;
public function picture(): ?Picture;

public function render(Config $config): Image;

public function title(): string;

public function url(): ?string;

public function watermark(): ?string;
public function watermark(): ?Picture;
}
5 changes: 3 additions & 2 deletions src/Layout/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SimonHamp\TheOg\BorderPosition;
use SimonHamp\TheOg\Interfaces\Box as BoxInterface;
use SimonHamp\TheOg\Interfaces\Layout;
use SimonHamp\TheOg\Theme\Picture;
use SimonHamp\TheOg\Traits\RendersFeatures;

abstract class AbstractLayout implements Layout
Expand Down Expand Up @@ -56,7 +57,7 @@ public function description(): ?string
return $this->config->description ?? null;
}

public function picture(): ?string
public function picture(): ?Picture
{
return $this->config->picture ?? null;
}
Expand All @@ -75,7 +76,7 @@ public function url(): ?string
return parse_url($this->config->url, PHP_URL_HOST) ?? $this->config->url;
}

public function watermark(): ?string
public function watermark(): ?Picture
{
return $this->config->watermark ?? null;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Layout/Layouts/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Avatar extends AbstractLayout
public function features(): void
{
$this->addFeature((new PictureBox())
->path($this->picture())
->path($this->picture()->path())
->circle()
->box(300, 300)
->position(
Expand All @@ -46,11 +46,11 @@ public function features(): void

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->box(100, 100)
->path($watermark->path())
->box(150, 150)
->position(
x: 1180,
y: 610,
x: 1150,
y: 580,
anchor: Position::BottomRight
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Layout/Layouts/GitHubBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function features(): void

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->path($watermark->path())
->box(100, 100)
->position(
x: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/Layout/Layouts/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function features(): void

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->path($watermark->path())
->box(100, 100)
->position(
x: 0,
Expand Down
6 changes: 4 additions & 2 deletions src/Layout/Layouts/TwoUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SimonHamp\TheOg\Layout\PictureBox;
use SimonHamp\TheOg\Layout\Position;
use SimonHamp\TheOg\Layout\TextBox;
use SimonHamp\TheOg\Theme\PicturePlacement;

class TwoUp extends AbstractLayout
{
Expand All @@ -20,7 +21,8 @@ public function features(): void
{
if ($picture = $this->picture()) {
$this->addFeature((new PictureBox())
->path($picture)
->path($picture->path())
->placement($picture->placement() ?? PicturePlacement::Cover)
->box($this->width / 2, $this->height)
->position(
x: 0,
Expand Down Expand Up @@ -70,7 +72,7 @@ public function features(): void

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->path($watermark->path())
->box(100, 100)
->position(
x: 20,
Expand Down
35 changes: 28 additions & 7 deletions src/Layout/PictureBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
use Imagick;
use ImagickDraw;
use ImagickPixel;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;
use SimonHamp\TheOg\Theme\PicturePlacement;

class PictureBox extends Box
class PictureBox extends Box
{
public string $path;

/**
* @var array<callable<Imagick>>
*/
public array $maskQueue;

public string $path;

public PicturePlacement $placement = PicturePlacement::Natural;

protected ImageInterface $picture;

public function render(ImageInterface $image): void
Expand Down Expand Up @@ -71,16 +75,33 @@ public function circle(): static
return $this;
}

public function path(string $path): self
public function path(string $path): static
{
$this->path = $path;
return $this;
}

public function placement(PicturePlacement $placement): static
{
$this->placement = $placement;
return $this;
}

protected function getPicture(): ImageInterface
{
return $this->picture ??= ImageManager::imagick()
->read(file_get_contents($this->path))
->cover($this->box->width(), $this->box->height());
$this->picture ??= ImageManager::imagick()
->read(file_get_contents($this->path));

match ($this->placement) {
PicturePlacement::Cover => $this->picture->cover($this->box->width(), $this->box->height()),
PicturePlacement::Natural => $this->picture->scaleDown(min($this->box->width(), $this->box->height())),
};

return $this->picture;
}

protected function getPrerenderedBox(): Rectangle|null
{
return $this->getPicture()->size();
}
}
59 changes: 59 additions & 0 deletions src/Theme/Picture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace SimonHamp\TheOg\Theme;

class Picture
{
protected bool $isUrl = false;

public function __construct(
protected string $path,
protected ?float $opacity = 1.0,
protected ?PicturePlacement $placement = null,
){
$this->setPath($path);
$this->setOpacity($opacity);
}

public function opacity(): float
{
return $this->opacity;
}

public function setOpacity(float $opacity): static
{
$this->opacity = max(0, min($opacity, 1));
return $this;
}

public function path(): string
{
return $this->path;
}

public function setPath(string $path): static
{
if (filter_var($path, FILTER_VALIDATE_URL)) {
$this->isUrl = true;
}

$this->path = $path;
return $this;
}

public function placement(): ?PicturePlacement
{
return $this->placement ?? null;
}

public function setPlacement(PicturePlacement $placement): static
{
$this->placement = $placement;
return $this;
}

public function isUrl(): bool
{
return $this->isUrl;
}
}
10 changes: 10 additions & 0 deletions src/Theme/PicturePlacement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SimonHamp\TheOg\Theme;

enum PicturePlacement: string
{
case Natural = 'natural';

case Cover = 'cover';
}
2 changes: 1 addition & 1 deletion tests/Integration/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static function snapshotImages(): iterable
->accentColor('#003')
->picture('https://i.pravatar.cc/300?img=10')
->title('Simone Hampstead')
->watermark(__DIR__.'/../resources/logo.png'),
->watermark(__DIR__.'/../resources/wide-logo.png'),
'avatar-layout',
];
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/wide-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 847573d

Please sign in to comment.