Skip to content

Add watermark support #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Interfaces/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public function render(Config $config): Image;
public function title(): string;

public function url(): ?string;

public function watermark(): ?string;
}
5 changes: 5 additions & 0 deletions src/Layout/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function url(): ?string
return parse_url($this->config->url, PHP_URL_HOST) ?? $this->config->url;
}

public function watermark(): ?string
{
return $this->config->watermark ?? null;
}

/**
* The area within the canvas that we should be rendering content. This is just a convenience object to help layout
* of other features and is not normally rendered (it's not added to the $features list)
Expand Down
12 changes: 12 additions & 0 deletions src/Layout/Layouts/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public function features(): void
anchor: Position::MiddleTop,
)
);

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->box(100, 100)
->position(
x: 1180,
y: 610,
anchor: Position::BottomRight
)
);
}
}

public function url(): string
Expand Down
14 changes: 14 additions & 0 deletions src/Layout/Layouts/GitHubBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SimonHamp\TheOg\BorderPosition;
use SimonHamp\TheOg\Layout\AbstractLayout;
use SimonHamp\TheOg\Layout\PictureBox;
use SimonHamp\TheOg\Layout\Position;
use SimonHamp\TheOg\Layout\TextBox;

Expand Down Expand Up @@ -90,5 +91,18 @@ public function features(): void
)
);
}

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->box(100, 100)
->position(
x: 0,
y: 0,
relativeTo: fn () => $this->mountArea()->anchor(Position::BottomRight),
anchor: Position::BottomRight
)
);
}
}
}
14 changes: 14 additions & 0 deletions src/Layout/Layouts/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SimonHamp\TheOg\BorderPosition;
use SimonHamp\TheOg\Layout\AbstractLayout;
use SimonHamp\TheOg\Layout\PictureBox;
use SimonHamp\TheOg\Layout\Position;
use SimonHamp\TheOg\Layout\TextBox;

Expand Down Expand Up @@ -89,6 +90,19 @@ public function features(): void
)
);
}

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->box(100, 100)
->position(
x: 0,
y: 0,
relativeTo: fn () => $this->mountArea()->anchor(Position::BottomRight),
anchor: Position::BottomRight
)
);
}
}

// XXX: This feels weird... maybe it should happen in the theme? Or let the content decide?
Expand Down
12 changes: 12 additions & 0 deletions src/Layout/Layouts/TwoUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public function features(): void
anchor: Position::BottomRight,
)
);

if ($watermark = $this->watermark()) {
$this->addFeature((new PictureBox())
->path($watermark)
->box(100, 100)
->position(
x: 20,
y: 610,
anchor: Position::BottomLeft
)
);
}
}

if ($url = $this->url()) {
Expand Down
18 changes: 15 additions & 3 deletions tests/Integration/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public static function snapshotImages(): iterable
'more-text-features',
];

yield 'with watermark' => [
(new Image())
->url('https://example.com/blog/some-blog-post-url')
->title('Some blog post title that is quite big and quite long')
->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')
->watermark(__DIR__.'/../resources/logo.png'),
'with-watermark',
];

yield 'different theme' => [
(new Image())
->theme(Theme::Dark)
Expand Down Expand Up @@ -109,7 +118,8 @@ public static function snapshotImages(): iterable
->layout(new GitHubBasic)
->url('username/repo')
->title('An awesome package')
->background(BuiltInBackground::CloudyDay, 0.8),
->background(BuiltInBackground::CloudyDay, 0.8)
->watermark(__DIR__.'/../resources/logo.png'),
'githubbasic-layout',
];

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

Expand Down Expand Up @@ -177,7 +188,8 @@ public static function snapshotImages(): iterable
->layout(new Avatar)
->accentColor('#003')
->picture('https://i.pravatar.cc/300?img=10')
->title('Simone Hampstead'),
->title('Simone Hampstead')
->watermark(__DIR__.'/../resources/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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.