diff --git a/config/glide.php b/config/glide.php index bb40c10..4cb642d 100644 --- a/config/glide.php +++ b/config/glide.php @@ -44,4 +44,11 @@ */ 'signed' => false, ], + + /** + * By default, upscaling is enabled up to 2x the original image size. + * This is due to Retina / HiDPI-displays, where it can be crisper + * to serve an upscaled interpolated Glide image than the original. + */ + 'upscale_enabled' => true, ]; diff --git a/src/GlideImageGenerator.php b/src/GlideImageGenerator.php index 2399128..81e6f64 100644 --- a/src/GlideImageGenerator.php +++ b/src/GlideImageGenerator.php @@ -2,6 +2,7 @@ namespace RalphJSmit\Laravel\Glide; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\URL; @@ -67,15 +68,27 @@ protected function getSrcsetAttribute(string $path, ?int $maxWidth, ?string $dis $imageWidth = $this->getImageWidth($path, $disk); + $upscaleEnabled = config('glide.upscale_enabled'); + $scale = $scale ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth) - // We will up-scale an image up to 2x it's original size. Above that it has no use anymore. - ->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth * 2)); + ->when( + $upscaleEnabled, + // We will up-scale an image up to 2x it's original size. Above that it has no use anymore. + fn (Collection $scale) => $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth * 2)), + // When upscaling is disabled, we should not generate images larger than the original image. + fn (Collection $scale) => $scale->when($imageWidth)->reject(fn (int $width) => $width > $imageWidth), + ); // Push a final version with exactly the correct max-width if the difference with the last item // in the scale is bigger than 50px. Otherwise, the additional provided type is not so useful. if ($maxWidth && ($maxWidth - $scale->last()) > 50) { - $scale->push($maxWidth); + // When upscaling is disabled, never push a width larger than the original image. + $pushWidth = (! $upscaleEnabled && $imageWidth) ? min($maxWidth, $imageWidth) : $maxWidth; + + if (($pushWidth - $scale->last()) > 50) { + $scale->push($pushWidth); + } } // We will push the exact original image width onto the scale so the image is never served