Skip to content

Commit 847573d

Browse files
authored
Merge pull request #24 from simonhamp/picture-placements
Picture placements
2 parents c2e4338 + bc6409d commit 847573d

13 files changed

Lines changed: 128 additions & 25 deletions

File tree

src/Image.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use SimonHamp\TheOg\Layout\Layouts\Standard;
1414
use SimonHamp\TheOg\Theme as BuiltInTheme;
1515
use SimonHamp\TheOg\Theme\BackgroundPlacement;
16+
use SimonHamp\TheOg\Theme\Picture;
1617

1718
class Image
1819
{
@@ -22,11 +23,11 @@ class Image
2223

2324
public readonly string $callToAction;
2425
public readonly string $description;
25-
public readonly string $picture;
26+
public readonly Picture $picture;
2627
public readonly string $title;
2728

2829
public readonly string $url;
29-
public readonly string $watermark;
30+
public readonly Picture $watermark;
3031

3132
public function __construct()
3233
{
@@ -55,8 +56,12 @@ public function description(string $description): self
5556
/**
5657
* The picture to display
5758
*/
58-
public function picture(string $picture): self
59+
public function picture(string|Picture $picture): self
5960
{
61+
if (is_string($picture)) {
62+
$picture = new Picture($picture);
63+
}
64+
6065
$this->picture = $picture;
6166
return $this;
6267
}
@@ -82,8 +87,12 @@ public function url(string $url): self
8287
/**
8388
* The watermark image
8489
*/
85-
public function watermark(string $watermark, ?float $opacity = 1.0): self
90+
public function watermark(string|Picture $watermark): self
8691
{
92+
if (is_string($watermark)) {
93+
$watermark = new Picture($watermark);
94+
}
95+
8796
$this->watermark = $watermark;
8897
return $this;
8998
}

src/Interfaces/Layout.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use SimonHamp\TheOg\Border;
77
use SimonHamp\TheOg\Image as Config;
88
use SimonHamp\TheOg\Layout\TextBox;
9+
use SimonHamp\TheOg\Theme\Picture;
910

1011
interface Layout
1112
{
@@ -17,13 +18,13 @@ public function description(): ?string;
1718

1819
public function features(): void;
1920

20-
public function picture(): ?string;
21+
public function picture(): ?Picture;
2122

2223
public function render(Config $config): Image;
2324

2425
public function title(): string;
2526

2627
public function url(): ?string;
2728

28-
public function watermark(): ?string;
29+
public function watermark(): ?Picture;
2930
}

src/Layout/AbstractLayout.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use SimonHamp\TheOg\BorderPosition;
77
use SimonHamp\TheOg\Interfaces\Box as BoxInterface;
88
use SimonHamp\TheOg\Interfaces\Layout;
9+
use SimonHamp\TheOg\Theme\Picture;
910
use SimonHamp\TheOg\Traits\RendersFeatures;
1011

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

59-
public function picture(): ?string
60+
public function picture(): ?Picture
6061
{
6162
return $this->config->picture ?? null;
6263
}
@@ -75,7 +76,7 @@ public function url(): ?string
7576
return parse_url($this->config->url, PHP_URL_HOST) ?? $this->config->url;
7677
}
7778

78-
public function watermark(): ?string
79+
public function watermark(): ?Picture
7980
{
8081
return $this->config->watermark ?? null;
8182
}

src/Layout/Layouts/Avatar.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Avatar extends AbstractLayout
1919
public function features(): void
2020
{
2121
$this->addFeature((new PictureBox())
22-
->path($this->picture())
22+
->path($this->picture()->path())
2323
->circle()
2424
->box(300, 300)
2525
->position(
@@ -46,11 +46,11 @@ public function features(): void
4646

4747
if ($watermark = $this->watermark()) {
4848
$this->addFeature((new PictureBox())
49-
->path($watermark)
50-
->box(100, 100)
49+
->path($watermark->path())
50+
->box(150, 150)
5151
->position(
52-
x: 1180,
53-
y: 610,
52+
x: 1150,
53+
y: 580,
5454
anchor: Position::BottomRight
5555
)
5656
);

src/Layout/Layouts/GitHubBasic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function features(): void
9494

9595
if ($watermark = $this->watermark()) {
9696
$this->addFeature((new PictureBox())
97-
->path($watermark)
97+
->path($watermark->path())
9898
->box(100, 100)
9999
->position(
100100
x: 0,

src/Layout/Layouts/Standard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function features(): void
9393

9494
if ($watermark = $this->watermark()) {
9595
$this->addFeature((new PictureBox())
96-
->path($watermark)
96+
->path($watermark->path())
9797
->box(100, 100)
9898
->position(
9999
x: 0,

src/Layout/Layouts/TwoUp.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SimonHamp\TheOg\Layout\PictureBox;
88
use SimonHamp\TheOg\Layout\Position;
99
use SimonHamp\TheOg\Layout\TextBox;
10+
use SimonHamp\TheOg\Theme\PicturePlacement;
1011

1112
class TwoUp extends AbstractLayout
1213
{
@@ -20,7 +21,8 @@ public function features(): void
2021
{
2122
if ($picture = $this->picture()) {
2223
$this->addFeature((new PictureBox())
23-
->path($picture)
24+
->path($picture->path())
25+
->placement($picture->placement() ?? PicturePlacement::Cover)
2426
->box($this->width / 2, $this->height)
2527
->position(
2628
x: 0,
@@ -70,7 +72,7 @@ public function features(): void
7072

7173
if ($watermark = $this->watermark()) {
7274
$this->addFeature((new PictureBox())
73-
->path($watermark)
75+
->path($watermark->path())
7476
->box(100, 100)
7577
->position(
7678
x: 20,

src/Layout/PictureBox.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
use Imagick;
66
use ImagickDraw;
77
use ImagickPixel;
8+
use Intervention\Image\Geometry\Rectangle;
89
use Intervention\Image\ImageManager;
910
use Intervention\Image\Interfaces\ImageInterface;
11+
use SimonHamp\TheOg\Theme\PicturePlacement;
1012

11-
class PictureBox extends Box
13+
class PictureBox extends Box
1214
{
13-
public string $path;
14-
1515
/**
1616
* @var array<callable<Imagick>>
1717
*/
1818
public array $maskQueue;
1919

20+
public string $path;
21+
22+
public PicturePlacement $placement = PicturePlacement::Natural;
23+
2024
protected ImageInterface $picture;
2125

2226
public function render(ImageInterface $image): void
@@ -71,16 +75,33 @@ public function circle(): static
7175
return $this;
7276
}
7377

74-
public function path(string $path): self
78+
public function path(string $path): static
7579
{
7680
$this->path = $path;
7781
return $this;
7882
}
7983

84+
public function placement(PicturePlacement $placement): static
85+
{
86+
$this->placement = $placement;
87+
return $this;
88+
}
89+
8090
protected function getPicture(): ImageInterface
8191
{
82-
return $this->picture ??= ImageManager::imagick()
83-
->read(file_get_contents($this->path))
84-
->cover($this->box->width(), $this->box->height());
92+
$this->picture ??= ImageManager::imagick()
93+
->read(file_get_contents($this->path));
94+
95+
match ($this->placement) {
96+
PicturePlacement::Cover => $this->picture->cover($this->box->width(), $this->box->height()),
97+
PicturePlacement::Natural => $this->picture->scaleDown(min($this->box->width(), $this->box->height())),
98+
};
99+
100+
return $this->picture;
101+
}
102+
103+
protected function getPrerenderedBox(): Rectangle|null
104+
{
105+
return $this->getPicture()->size();
85106
}
86107
}

src/Theme/Picture.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace SimonHamp\TheOg\Theme;
4+
5+
class Picture
6+
{
7+
protected bool $isUrl = false;
8+
9+
public function __construct(
10+
protected string $path,
11+
protected ?float $opacity = 1.0,
12+
protected ?PicturePlacement $placement = null,
13+
){
14+
$this->setPath($path);
15+
$this->setOpacity($opacity);
16+
}
17+
18+
public function opacity(): float
19+
{
20+
return $this->opacity;
21+
}
22+
23+
public function setOpacity(float $opacity): static
24+
{
25+
$this->opacity = max(0, min($opacity, 1));
26+
return $this;
27+
}
28+
29+
public function path(): string
30+
{
31+
return $this->path;
32+
}
33+
34+
public function setPath(string $path): static
35+
{
36+
if (filter_var($path, FILTER_VALIDATE_URL)) {
37+
$this->isUrl = true;
38+
}
39+
40+
$this->path = $path;
41+
return $this;
42+
}
43+
44+
public function placement(): ?PicturePlacement
45+
{
46+
return $this->placement ?? null;
47+
}
48+
49+
public function setPlacement(PicturePlacement $placement): static
50+
{
51+
$this->placement = $placement;
52+
return $this;
53+
}
54+
55+
public function isUrl(): bool
56+
{
57+
return $this->isUrl;
58+
}
59+
}

src/Theme/PicturePlacement.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SimonHamp\TheOg\Theme;
4+
5+
enum PicturePlacement: string
6+
{
7+
case Natural = 'natural';
8+
9+
case Cover = 'cover';
10+
}

0 commit comments

Comments
 (0)