Skip to content

Commit

Permalink
Allow to format change responsive images in conversions (spatie#1752)
Browse files Browse the repository at this point in the history
Co-authored-by: Malte Buttjer <[email protected]>
  • Loading branch information
buttjer and buttjer authored Mar 9, 2020
1 parent 944f00a commit a2cc5ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ResponsiveImages/ResponsiveImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/ResponsiveImages/ResponsiveImageGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/TestModels/TestModelWithResponsiveImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\MediaLibrary\Tests\Support\TestModels;

use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\Models\Media;

class TestModelWithResponsiveImages extends TestModel
Expand All @@ -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();
}
}

0 comments on commit a2cc5ef

Please sign in to comment.