Skip to content

Commit 0610f57

Browse files
committed
fix png / gif thumbnail generation
1 parent 93fca3f commit 0610f57

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

src/Form/Eloquent/Uploads/Thumbnails/Thumbnail.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ private function resolveEncoder(): EncoderInterface
224224
) {
225225
$class = $this->encoderClass;
226226

227-
return new $class(quality: $this->quality);
227+
return tap(new $class(), function ($encoder) {
228+
if (isset($encoder->quality)) {
229+
$encoder->quality = $this->quality;
230+
}
231+
});
228232
}
229233
throw new EncoderException('Encoder class ('.$this->encoderClass.') does not exist or does not implement EncoderInterface.');
230234
}

tests/Unit/Form/Eloquent/Uploads/SharpUploadModelTest.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,51 @@
111111
->and($thumbWasCreatedTwice)->toBeFalse();
112112
});
113113

114-
it('allows to define an encoder when creating the thumbnail', function () {
115-
$file = createImage(name: 'my-image.png');
116-
$upload = createSharpUploadModel($file);
117-
118-
$thumb = $upload->thumbnail()
119-
->setQuality(80)
120-
->when($upload->mime_type == 'image/png', fn ($thumbnail) => $thumbnail->toWebp())
121-
->make(150);
122-
123-
expect($thumb)
114+
it('allows to define an encoder when creating the thumbnail', function (string $driver) {
115+
sharp()->config()
116+
->configureUploadsThumbnailCreation(imageDriverClass: $driver);
117+
118+
expect(
119+
createSharpUploadModel(createImage(name: 'my-image.png'))->thumbnail()->setQuality(80)
120+
->toWebp()->make(150)
121+
)
124122
->toEqual('/storage/thumbnails/data/150-_q-80/my-image.webp')
125-
->and(Storage::disk('public')->exists('thumbnails/data/150-_q-80/my-image.webp'))
126-
->toBeTrue();
127-
});
123+
->and(Storage::disk('public')->exists('thumbnails/data/150-_q-80/my-image.webp'))->toBeTrue();
124+
125+
expect(
126+
createSharpUploadModel(createImage(name: 'my-image.png'))->thumbnail()->setQuality(80)
127+
->toGif()->make(150)
128+
)
129+
->toEqual('/storage/thumbnails/data/150-_q-80/my-image.gif')
130+
->and(Storage::disk('public')->exists('thumbnails/data/150-_q-80/my-image.gif'))->toBeTrue();
131+
132+
expect(
133+
createSharpUploadModel(createImage(name: 'my-image.gif'))->thumbnail()->setQuality(80)
134+
->toPng()->make(150)
135+
)
136+
->toEqual('/storage/thumbnails/data/150-_q-80/my-image.png')
137+
->and(Storage::disk('public')->exists('thumbnails/data/150-_q-80/my-image.png'))->toBeTrue();
138+
139+
expect(
140+
createSharpUploadModel(createImage(name: 'my-image.gif'))->thumbnail()->setQuality(80)
141+
->toJpeg()->make(150)
142+
)
143+
->toEqual('/storage/thumbnails/data/150-_q-80/my-image.jpeg')
144+
->and(Storage::disk('public')->exists('thumbnails/data/150-_q-80/my-image.jpeg'))->toBeTrue();
145+
146+
if ($driver === \Intervention\Image\Drivers\Imagick\Driver::class) {
147+
expect(
148+
createSharpUploadModel(createImage(name: 'my-image.png'))->thumbnail()->setQuality(80)
149+
->toAvif()->make(150)
150+
)
151+
->toEqual('/storage/thumbnails/data/150-_q-80/my-image.avif')
152+
->and(Storage::disk('public')->exists('thumbnails/data/150-_q-80/my-image.avif'))->toBeTrue();
153+
}
154+
})
155+
->with([
156+
\Intervention\Image\Drivers\Gd\Driver::class,
157+
\Intervention\Image\Drivers\Imagick\Driver::class,
158+
]);
128159

129160
it('allows to create SVG thumbnail by only copying the file', function () {
130161
$file = createImage('local', 'test.svg');

0 commit comments

Comments
 (0)