Skip to content

Commit eb8b28d

Browse files
authored
Merge pull request #23 from simonhamp/add-watermark
Add watermark support
2 parents 7e88fbb + 20a4c65 commit eb8b28d

12 files changed

+74
-3
lines changed

src/Interfaces/Layout.php

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public function render(Config $config): Image;
2424
public function title(): string;
2525

2626
public function url(): ?string;
27+
28+
public function watermark(): ?string;
2729
}

src/Layout/AbstractLayout.php

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function url(): ?string
7575
return parse_url($this->config->url, PHP_URL_HOST) ?? $this->config->url;
7676
}
7777

78+
public function watermark(): ?string
79+
{
80+
return $this->config->watermark ?? null;
81+
}
82+
7883
/**
7984
* The area within the canvas that we should be rendering content. This is just a convenience object to help layout
8085
* of other features and is not normally rendered (it's not added to the $features list)

src/Layout/Layouts/Avatar.php

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ public function features(): void
4343
anchor: Position::MiddleTop,
4444
)
4545
);
46+
47+
if ($watermark = $this->watermark()) {
48+
$this->addFeature((new PictureBox())
49+
->path($watermark)
50+
->box(100, 100)
51+
->position(
52+
x: 1180,
53+
y: 610,
54+
anchor: Position::BottomRight
55+
)
56+
);
57+
}
4658
}
4759

4860
public function url(): string

src/Layout/Layouts/GitHubBasic.php

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use SimonHamp\TheOg\BorderPosition;
66
use SimonHamp\TheOg\Layout\AbstractLayout;
7+
use SimonHamp\TheOg\Layout\PictureBox;
78
use SimonHamp\TheOg\Layout\Position;
89
use SimonHamp\TheOg\Layout\TextBox;
910

@@ -90,5 +91,18 @@ public function features(): void
9091
)
9192
);
9293
}
94+
95+
if ($watermark = $this->watermark()) {
96+
$this->addFeature((new PictureBox())
97+
->path($watermark)
98+
->box(100, 100)
99+
->position(
100+
x: 0,
101+
y: 0,
102+
relativeTo: fn () => $this->mountArea()->anchor(Position::BottomRight),
103+
anchor: Position::BottomRight
104+
)
105+
);
106+
}
93107
}
94108
}

src/Layout/Layouts/Standard.php

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use SimonHamp\TheOg\BorderPosition;
66
use SimonHamp\TheOg\Layout\AbstractLayout;
7+
use SimonHamp\TheOg\Layout\PictureBox;
78
use SimonHamp\TheOg\Layout\Position;
89
use SimonHamp\TheOg\Layout\TextBox;
910

@@ -89,6 +90,19 @@ public function features(): void
8990
)
9091
);
9192
}
93+
94+
if ($watermark = $this->watermark()) {
95+
$this->addFeature((new PictureBox())
96+
->path($watermark)
97+
->box(100, 100)
98+
->position(
99+
x: 0,
100+
y: 0,
101+
relativeTo: fn () => $this->mountArea()->anchor(Position::BottomRight),
102+
anchor: Position::BottomRight
103+
)
104+
);
105+
}
92106
}
93107

94108
// XXX: This feels weird... maybe it should happen in the theme? Or let the content decide?

src/Layout/Layouts/TwoUp.php

+12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public function features(): void
6767
anchor: Position::BottomRight,
6868
)
6969
);
70+
71+
if ($watermark = $this->watermark()) {
72+
$this->addFeature((new PictureBox())
73+
->path($watermark)
74+
->box(100, 100)
75+
->position(
76+
x: 20,
77+
y: 610,
78+
anchor: Position::BottomLeft
79+
)
80+
);
81+
}
7082
}
7183

7284
if ($url = $this->url()) {

tests/Integration/ImageTest.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public static function snapshotImages(): iterable
5252
'more-text-features',
5353
];
5454

55+
yield 'with watermark' => [
56+
(new Image())
57+
->url('https://example.com/blog/some-blog-post-url')
58+
->title('Some blog post title that is quite big and quite long')
59+
->description('Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words')
60+
->watermark(__DIR__.'/../resources/logo.png'),
61+
'with-watermark',
62+
];
63+
5564
yield 'different theme' => [
5665
(new Image())
5766
->theme(Theme::Dark)
@@ -109,7 +118,8 @@ public static function snapshotImages(): iterable
109118
->layout(new GitHubBasic)
110119
->url('username/repo')
111120
->title('An awesome package')
112-
->background(BuiltInBackground::CloudyDay, 0.8),
121+
->background(BuiltInBackground::CloudyDay, 0.8)
122+
->watermark(__DIR__.'/../resources/logo.png'),
113123
'githubbasic-layout',
114124
];
115125

@@ -126,7 +136,8 @@ public static function snapshotImages(): iterable
126136
->url('https://my-ecommerce-store.com/')
127137
->title('This layout is great for eCommerce!')
128138
->callToAction('Buy Now →')
129-
->background(BuiltInBackground::CloudyDay, 0.8),
139+
->background(BuiltInBackground::CloudyDay, 0.8)
140+
->watermark(__DIR__.'/../resources/logo.png'),
130141
'twoup-layout',
131142
];
132143

@@ -177,7 +188,8 @@ public static function snapshotImages(): iterable
177188
->layout(new Avatar)
178189
->accentColor('#003')
179190
->picture('https://i.pravatar.cc/300?img=10')
180-
->title('Simone Hampstead'),
191+
->title('Simone Hampstead')
192+
->watermark(__DIR__.'/../resources/logo.png'),
181193
'avatar-layout',
182194
];
183195
}
Loading

tests/resources/logo.png

1020 KB
Loading

0 commit comments

Comments
 (0)