From 923229145c50887a575ee01e6a6608f533c3ef7c Mon Sep 17 00:00:00 2001 From: Balazs Nasz <788569+balazsnasz@users.noreply.github.com> Date: Tue, 26 May 2026 03:09:12 +0200 Subject: [PATCH 1/5] feat: Ability to disable upscaling. --- config/glide.php | 2 ++ src/GlideImageGenerator.php | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/config/glide.php b/config/glide.php index d593ed6..5cbf11a 100644 --- a/config/glide.php +++ b/config/glide.php @@ -23,4 +23,6 @@ */ 'signed' => false, ], + + 'disable_upscaling' => false, ]; diff --git a/src/GlideImageGenerator.php b/src/GlideImageGenerator.php index 0bac5ae..e21ecde 100644 --- a/src/GlideImageGenerator.php +++ b/src/GlideImageGenerator.php @@ -84,9 +84,14 @@ protected function getSrcsetAttribute(string $path, ?int $maxWidth, ?string $dis $imageWidth = $this->getImageWidth($path, $disk); $scale = $scale - ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth) + ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth); + // If upscaling is disabled (e.g. config parameter set to true), we should not generate images larger than the original image. + if (config('glide.disable_upscaling')) { + $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth)); + } else { // 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)); + $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth * 2)); + } // 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. From d0a509530e8ae6a5a868ed9a947b1fa6e96ecba7 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" Date: Thu, 28 May 2026 22:49:29 +0200 Subject: [PATCH 2/5] Prevent upscaling at all --- README.md | 2 +- config/glide.php | 2 -- src/GlideImageGenerator.php | 11 +++-------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6c3b0d3..6979857 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Glide will **cache all images**, so that it doesn't have to generate the same im Because the browser in this case only requests the 1600px, the other URLs are **not called** and therefore also **not processed** by Glide. This solution is therefore perfect, because it will only do the **minimum amount of work**. -Your image will automatically be upscaled to a maximum of 2x the resolution provided in your original image. +Glide will never upscale an image beyond its original resolution, so generated versions are always capped at the source image's width. The `glide()->src()` function is even **auto-completed** if you use Laravel Idea to the files in your public-/asset-path. diff --git a/config/glide.php b/config/glide.php index 5cbf11a..d593ed6 100644 --- a/config/glide.php +++ b/config/glide.php @@ -23,6 +23,4 @@ */ 'signed' => false, ], - - 'disable_upscaling' => false, ]; diff --git a/src/GlideImageGenerator.php b/src/GlideImageGenerator.php index e21ecde..6e4539c 100644 --- a/src/GlideImageGenerator.php +++ b/src/GlideImageGenerator.php @@ -84,14 +84,9 @@ protected function getSrcsetAttribute(string $path, ?int $maxWidth, ?string $dis $imageWidth = $this->getImageWidth($path, $disk); $scale = $scale - ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth); - // If upscaling is disabled (e.g. config parameter set to true), we should not generate images larger than the original image. - if (config('glide.disable_upscaling')) { - $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth)); - } else { - // We will up-scale an image up to 2x it's original size. Above that it has no use anymore. - $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth * 2)); - } + ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth) + // We never up-scale: generating images larger than the original is wasteful and lowers quality. + ->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. From 819c07bed2c2c888b0c131a8719215d25bdd0d09 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" Date: Thu, 28 May 2026 22:53:03 +0200 Subject: [PATCH 3/5] Revert "Prevent upscaling at all" This reverts commit d0a509530e8ae6a5a868ed9a947b1fa6e96ecba7. --- README.md | 2 +- config/glide.php | 2 ++ src/GlideImageGenerator.php | 11 ++++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6979857..6c3b0d3 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Glide will **cache all images**, so that it doesn't have to generate the same im Because the browser in this case only requests the 1600px, the other URLs are **not called** and therefore also **not processed** by Glide. This solution is therefore perfect, because it will only do the **minimum amount of work**. -Glide will never upscale an image beyond its original resolution, so generated versions are always capped at the source image's width. +Your image will automatically be upscaled to a maximum of 2x the resolution provided in your original image. The `glide()->src()` function is even **auto-completed** if you use Laravel Idea to the files in your public-/asset-path. diff --git a/config/glide.php b/config/glide.php index bb40c10..9d1a487 100644 --- a/config/glide.php +++ b/config/glide.php @@ -44,4 +44,6 @@ */ 'signed' => false, ], + + 'disable_upscaling' => false, ]; diff --git a/src/GlideImageGenerator.php b/src/GlideImageGenerator.php index 842f258..772293c 100644 --- a/src/GlideImageGenerator.php +++ b/src/GlideImageGenerator.php @@ -68,9 +68,14 @@ protected function getSrcsetAttribute(string $path, ?int $maxWidth, ?string $dis $imageWidth = $this->getImageWidth($path, $disk); $scale = $scale - ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth) - // We never up-scale: generating images larger than the original is wasteful and lowers quality. - ->when($imageWidth)->reject(fn (int $width) => $width > $imageWidth); + ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth); + // If upscaling is disabled (e.g. config parameter set to true), we should not generate images larger than the original image. + if (config('glide.disable_upscaling')) { + $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth)); + } else { + // We will up-scale an image up to 2x it's original size. Above that it has no use anymore. + $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth * 2)); + } // 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. From 198ec0219a3e1bfc777769e260d435ac9c573641 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" Date: Thu, 28 May 2026 22:56:13 +0200 Subject: [PATCH 4/5] WIP --- config/glide.php | 7 ++++++- src/GlideImageGenerator.php | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/config/glide.php b/config/glide.php index 9d1a487..4cb642d 100644 --- a/config/glide.php +++ b/config/glide.php @@ -45,5 +45,10 @@ 'signed' => false, ], - 'disable_upscaling' => 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 772293c..3bf3f52 100644 --- a/src/GlideImageGenerator.php +++ b/src/GlideImageGenerator.php @@ -70,7 +70,7 @@ protected function getSrcsetAttribute(string $path, ?int $maxWidth, ?string $dis $scale = $scale ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth); // If upscaling is disabled (e.g. config parameter set to true), we should not generate images larger than the original image. - if (config('glide.disable_upscaling')) { + if (! config('glide.upscale_enabled')) { $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth)); } else { // We will up-scale an image up to 2x it's original size. Above that it has no use anymore. From 4caff14adbe2904cb22647f0c7082720ce01ec1c Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" Date: Thu, 28 May 2026 22:59:17 +0200 Subject: [PATCH 5/5] Update GlideImageGenerator.php --- src/GlideImageGenerator.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/GlideImageGenerator.php b/src/GlideImageGenerator.php index 3bf3f52..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,20 +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); - // If upscaling is disabled (e.g. config parameter set to true), we should not generate images larger than the original image. - if (! config('glide.upscale_enabled')) { - $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth)); - } else { - // We will up-scale an image up to 2x it's original size. Above that it has no use anymore. - $scale = $scale->when($imageWidth)->reject(fn (int $width) => $width > ($imageWidth * 2)); - } + ->when($maxWidth)->reject(fn (int $width) => $width > $maxWidth) + ->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