Skip to content

Commit

Permalink
Add background opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhamp committed Jan 2, 2024
1 parent efd5b20 commit 2196540
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Image
protected string $accentColor;
protected ?Background $background = null;
protected string $backgroundColor = '#ffffff';
protected float $backgroundOpacity = 1.0;
protected ?Border $border = null;
protected string $callToAction;
protected string $description;
Expand Down Expand Up @@ -64,13 +65,14 @@ public function accentColor(string $hexCode): self
$this->accentColor = $hexCode;
return $this;
}
public function background(Background $background): self

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

public function callToAction(string $content): self
{
$this->callToAction = $content;
Expand Down
9 changes: 8 additions & 1 deletion src/Traits/RendersImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimonHamp\TheOg\Traits;

use Imagick;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Intervention\Image\Typography\FontFactory;
Expand Down Expand Up @@ -144,9 +145,15 @@ protected function renderHorizontalAccentedRectangle(): callable

protected function renderBackground(): void
{
// How many iterations are needed to fill the width?
$panel = $this->manager->read($this->background->path());

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

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

$imagick->evaluateImage(Imagick::EVALUATE_MULTIPLY, $this->backgroundOpacity, Imagick::CHANNEL_ALPHA);

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

Expand Down
2 changes: 1 addition & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
->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')
->background(Background::JustWaves)
->background(Background::JustWaves, 0.2)
->save(__DIR__.'/test.png');

0 comments on commit 2196540

Please sign in to comment.