Skip to content
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

feat: add support for background image URL #1

Merged
merged 6 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Image

protected string $accentColor;
protected ?Background $background = null;
protected ?string $backgroundURL = null;
protected string $backgroundColor = '#ffffff';
protected float $backgroundOpacity = 1.0;
protected ?Border $border = null;
Expand Down Expand Up @@ -73,6 +74,22 @@ public function background(Background $background, float $opacity = 1.0): self
return $this;
}

public function backgroundURL(string $backgroundURL, float $opacity = 1.0): self
rico-vz marked this conversation as resolved.
Show resolved Hide resolved
{
if (!filter_var($backgroundURL, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('URL is not valid');
}

$imageInfo = @getimagesize($backgroundURL);
if (!$imageInfo) {
throw new \InvalidArgumentException('URL doesn\'t point to an image');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small sanity checks to make sure it's a valid image link.


$this->backgroundOpacity = $opacity < 0 ? 0 : ($opacity > 1 ? 1 : $opacity);
simonhamp marked this conversation as resolved.
Show resolved Hide resolved
$this->backgroundURL = $backgroundURL;
return $this;
}

public function callToAction(string $content): self
{
$this->callToAction = $content;
Expand Down
41 changes: 41 additions & 0 deletions src/Traits/RendersImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,45 @@ protected function renderBackground(): void
$filledRows++;
}
}

protected function renderBackgroundURL(): void
rico-vz marked this conversation as resolved.
Show resolved Hide resolved
{
$panel = $this->manager->read(file_get_contents($this->backgroundURL));
rico-vz marked this conversation as resolved.
Show resolved Hide resolved
rico-vz marked this conversation as resolved.
Show resolved Hide resolved

$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();

$columns = ceil($this->width / $width);
$rows = ceil($this->height / $height);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resize the image so it looks properly in place


$filledRows = 0;

while ($filledRows <= $rows) {
$filledColumns = 0;

while ($filledColumns <= $columns) {
$this->image->place(
element: $panel,
offset_x: $filledColumns * $width,
offset_y: $filledRows * $height,
);

$filledColumns++;
}

$filledRows++;
}
}
}
Binary file added tests/test_background_url.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/test_with_url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use SimonHamp\TheOg\Image;

include_once __DIR__.'/../vendor/autoload.php';

(new Image())
->accentColor('#cc0000')
->border()
->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) // CC0 licensed image from https://www.goodfreephotos.com/
->save(__DIR__.'/test_background_url.png');