Skip to content

Commit

Permalink
Merge pull request #1 from rico-vz/feature/backgroundURL
Browse files Browse the repository at this point in the history
Add support for background image URL
  • Loading branch information
simonhamp authored Jan 6, 2024
2 parents d68a462 + 699f31c commit 2221ff4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Image
public readonly string $title;
public readonly string $url;
public readonly string $watermark;
public readonly string $backgroundUrl;

public function __construct()
{
Expand Down Expand Up @@ -110,6 +111,16 @@ public function theme(Themes|Theme $theme): self
return $this;
}

/**
* The background image from URL
*/
public function backgroundUrl(string $backgroundUrl, ?float $opacity): self
{
$this->backgroundUrl = $backgroundUrl;
$this->theme->backgroundOpacity($opacity);
return $this;
}

/**
* Override the theme's default accent color
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Interfaces/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function backgroundOpacity(float $opacity): self;

public function getBackgroundOpacity(): float;

public function backgroundUrl(string $url): self;

public function getBackgroundUrl(): ?string;

public function baseColor(string $color): self;

public function getBaseColor(): Color;
Expand Down
14 changes: 13 additions & 1 deletion src/Themes/AbstractTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
protected string $baseColor,
protected string $backgroundColor,
protected ?Background $background = null,
protected ?string $backgroundUrl = null,
protected ?float $backgroundOpacity = 1.0,
protected ?string $borderColor = null,
protected ?string $callToActionBackgroundColor = null,
Expand Down Expand Up @@ -53,6 +54,17 @@ public function getBackground(): ?Background
return $this->background;
}

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

public function getBackgroundUrl(): ?string
{
return $this->backgroundUrl;
}

public function backgroundColor(string $color): self
{
$this->backgroundColor = $color;
Expand All @@ -66,7 +78,7 @@ public function getBackgroundColor(): Color

public function backgroundOpacity(float $opacity): self
{
$this->backgroundOpacity = $opacity < 0 ? 0 : ($opacity > 1 ? 1 : $opacity);
$this->backgroundOpacity = max(0, min($opacity, 1));
return $this;
}

Expand Down
47 changes: 46 additions & 1 deletion src/Traits/RendersFeatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait RendersFeatures
protected Config $config;
protected Image $canvas;
protected ImageManager $manager;

public function render(Config $config): Image
{
$this->config = $config;
Expand All @@ -41,6 +41,18 @@ public function render(Config $config): Image
$this->renderBackground();
}

if (isset($this->config->backgroundUrl) && $backgroundUrl = $this->getUrl($this->config->backgroundUrl)) {
if (!filter_var($this->config->backgroundUrl, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('URL is not valid');
}
$imageInfo = @getimagesize($this->config->backgroundUrl);
if (!$imageInfo) {
throw new \InvalidArgumentException('URL is not an image');
}

$this->renderBackgroundUrl();
}

if (isset($this->config->url) && $url = $this->getUrl($this->config->url)) {
$this->renderTextBox($url);
}
Expand Down Expand Up @@ -178,4 +190,37 @@ protected function renderBackground(): void
$filledRows++;
}
}

/**
* Renders a background image URL across the canvas and resizes it to cover the canvas
*/
protected function renderBackgroundUrl(): void
{
$panel = $this->manager->read(file_get_contents($this->config->backgroundUrl));

$imagick = $panel->core()->native();

$imagick->setImageVirtualPixelMethod(1);
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);

$imagick->evaluateImage(
Imagick::EVALUATE_MULTIPLY,
$this->config->theme->getBackgroundOpacity(),
Imagick::CHANNEL_ALPHA
);

$width = $panel->width();
$height = $panel->height();

$panel->resize($this->width, $this->height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});

$this->canvas->place(
element: $panel,
offset_x: 0,
offset_y: 0,
);
}
}
19 changes: 18 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,22 @@
Some slightly smaller but potentially much longer subtext. It could be really long so we might need to trim it completely after many words.
TEXT)
->background(Background::JustWaves, 0.2)
->background(Background::JustWaves,0.2)
->save(__DIR__.'/test3.png');

// Basic with BackgroundUrl
(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')
->backgroundUrl('https://www.goodfreephotos.com/albums/animals/mammals/african-bush-elephant-loxodonta-africana.jpg', 0.2)
->save(__DIR__ . '/test4.png');

// Different theme with BackgroundUrl
(new Image())
->theme(Themes::Dark)
->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')
->backgroundUrl('https://www.goodfreephotos.com/albums/animals/mammals/african-bush-elephant-loxodonta-africana.jpg', 0.2)
->save(__DIR__ . '/test5.png');
Binary file removed tests/test.png
Binary file not shown.
Binary file added tests/test4.png
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/test5.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 2221ff4

Please sign in to comment.