Skip to content
Snippets Groups Projects
Commit 62d3b88f authored by Sean B's avatar Sean B
Browse files

Issue #3300691: Add support for webp module

parent 3aafc2c2
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
namespace Drupal\easy_responsive_images;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
......@@ -187,17 +188,26 @@ class EasyResponsiveImagesManager implements EasyResponsiveImagesManagerInterfac
* Returns a WebP Derivative as a string.
*/
protected function getWebpDerivatives(string $url, string $uri): string {
// If the imageapi_optimize_webp module is installed and the browser
// supports webp, return the webp version of the image.
if ($this->moduleHandler->moduleExists('imageapi_optimize_webp')) {
if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== FALSE) {
$path_parts = pathinfo($uri);
$original_extension = '.' . $path_parts['extension'];
$pos = strrpos($url, $original_extension);
if ($pos !== FALSE) {
$url = substr_replace($url, $original_extension . '.webp', $pos, strlen($original_extension));
}
}
// Check if a module creating WebP derivatives is available.
if (!$this->moduleHandler->moduleExists('imageapi_optimize_webp') && !$this->moduleHandler->moduleExists('webp')) {
return $url;
}
// Check if the browser supports WebP images.
if (!isset($_SERVER['HTTP_ACCEPT']) || strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') === FALSE) {
return $url;
}
// Check if we have a local image with an extension.
$path_parts = pathinfo($uri);
if (UrlHelper::isExternal($uri) || !isset($path_parts['extension'])) {
return $url;
}
$original_extension = '.' . $path_parts['extension'];
$pos = strrpos($url, $original_extension);
if ($pos !== FALSE) {
$url = substr_replace($url, $original_extension . '.webp', $pos, strlen($original_extension));
}
return $url;
......
......@@ -184,6 +184,10 @@ class EasyResponsiveImagesFormatter extends ImageFormatter {
}
}
// Cache the output using the accept header since we use it to detect
// support for WebP images.
$elements['#cache']['contexts'] = ['headers:accept'];
return $elements;
}
......
......@@ -68,7 +68,7 @@ class ImageUrl extends AbstractExtension {
// If the imageapi_optimize_webp module is installed and the browser
// supports webp, return the webp version of the image.
if (\Drupal::moduleHandler()->moduleExists('imageapi_optimize_webp')) {
if (\Drupal::moduleHandler()->moduleExists('imageapi_optimize_webp') || \Drupal::moduleHandler()->moduleExists('webp')) {
if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== FALSE) {
$path_parts = pathinfo($uri);
$original_extension = '.' . $path_parts['extension'];
......@@ -79,8 +79,11 @@ class ImageUrl extends AbstractExtension {
}
}
// Cache the output using the accept header since we use it to detect
// support for WebP images.
$build['#markup'] = $file_url;
$build['#cache']['contexts'] = ['headers:accept'];
return $build;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment