From d92a827644348eff5b7efd258c68ff9b26835d06 Mon Sep 17 00:00:00 2001 From: Tauseef Shah Date: Sun, 16 Mar 2025 22:11:46 +0530 Subject: [PATCH 1/3] fix: GenerateSocialShareImage to calculate text y position relatively based on the no of lines --- app/Jobs/GenerateSocialShareImage.php | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/app/Jobs/GenerateSocialShareImage.php b/app/Jobs/GenerateSocialShareImage.php index e7c60489a..f72012510 100644 --- a/app/Jobs/GenerateSocialShareImage.php +++ b/app/Jobs/GenerateSocialShareImage.php @@ -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'; @@ -32,18 +32,30 @@ 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 + { + return self::TEXT_Y_BASE_POSITION + + ((self::FONT_SIZE * substr_count($text, "\n")) - substr_count($text, "\n")); + } } From d32365c1310c1c01b2a12cce3ed979e287b3bda1 Mon Sep 17 00:00:00 2001 From: Tauseef Shah Date: Sun, 16 Mar 2025 22:15:00 +0530 Subject: [PATCH 2/3] test: refactoring test for generate social share image --- .../Jobs/GenerateSocialShareImageTest.php | 2 +- tests/stubs/generate_social_share_image.png | Bin 49427 -> 49427 bytes 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Jobs/GenerateSocialShareImageTest.php b/tests/Integration/Jobs/GenerateSocialShareImageTest.php index b966dd5fc..57deaa5f9 100644 --- a/tests/Integration/Jobs/GenerateSocialShareImageTest.php +++ b/tests/Integration/Jobs/GenerateSocialShareImageTest.php @@ -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(); diff --git a/tests/stubs/generate_social_share_image.png b/tests/stubs/generate_social_share_image.png index a915202f691104722d5f41259722c8310ba38493..6f46569d3a6b82b86d105be835edadb0e57b29e1 100644 GIT binary patch delta 98 zcmbQ-#5}o)c|(oVW^QR?*2!K9{XCwli#9MYC@^@sIEHM#sZc0CIn1G-r&BUR3NHH8 v;hp$ofr$Rimm`|lSd)d(jRG5~?6tDnm{r-UW|@S-Dm From ba6a3431c84294b53cb167b5355019ea096fcf66 Mon Sep 17 00:00:00 2001 From: Tauseef Shah Date: Sun, 16 Mar 2025 22:22:18 +0530 Subject: [PATCH 3/3] wip --- app/Jobs/GenerateSocialShareImage.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Jobs/GenerateSocialShareImage.php b/app/Jobs/GenerateSocialShareImage.php index f72012510..ff3bd7b70 100644 --- a/app/Jobs/GenerateSocialShareImage.php +++ b/app/Jobs/GenerateSocialShareImage.php @@ -55,7 +55,9 @@ function ($font) { private function calculateTextYPosition(string $text): int { + $noOfLinesInText = substr_count($text, "\n"); + return self::TEXT_Y_BASE_POSITION - + ((self::FONT_SIZE * substr_count($text, "\n")) - substr_count($text, "\n")); + + ((self::FONT_SIZE * $noOfLinesInText) - $noOfLinesInText); } }