Skip to content

Commit

Permalink
Ability to use different width calculator per conversion using Respon…
Browse files Browse the repository at this point in the history
…sive Images (#3520)

* Disabled tiny placeholder generation when use_tiny_placeholder config is disabled

* Conversion specific Width Calculator is supported

* Conversion specific Width Calculator is supported

* Revert "Conversion specific Width Calculator is supported"

This reverts commit 2864d0b.

* Revert "Conversion specific Width Calculator is supported"

This reverts commit f102ebf.

* Revert "Revert "Conversion specific Width Calculator is supported""

This reverts commit 00911c5.

* CustomWidthCalculator

* CustomWidthCalculator

* CustomWidthCalculator

* CustomWidthCalculator

---------

Co-authored-by: jhorie <jhorie@mynober.nl>
  • Loading branch information
jhorie and jhorie authored Jan 29, 2024
1 parent bcda3d0 commit 04766fe
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Conversions/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Traits\Conditionable;
use Spatie\ImageOptimizer\OptimizerChainFactory;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\WidthCalculator;
use Spatie\MediaLibrary\Support\FileNamer\FileNamer;

/** @mixin \Spatie\Image\Drivers\ImageDriver */
Expand All @@ -27,6 +28,8 @@ class Conversion

protected bool $generateResponsiveImages = false;

protected ?WidthCalculator $widthCalculator = null;

protected ?string $loadingAttributeValue;

protected int $pdfPageNumber = 1;
Expand Down Expand Up @@ -190,6 +193,18 @@ public function withResponsiveImages(): self
return $this;
}

public function withWidthCalculator(WidthCalculator $widthCalculator): self
{
$this->widthCalculator = $widthCalculator;

return $this;
}

public function getWidthCalculator(): ?WidthCalculator
{
return $this->widthCalculator;
}

public function shouldGenerateResponsiveImages(): bool
{
return $this->generateResponsiveImages;
Expand Down
4 changes: 3 additions & 1 deletion src/ResponsiveImages/ResponsiveImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function generateResponsiveImagesForConversion(Media $media, Conversion $

$media = $this->cleanResponsiveImages($media, $conversion->getName());

foreach ($this->widthCalculator->calculateWidthsFromFile($baseImage) as $width) {
$widthCalculator = $conversion->getWidthCalculator() ?? $this->widthCalculator;

foreach ($widthCalculator->calculateWidthsFromFile($baseImage) as $width) {
$this->generateResponsiveImage($media, $baseImage, $conversion->getName(), $width, $temporaryDirectory, $this->getConversionQuality($conversion));
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Conversions/ConversionCustomWidthCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php


use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;
use Spatie\MediaLibrary\Tests\TestSupport\WidthCalculators\FixedWidthCalculator;

it('can utilize various width calculators for conversions across different models', function () {
$testModel3Sizes = (new class() extends TestModel {
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('fixed_width')->withWidthCalculator(new FixedWidthCalculator([99, 60, 33]))->withResponsiveImages();
}
})::create(['name' => 'test.jpg']);;

$testModel5Sizes = (new class() extends TestModel {
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('fixed_width')->withWidthCalculator(new FixedWidthCalculator([76, 59, 44, 23, 11]))->withResponsiveImages();
}
})::create(['name' => 'test.png']);

$testModel3Sizes->addMedia($this->getTestJpg())->toMediaCollection();

$this->assertSame([
"/media/1/responsive-images/test___fixed_width_99_82.jpg",
"/media/1/responsive-images/test___fixed_width_60_49.jpg",
"/media/1/responsive-images/test___fixed_width_33_27.jpg",
], $testModel3Sizes->getFirstMedia()->getResponsiveImageUrls("fixed_width"));


$testModel5Sizes->addMedia($this->getTestPng())->toMediaCollection();

$this->assertSame([
"/media/2/responsive-images/test___fixed_width_76_96.jpg",
"/media/2/responsive-images/test___fixed_width_59_74.jpg",
"/media/2/responsive-images/test___fixed_width_44_56.jpg",
"/media/2/responsive-images/test___fixed_width_23_29.jpg",
"/media/2/responsive-images/test___fixed_width_11_14.jpg",
], $testModel5Sizes->getFirstMedia()->getResponsiveImageUrls("fixed_width"));
});
23 changes: 23 additions & 0 deletions tests/TestSupport/WidthCalculators/FixedWidthCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\MediaLibrary\Tests\TestSupport\WidthCalculators;
use Illuminate\Support\Collection;
use Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\WidthCalculator;

class FixedWidthCalculator implements WidthCalculator {

public function __construct(public array $widths)
{
}

public function calculateWidthsFromFile(string $imagePath): Collection
{

return $this->calculateWidths(0, 0, 0);
}

public function calculateWidths(int $fileSize, int $width, int $height): Collection
{
return collect($this->widths);
}
}

0 comments on commit 04766fe

Please sign in to comment.