From a2cc5ef6d0b2f57992d61eb13a6f93027efa6296 Mon Sep 17 00:00:00 2001 From: Malte Buttjer Date: Mon, 9 Mar 2020 17:41:43 +0100 Subject: [PATCH] Allow to format change responsive images in conversions (#1752) Co-authored-by: Malte Buttjer --- src/ResponsiveImages/ResponsiveImageGenerator.php | 6 +++--- .../ResponsiveImageGeneratorTest.php | 12 ++++++++++++ .../TestModels/TestModelWithResponsiveImages.php | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/ResponsiveImages/ResponsiveImageGenerator.php b/src/ResponsiveImages/ResponsiveImageGenerator.php index 0a1e3e725..771b1e71c 100644 --- a/src/ResponsiveImages/ResponsiveImageGenerator.php +++ b/src/ResponsiveImages/ResponsiveImageGenerator.php @@ -82,7 +82,7 @@ public function generateResponsiveImage( int $targetWidth, BaseTemporaryDirectory $temporaryDirectory ) { - $responsiveImagePath = $this->appendToFileName($media->file_name, "___{$conversionName}_{$targetWidth}"); + $responsiveImagePath = $this->appendToFileName($media->file_name, "___{$conversionName}_{$targetWidth}", $baseImage); $tempDestination = $temporaryDirectory->path($responsiveImagePath); @@ -133,11 +133,11 @@ public function generateTinyJpg(Media $media, string $originalImagePath, string ResponsiveImage::registerTinySvg($media, $base64Svg, $conversionName); } - protected function appendToFileName(string $filePath, string $suffix): string + protected function appendToFileName(string $filePath, string $suffix, string $extensionFilePath = null): string { $baseName = pathinfo($filePath, PATHINFO_FILENAME); - $extension = pathinfo($filePath, PATHINFO_EXTENSION); + $extension = pathinfo($extensionFilePath ?? $filePath, PATHINFO_EXTENSION); return $baseName.$suffix.'.'.$extension; } diff --git a/tests/Feature/ResponsiveImages/ResponsiveImageGeneratorTest.php b/tests/Feature/ResponsiveImages/ResponsiveImageGeneratorTest.php index 79f977c66..66b4ec245 100644 --- a/tests/Feature/ResponsiveImages/ResponsiveImageGeneratorTest.php +++ b/tests/Feature/ResponsiveImages/ResponsiveImageGeneratorTest.php @@ -33,6 +33,18 @@ public function its_conversions_can_have_responsive_images() $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___thumb_50_41.jpg')); } + /** @test */ + public function its_conversions_can_have_responsive_images_and_change_format() + { + $this->testModelWithResponsiveImages + ->addMedia($this->getTestPng()) + ->withResponsiveImages() + ->toMediaCollection(); + + $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___pngtojpg_700_883.jpg')); + $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___pngtojpg_585_737.jpg')); + } + /** @test */ public function it_triggers_an_event_when_the_responsive_images_are_generated() { diff --git a/tests/Support/TestModels/TestModelWithResponsiveImages.php b/tests/Support/TestModels/TestModelWithResponsiveImages.php index d92cf1a6f..6491038c3 100644 --- a/tests/Support/TestModels/TestModelWithResponsiveImages.php +++ b/tests/Support/TestModels/TestModelWithResponsiveImages.php @@ -2,6 +2,7 @@ namespace Spatie\MediaLibrary\Tests\Support\TestModels; +use Spatie\Image\Manipulations; use Spatie\MediaLibrary\Models\Media; class TestModelWithResponsiveImages extends TestModel @@ -20,5 +21,12 @@ public function registerMediaConversions(Media $media = null) $this->addMediaConversion('otherImageConversion') ->greyscale(); + + $this->addMediaConversion('pngtojpg') + ->width(700) + ->quality(1) + ->background('blue') + ->format(Manipulations::FORMAT_JPG) + ->withResponsiveImages(); } }