Skip to content

Commit

Permalink
Merge pull request #18 from simonhamp/consolidate-backgrounds
Browse files Browse the repository at this point in the history
Consolidate backgrounds
  • Loading branch information
simonhamp authored Jan 13, 2024
2 parents 514f2ba + 1303550 commit cb5241c
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 98 deletions.
27 changes: 12 additions & 15 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@
use SimonHamp\TheOg\Interfaces\Layout;
use SimonHamp\TheOg\Interfaces\Theme;
use SimonHamp\TheOg\Layout\Layouts\Standard;
use SimonHamp\TheOg\Theme\BackgroundPlacement;
use SimonHamp\TheOg\Theme\Theme as BuiltInTheme;

class Image
{
public Layout $layout;

public Theme $theme;

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

public readonly string $url;
public readonly string $watermark;
public readonly string $backgroundUrl;

public function __construct()
{
Expand Down Expand Up @@ -109,16 +111,6 @@ public function theme(Theme|BuiltInTheme $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 All @@ -131,16 +123,21 @@ public function accentColor(string $color): self
/**
* Override the theme's default background
*/
public function background(Background|BuiltInBackground $background, ?float $opacity = 1.0): self
public function background(Background|BuiltInBackground $background, ?float $opacity = null, ?BackgroundPlacement $placement = null): self
{
if ($background instanceof BuiltInBackground) {
$background = $background->load();
} else {
$background = $background;
}

if (isset($opacity)) {
$background->setOpacity($opacity);
}

if (isset($placement)) {
$background->setPlacement($placement);
}

$this->theme->background($background);
$this->theme->backgroundOpacity($opacity);
return $this;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Interfaces/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@

namespace SimonHamp\TheOg\Interfaces;

use SimonHamp\TheOg\Theme\BackgroundPlacement;

interface Background
{
public function isUrl(): bool;

public function opacity(): float;

public function setOpacity(float $opacity): static;

public function path(): string;

public function setPath(string $path): static;

public function placement(): BackgroundPlacement;

public function setPlacement(BackgroundPlacement $placement): static;
}
8 changes: 0 additions & 8 deletions src/Interfaces/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ public function backgroundColor(string $color): self;

public function getBackgroundColor(): ColorInterface;

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(): ColorInterface;
Expand Down
32 changes: 1 addition & 31 deletions src/Theme/AbstractTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Interfaces\ColorInterface;
use SimonHamp\TheOg\Background as BuiltInBackground;
use SimonHamp\TheOg\Interfaces\Background;
use SimonHamp\TheOg\Interfaces\Font;
use SimonHamp\TheOg\Interfaces\Theme;
Expand All @@ -17,8 +16,6 @@ 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,
protected ?string $callToActionColor = null,
Expand All @@ -31,7 +28,6 @@ public function __construct(
protected ?Font $urlFont = null,
)
{
$this->backgroundOpacity($backgroundOpacity);
}

public function accentColor(string $color): self
Expand All @@ -45,12 +41,8 @@ public function getAccentColor(): ColorInterface
return Color::create($this->accentColor);
}

public function background(Background|BuiltInBackground $background): self
public function background(Background $background): self
{
if ($background instanceof BuiltInBackground) {
$background = $background->load();
}

$this->background = $background;
return $this;
}
Expand All @@ -60,17 +52,6 @@ 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 @@ -82,17 +63,6 @@ public function getBackgroundColor(): ColorInterface
return Color::create($this->backgroundColor);
}

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

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

public function baseColor(string $color): self
{
$this->baseColor = $color;
Expand Down
49 changes: 46 additions & 3 deletions src/Theme/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,56 @@

class Background implements BackgroundInterface
{
public function __construct(protected string $path)
protected bool $isUrl = false;

public function __construct(
protected string $path,
protected ?float $opacity = 1.0,
protected BackgroundPlacement $placement = BackgroundPlacement::Repeat,
){
$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(): BackgroundPlacement
{
return $this->placement;
}

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

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

namespace SimonHamp\TheOg\Theme;

enum BackgroundPlacement: string
{
case Repeat = 'repeat';
case Cover = 'cover';
}
67 changes: 30 additions & 37 deletions src/Traits/RendersFeatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SimonHamp\TheOg\Image as Config;
use SimonHamp\TheOg\Interfaces\Background;
use SimonHamp\TheOg\Layout\TextBox;
use SimonHamp\TheOg\Theme\BackgroundPlacement;

trait RendersFeatures
{
Expand Down Expand Up @@ -41,13 +42,7 @@ public function render(Config $config): Image
}

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();
}
Expand Down Expand Up @@ -152,7 +147,21 @@ protected function renderHorizontalAccentedRectangle(): callable
*/
protected function renderBackground(): void
{
$panel = $this->manager->read($this->config->theme->getBackground()->path());
$background = $this->config->theme->getBackground();

$path = $background->path();

if ($background->isUrl()) {
$imageInfo = @getimagesize($path);

if (!$imageInfo) {
throw new \InvalidArgumentException('Background URL provided is invalid');
}

$data = file_get_contents($path);
}

$panel = $this->manager->read($data ?? $path);

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

Expand All @@ -161,10 +170,19 @@ protected function renderBackground(): void

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

match ($background->placement()) {
BackgroundPlacement::Repeat => $this->renderBackgroundRepeat($panel),
BackgroundPlacement::Cover => $this->renderBackgroundCover($panel),
default => null,
};
}

protected function renderBackgroundRepeat(Image $panel): void
{
$width = $panel->width();
$height = $panel->height();

Expand All @@ -191,35 +209,10 @@ protected function renderBackground(): void
}

/**
* Renders a background image URL across the canvas and resizes it to cover the canvas
* Resizes the background image to cover the canvas
*/
protected function renderBackgroundUrl(): void
protected function renderBackgroundCover(Image $panel): 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,
);
$this->canvas->place($panel->cover($this->width, $this->height));
}
}
Loading

0 comments on commit cb5241c

Please sign in to comment.