-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to use different width calculator per conversion using Respon…
…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
Showing
4 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
tests/TestSupport/WidthCalculators/FixedWidthCalculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |