From 28a2b22e559d5aa5529b61100c7cb931530cf6bf Mon Sep 17 00:00:00 2001 From: Rias Date: Wed, 30 Oct 2024 20:14:49 +0100 Subject: [PATCH] Add a way to configure threshold --- config/responsive-images.php | 16 +++++++++++++++- src/ResponsiveDimensionCalculator.php | 2 +- tests/Feature/DimensionCalculatorTest.php | 19 ++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/config/responsive-images.php b/config/responsive-images.php index 8e0b580..d2884a9 100644 --- a/config/responsive-images.php +++ b/config/responsive-images.php @@ -36,7 +36,7 @@ | */ 'force_absolute_urls' => false, - + /* |-------------------------------------------------------------------------- | Queue @@ -61,6 +61,20 @@ 'max_width' => null, + /* + |-------------------------------------------------------------------------- + | Dimension Calculator Threshold + |-------------------------------------------------------------------------- + | + | Define the file size threshold at which the default + | dimension calculator decides to generate a new + | variant. By default, this is 30% smaller. + | Must be a value: 0 < x < 1 + | + */ + + 'dimension_calculator_threshold' => 0.7, + /* |-------------------------------------------------------------------------- | Placeholder diff --git a/src/ResponsiveDimensionCalculator.php b/src/ResponsiveDimensionCalculator.php index 021a662..1b5785e 100644 --- a/src/ResponsiveDimensionCalculator.php +++ b/src/ResponsiveDimensionCalculator.php @@ -77,7 +77,7 @@ protected function calculateDimensions(int $assetFilesize, int $assetWidth, int $pixelPrice = $predictedFileSize / $area; while (true) { - $predictedFileSize *= 0.7; + $predictedFileSize *= config('statamic.responsive-images.dimension_calculator_threshold', 0.7); $newWidth = (int) floor(sqrt(($predictedFileSize / $pixelPrice) / $ratioForFilesize)); diff --git a/tests/Feature/DimensionCalculatorTest.php b/tests/Feature/DimensionCalculatorTest.php index 5a79d3f..0da5e83 100644 --- a/tests/Feature/DimensionCalculatorTest.php +++ b/tests/Feature/DimensionCalculatorTest.php @@ -130,6 +130,23 @@ function getWidths(Asset $asset, Breakpoint $breakpoint): array ]); }); +it('can calculate the optimized widths for different dimensions with a custom threshold', function () { + config()->set('statamic.responsive-images.dimension_calculator_threshold', 0.25); + + $stubbedAsset = stubAsset(2400, 1800, 3000 * 1024); + $breakpoint = new Breakpoint($stubbedAsset, 'default', 0, []); + + $widths = getWidths($stubbedAsset, $breakpoint); + + expect($widths)->toEqual([ + 0 => 2400, + 1 => 1200, + 2 => 600, + 3 => 300, + 4 => 150, + ]); +}); + it('filters out widths to be less than max width specified in config', function() { config()->set('statamic.responsive-images.max_width', 300); @@ -205,4 +222,4 @@ function getWidths(Asset $asset, Breakpoint $breakpoint): array $calculatedDimensions = app(DimensionCalculator::class)->calculateForImgTag($breakpoint); expect($calculatedDimensions->getHeight())->toEqual(170); -}); \ No newline at end of file +});