spatie / statamic-responsive-images

Responsive images for Statamic 3
MIT License
99 stars 29 forks source link

Add a config variable to the srcset generation multiplier #219

Closed antonhedling closed 1 year ago

antonhedling commented 1 year ago

When using this package, I believe it sometimes generates too many variants in the srcset tag. By simply allowing us to change the size multiplier, one can alter this behavior to their liking.

Feel free to change any of the config names if there's something more suitable.

ncla commented 1 year ago

One can bind a custom DimensionCalculator in service provider, which allows you to change behavior of dimension calculators how you see fit.

        $this->app->bind(DimensionCalculator::class, function () {
            return new class extends ResponsiveDimensionCalculator {
               // ...
            };
        });

However I just realized calculateDimensions in ResponsiveDimensionCalculator is private and therefor cannot be extended. But you can just copy paste the class.

Would something like this work for you?

antonhedling commented 1 year ago

I mean yes, that works! But it felt unnecessary to re-bind & recreate the entire class when all I want to do is alter the behaviour slightly. Just thought this was a nice little addition :)

ncla commented 1 year ago

Sorry I will have to close this. In my opinion if you are thinking about how many and what srcsets get generated then extending DimensionCalculator is the right choice. I have loosened up the methods in ResponsiveDimensionCalculator to allow for extending so you do not have to copy the whole class, this is available in v4.0.1.

antonhedling commented 1 year ago

Better than before, but I personally disagree. Still, a fair bit of code in that method to copy & paste before being able to alter the ratio. Since we're planning on changing this in most of our projects it's a fairly tedious thing, especially if the code were to change in upcoming updates; oh well