Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
19 changes: 16 additions & 3 deletions src/GlideImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading