From 690390aeddf20234e37bfc276c551951694cd3ca Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Mon, 6 Jan 2025 16:54:22 +0100 Subject: [PATCH 1/3] Thumbnail __toString --- src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php b/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php index 318c8aadf..7df293ea0 100644 --- a/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php +++ b/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php @@ -262,4 +262,10 @@ private function resolveThumbnailPath(?int $width = null, ?int $height = null): // Strip double / return Str::replace('//', '/', $thumbnailPath); } + + public function __toString() + { + // If thumbnail is used in a blade template without parameters, we want to return the URL directly + return $this->make(); + } } From 56478723b054c7132860eefb53bfa7b3cbfec2f6 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Tue, 7 Jan 2025 11:38:14 +0100 Subject: [PATCH 2/3] Add test + Fix comment --- src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php | 2 +- .../Form/Eloquent/Uploads/SharpUploadModelTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php b/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php index 7df293ea0..3e62536ec 100644 --- a/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php +++ b/src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php @@ -265,7 +265,7 @@ private function resolveThumbnailPath(?int $width = null, ?int $height = null): public function __toString() { - // If thumbnail is used in a blade template without parameters, we want to return the URL directly + // Return URL when Thumbnail is used as a string return $this->make(); } } diff --git a/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php b/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php index b35cdfc13..603114e0e 100644 --- a/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php +++ b/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php @@ -1,6 +1,7 @@ toBeTrue(); }); +it('allows to display thumbnails with no width or height params', function () { + $file = createImage(); + $upload = createSharpUploadModel($file); + + expect($upload->thumbnail()) + ->toBeInstanceOf(Thumbnail::class) + ->and($upload->thumbnail()->__toString()) + ->toStartWith('/storage/thumbnails/data/-_q-90/'.basename($file)); +}); + it('returns null on error with a thumbnail creation', function () { $file = createImage(); $upload = createSharpUploadModel($file); From 8cf7508d2f4d265080f3716060bec91a0cdf7f13 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Tue, 7 Jan 2025 11:41:53 +0100 Subject: [PATCH 3/3] Small test improvment --- tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php b/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php index 603114e0e..3e1addf8f 100644 --- a/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php +++ b/tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php @@ -50,7 +50,7 @@ expect($upload->thumbnail()) ->toBeInstanceOf(Thumbnail::class) - ->and($upload->thumbnail()->__toString()) + ->and((string) $upload->thumbnail()) ->toStartWith('/storage/thumbnails/data/-_q-90/'.basename($file)); });