Skip to content
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
34 changes: 24 additions & 10 deletions app/Jobs/GenerateSocialShareImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class GenerateSocialShareImage
{
const TEXT_X_POSITION = 50;

const TEXT_Y_POSITION = 150;
const TEXT_Y_BASE_POSITION = 90;

const TEXT_COLOUR = '#161e2e';

Expand All @@ -32,18 +32,32 @@ public function handle(): Response
$text = wordwrap($this->article->title(), self::CHARACTERS_PER_LINE);

return Cache::remember(
'articleSocialImage-'.$this->article->id,
'articleSocialImage-' . $this->article->id,
now()->addDay(),
fn () => response(
$image->read(resource_path('images/'.self::TEMPLATE))
->text($text, self::TEXT_X_POSITION, self::TEXT_Y_POSITION, function ($font) {
$font->file(resource_path('fonts/'.self::FONT));
$font->size(self::FONT_SIZE);
$font->color(self::TEXT_COLOUR);
})
fn() => response(
$image->read(resource_path('images/' . self::TEMPLATE))
->text(
$text,
self::TEXT_X_POSITION,
self::calculateTextYPosition($text),
function ($font) {
$font->file(resource_path('fonts/' . self::FONT));
$font->size(self::FONT_SIZE);
$font->color(self::TEXT_COLOUR);
}
)
->toPng()
)->header('Content-Type', 'image/png')
)
->header('Content-Type', 'image/png')
->header('Cache-Control', 'max-age=86400, public')
);
}

private function calculateTextYPosition(string $text): int
{
$noOfLinesInText = substr_count($text, "\n");

return self::TEXT_Y_BASE_POSITION
+ ((self::FONT_SIZE * $noOfLinesInText) - $noOfLinesInText);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Jobs/GenerateSocialShareImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
expect(
Pixelmatch::new(
$generatedSocialShareImagePath,
__DIR__.'/stubs/generate_social_share_image.png'
base_path('tests/stubs/generate_social_share_image.png')
)->matches()
)->toBeTrue();

Expand Down
Binary file modified tests/stubs/generate_social_share_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.