imageapi_optimize_webp_responsive.module in ImageAPI Optimize WebP 2.x
File
modules/imageapi_optimize_webp_responsive/imageapi_optimize_webp_responsive.module
View source
<?php
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\image\Entity\ImageStyle;
function imageapi_optimize_webp_responsive_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.imageapi_optimize_webp_responsive':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Implements WebP derivatives in core Responsive Images') . '</p>';
return $output;
default:
}
}
function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variables) {
if (!empty($variables['sources'])) {
$webp_sources = [];
$entity = ResponsiveImageStyle::load($variables['responsive_image_style_id']);
$image_styles = ImageStyle::loadMultiple($entity
->getImageStyleIds());
$pipeline_webp_map = [];
$image_style_map = [];
foreach ($image_styles as $image_style) {
if ($image_style
->hasPipeline()) {
if (!isset($pipeline_webp_map[$image_style
->getPipeline()])) {
$pipeline_webp_map[$image_style
->getPipeline()] = FALSE;
foreach ($image_style
->getPipelineEntity()
->getProcessors() as $processor) {
if ($processor
->getPluginId() == "imageapi_optimize_webp") {
$pipeline_webp_map[$image_style
->getPipeline()] = TRUE;
break;
}
}
}
if ($pipeline_webp_map[$image_style
->getPipeline()]) {
$image_uri = $image_style
->buildUri($variables['uri']);
$image_url = file_create_url($image_uri);
$image_relative_path = file_url_transform_relative($image_url);
$image_relative_path_file = explode('?', $image_relative_path)[0];
$webp_relative_path = str_replace($image_relative_path_file, $image_relative_path_file . '.webp', $image_relative_path);
$image_style_map[$image_relative_path] = $webp_relative_path;
}
}
}
if ($image_style_map) {
foreach ($variables['sources'] as $source) {
$offset_key = NULL;
if ($source
->offsetExists('srcset')) {
$offset_key = 'srcset';
}
elseif ($source
->offsetExists('data-srcset')) {
$offset_key = 'data-srcset';
}
if ($offset_key) {
$source_srcset = $source
->offsetGet($offset_key)
->value();
$cloned_srcset = strtr($source_srcset, $image_style_map);
if ($source_srcset != $cloned_srcset) {
$source_webp = clone $source;
$source_webp
->offsetSet($offset_key, $cloned_srcset);
$source_webp
->offsetSet('type', 'image/webp');
$webp_sources[] = $source_webp;
}
}
}
if (!empty($webp_sources)) {
$variables['sources'] = array_merge($webp_sources, $variables['sources']);
$variables['output_image_tag'] = FALSE;
}
}
}
}