-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #399 from thephpleague/feat/progressive-interlaced
Feat/progressive interlaced
- Loading branch information
Showing
11 changed files
with
426 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
layout: default | ||
title: Progressive & Interlaced | ||
--- | ||
|
||
## Progressive & Interlaced Images | ||
|
||
## Interlace `interlace` | ||
|
||
The `interlace` parameter controls whether an image is rendered in a progressive or interlaced format. This feature enhances the loading experience of images, making them appear gradually as they are downloaded, which can improve the user experience on slower connections. | ||
|
||
> Caution: For GIF/PNG, it can generate a slightly larger file size. | ||
### Supported Formats | ||
|
||
- **JPG**: The `onterlace` parameter applies a progressive scan to JPG images. | ||
- **PNG** and **GIF**: The `interlace` parameter enables interlacing for GIF/PNG images. | ||
|
||
> Note: When `ext` is set to `.pjpg`, it will automatically generate a progressive JPG image, regardless of the `interlace` parameter. | ||
~~~ html | ||
<img src="kayaks.jpg?interlace=1"> | ||
<img src="logo.png?interlace=1"> | ||
~~~ | ||
|
||
[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?interlace=1)](https://glide.herokuapp.com/1.0/kayaks.jpg?interlace=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace League\Glide\Api; | ||
|
||
use Intervention\Image\Interfaces\EncodedImageInterface; | ||
use Intervention\Image\Interfaces\ImageInterface; | ||
|
||
/** | ||
* Encoder Api class to convert a given image to a specific format. | ||
*/ | ||
class Encoder | ||
{ | ||
/** | ||
* The manipulation params. | ||
*/ | ||
protected array $params; | ||
|
||
/** | ||
* Class constructor. | ||
* | ||
* @param array $params the manipulator params | ||
*/ | ||
public function __construct(array $params = []) | ||
{ | ||
$this->params = $params; | ||
} | ||
|
||
/** | ||
* Set the manipulation params. | ||
* | ||
* @param array $params The manipulation params. | ||
* | ||
* @return $this | ||
*/ | ||
public function setParams(array $params) | ||
{ | ||
$this->params = $params; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get a specific manipulation param. | ||
*/ | ||
public function getParam(string $name): mixed | ||
{ | ||
return array_key_exists($name, $this->params) | ||
? $this->params[$name] | ||
: null; | ||
} | ||
|
||
/** | ||
* Perform output image manipulation. | ||
* | ||
* @param ImageInterface $image The source image. | ||
* | ||
* @return EncodedImageInterface The encoded image. | ||
*/ | ||
public function run(ImageInterface $image): EncodedImageInterface | ||
{ | ||
$format = $this->getFormat($image); | ||
$quality = $this->getQuality(); | ||
$shouldInterlace = filter_var($this->getParam('interlace'), FILTER_VALIDATE_BOOLEAN); | ||
|
||
if ('pjpg' === $format) { | ||
$shouldInterlace = true; | ||
$format = 'jpg'; | ||
} | ||
|
||
$encoderOptions = ['extension' => $format]; | ||
switch ($format) { | ||
case 'avif': | ||
case 'heic': | ||
case 'tiff': | ||
case 'webp': | ||
$encoderOptions['quality'] = $quality; | ||
break; | ||
case 'jpg': | ||
$encoderOptions['quality'] = $quality; | ||
$encoderOptions['progressive'] = $shouldInterlace; | ||
break; | ||
case 'gif': | ||
case 'png': | ||
$encoderOptions['interlaced'] = $shouldInterlace; | ||
break; | ||
default: | ||
throw new \Exception("Invalid format provided: {$format}"); | ||
} | ||
|
||
return $image->encodeByExtension(...$encoderOptions); | ||
} | ||
|
||
/** | ||
* Resolve format. | ||
* | ||
* @param ImageInterface $image The source image. | ||
* | ||
* @return string The resolved format. | ||
*/ | ||
public function getFormat(ImageInterface $image): string | ||
{ | ||
$fm = (string) $this->getParam('fm'); | ||
|
||
if ($fm && array_key_exists($fm, static::supportedFormats())) { | ||
return $fm; | ||
} | ||
|
||
/** @psalm-suppress RiskyTruthyFalsyComparison */ | ||
return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: 'jpg'; | ||
} | ||
|
||
/** | ||
* Get a list of supported image formats and MIME types. | ||
* | ||
* @return array<string,string> | ||
*/ | ||
public static function supportedFormats(): array | ||
{ | ||
return [ | ||
'avif' => 'image/avif', | ||
'gif' => 'image/gif', | ||
'jpg' => 'image/jpeg', | ||
'pjpg' => 'image/jpeg', | ||
'png' => 'image/png', | ||
'webp' => 'image/webp', | ||
'tiff' => 'image/tiff', | ||
'heic' => 'image/heic', | ||
]; | ||
} | ||
|
||
/** | ||
* Resolve quality. | ||
* | ||
* @return int The resolved quality. | ||
*/ | ||
public function getQuality(): int | ||
{ | ||
$default = 90; | ||
$q = $this->getParam('q'); | ||
|
||
if ( | ||
!is_numeric($q) | ||
|| $q < 0 | ||
|| $q > 100 | ||
) { | ||
return $default; | ||
} | ||
|
||
return (int) $q; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.