Skip to content
Snippets Groups Projects
Commit d94969cb authored by Dieter Holvoet's avatar Dieter Holvoet
Browse files

Issue #3391520 by dtfabio, DieterHolvoet, weseze: Provide drimage support for core Webp

parent 772e8af6
Branches 1.0.x
Tags 1.0.1
1 merge request!8Add the image style convert to webp effect to the controller, rearrange the...
......@@ -4,6 +4,7 @@ upscale: 320
downscale: 3840
multiplier: 1
lazy_offset: 100
core_webp: false
imageapi_optimize_webp: false
automated_crop: ''
fallback_style: ''
......
......@@ -20,6 +20,9 @@ drimage.settings:
lazy_offset:
type: integer
label: 'Lazyloader offeset'
core_webp:
type: boolean
label: 'Support for webp through Core'
imageapi_optimize_webp:
type: boolean
label: 'Support for webp through the imageapi_optimize_webp module'
......
......@@ -66,6 +66,15 @@ function drimage_update_8010(): void {
$settings->save();
}
/**
* Set false as default value for "core_webp".
*/
function drimage_update_8011(&$sandbox) {
\Drupal::configFactory()->getEditable('drimage.settings')
->set('core_webp', FALSE)
->save();
}
/**
* Set default value for "cache_max_age".
*/
......
......@@ -32,6 +32,7 @@ function drimage_theme() {
'item' => NULL,
'item_attributes' => NULL,
'image_style' => NULL,
'core_webp' => NULL,
'imageapi_optimize_webp' => NULL,
'url' => NULL,
'alt' => NULL,
......
......@@ -217,7 +217,7 @@
var imgUrl = data.subdir + '/drimage/' + size[0] + '/' + size[1] + '/' + data.fid + '/' + iwc + data.original_source;
if (data.image_handling === 'background') {
if (data.imageapi_optimize_web && Drupal.drimage.webp === true) {
if ((data.core_webp || data.imageapi_optimize_webp) && Drupal.drimage.webp === true) {
imgUrl = imgUrl + '.webp';
}
img.onload = function() {
......@@ -227,7 +227,7 @@
img.src = imgUrl;
}
else {
if (data.imageapi_optimize_webp) {
if (data.core_webp || data.imageapi_optimize_webp) {
var source = el.querySelector('source[data-format="webp"]');
if (source) {
source.setAttribute('srcset', imgUrl + '.webp');
......
......@@ -203,22 +203,41 @@ class DrImageController extends ImageStyleDownloadController {
// already created.
try {
$style = ImageStyle::create(['name' => $name, 'label' => $label]);
$drimage_config = $this->config('drimage.settings');
// If webp is used, convert the image before doing anything else.
if ($drimage_config->get('core_webp') === TRUE) {
$convert_webp_effect_config = [
'uuid' => NULL,
'id' => 'image_convert',
'weight' => 0,
'data' => [
'extension' => 'webp',
],
];
$convert_webp_effect = \Drupal::service('plugin.manager.image.effect')
->createInstance(
$convert_webp_effect_config['id'],
$convert_webp_effect_config
);
$style->addImageEffect($convert_webp_effect->getConfiguration());
}
// When using image_widget_crop insert that here first.
// When using image_widget_crop insert that here first after converting.
if ($iwc_id) {
if ($this->moduleHandler()->moduleExists('image_widget_crop')) {
if (\Drupal::entityTypeManager()->getStorage('crop_type')->load($iwc_id)) {
$iwc_configuration = [
'uuid' => NULL,
'id' => 'crop_crop',
'weight' => 0,
'weight' => 1,
'data' => [
'crop_type' => $iwc_id,
],
];
// Add support for automated_crop module.
$drimage_config = $this->config('drimage.settings');
if (!empty($drimage_config->get('automated_crop'))) {
$iwc_configuration['data']['automatic_crop_provider'] = $drimage_config->get('automated_crop');
}
......@@ -231,7 +250,7 @@ class DrImageController extends ImageStyleDownloadController {
$configuration = [
'uuid' => NULL,
'weight' => 1,
'weight' => 2,
'data' => [
'upscale' => FALSE,
'width' => NULL,
......@@ -394,7 +413,7 @@ class DrImageController extends ImageStyleDownloadController {
try {
$response = $this->deliver($request, $scheme, $image_style);
$drimage_config = $this->config('drimage.settings');
if ($format === 'webp' && $drimage_config->get('imageapi_optimize_webp')) {
if ($format === 'webp' && ($drimage_config->get('core_webp') || $drimage_config->get('imageapi_optimize_webp'))) {
$webp_image_derivative_uri = $image_style->buildUri($image_uri . '.webp');
if (file_exists($webp_image_derivative_uri)) {
$image = $this->imageFactory->get($webp_image_derivative_uri);
......
......@@ -2,6 +2,7 @@
namespace Drupal\drimage\Form;
use Drupal\Core\ImageToolkit\ImageToolkitManager;
use Drupal\crop\Events\AutomaticCropProviders;
use Drupal\crop\Events\Events;
use Drupal\Core\Config\ConfigFactoryInterface;
......@@ -33,6 +34,13 @@ class DrimageSettingsForm extends ConfigFormBase {
*/
protected $moduleHandler;
/**
* The image toolkit manager.
*
* @var \Drupal\Core\ImageToolkit\ImageToolkitManager
*/
protected $imageToolkitManager;
/**
* Constructs a DrimageSettingsForm object.
*
......@@ -42,12 +50,19 @@ class DrimageSettingsForm extends ConfigFormBase {
* The date formatter service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\ImageToolkit\ImageToolkitManager $image_toolkit_manager
* The image toolkit manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
public function __construct(
ConfigFactoryInterface $config_factory,
DateFormatterInterface $date_formatter,
ModuleHandlerInterface $module_handler,
ImageToolkitManager $image_toolkit_manager
) {
parent::__construct($config_factory);
$this->dateFormatter = $date_formatter;
$this->moduleHandler = $module_handler;
$this->imageToolkitManager = $image_toolkit_manager;
}
/**
......@@ -57,7 +72,8 @@ class DrimageSettingsForm extends ConfigFormBase {
return new static(
$container->get('config.factory'),
$container->get('date.formatter'),
$container->get('module_handler')
$container->get('module_handler'),
$container->get('image.toolkit.manager')
);
}
......@@ -140,7 +156,10 @@ class DrimageSettingsForm extends ConfigFormBase {
'#description' => $this->t('Will produce higher quality images on screens that have more physical pixels then logical pixels.'),
];
$image_toolkit = $this->imageToolkitManager->getDefaultToolkit();
$allow_core_webp = FALSE;
$imageapi_optimize_webp_check = FALSE;
$core_webp_option_enabled = $this->config('drimage.settings')->get('core_webp');
$module_handler = \Drupal::service('module_handler');
if ($module_handler->moduleExists('imageapi_optimize_webp')) {
if ($imageapi_optimize_settings = \Drupal::config('imageapi_optimize.settings')) {
......@@ -148,13 +167,39 @@ class DrimageSettingsForm extends ConfigFormBase {
$imageapi_optimize_webp_check = TRUE;
}
}
if ($core_webp_option_enabled === TRUE) {
// Disable core WebP when the image optimize WebP module gets enabled.
$this->config('drimage.settings')->set('core_webp', FALSE)
->save();
$core_webp_option_enabled = FALSE;
}
} elseif (in_array('webp', $image_toolkit->getSupportedExtensions())) {
$allow_core_webp = TRUE;
} elseif ($core_webp_option_enabled === TRUE) {
// Disable core WebP if Webp support is removed from the image toolkit.
$this->config('drimage.settings')->set('core_webp', FALSE)
->save();
$core_webp_option_enabled = FALSE;
}
$form['core_webp'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable core webp support'),
'#default_value' => $core_webp_option_enabled,
'#description' => $this->t('Core webp support is only available when the image optimize webp module is disabled and current image toolkit supports WEBP images.'),
'#disabled' => !$allow_core_webp,
];
if ($image_toolkit->getPluginId() === 'gd') {
$form['core_webp']['#description'] .= ' ' . $this->t('Visit the <a href="@statusReportUrl">status report</a> to check if your current GD version supports webp.', ['@statusReportUrl' => '/admin/reports/status/php#module_gd']);
}
$form['imageapi_optimize_webp'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable webp support'),
'#title' => $this->t('Enable ImageAPI Optimize WebP support'),
'#default_value' => $this->config('drimage.settings')->get('imageapi_optimize_webp'),
'#description' => $this->t('This options is only available if <a href="https://www.drupal.org/project/imageapi_optimize_webp">imageapi_optimize_webp</a> module is installed. and a "Sitewide default pipeline" is set up.'),
'#disabled' => !$imageapi_optimize_webp_check,
'#description' => $this->t('This options is only available if <a href="https://www.drupal.org/project/imageapi_optimize_webp">ImageAPI Optimize WebP</a> module is installed, the core Webp option is disabled and a "Sitewide default pipeline" is set up.'),
'#disabled' => (!$imageapi_optimize_webp_check || $core_webp_option_enabled === TRUE),
];
$automated_crop_check = FALSE;
......@@ -250,6 +295,7 @@ class DrimageSettingsForm extends ConfigFormBase {
->set('upscale', $form_state->getValue('upscale'))
->set('downscale', $form_state->getValue('downscale'))
->set('multiplier', $form_state->getValue('multiplier'))
->set('core_webp', $form_state->getValue('core_webp'))
->set('imageapi_optimize_webp', $form_state->getValue('imageapi_optimize_webp'))
->set('automated_crop', $form_state->getValue('automated_crop'))
->set('lazy_offset', $form_state->getValue('lazy_offset'))
......
......@@ -263,6 +263,7 @@ class DrImageFormatter extends ImageFormatter {
'upscale' => $config->get('upscale'),
'downscale' => $config->get('downscale'),
'multiplier' => $config->get('multiplier'),
'core_webp' => $config->get('core_webp'),
'imageapi_optimize_webp' => $config->get('imageapi_optimize_webp'),
'lazy_offset' => $config->get('lazy_offset'),
'subdir' => $url,
......@@ -273,6 +274,7 @@ class DrImageFormatter extends ImageFormatter {
// implementing lightbox-style plugins that show the original image.
$elements[$delta]['#width'] = $element['#item']->getValue()['width'];
$elements[$delta]['#height'] = $element['#item']->getValue()['height'];
$elements[$delta]['#core_webp'] = $config->get('core_webp');
$elements[$delta]['#imageapi_optimize_webp'] = $config->get('imageapi_optimize_webp');
$elements[$delta]['#alt'] = $element['#item']->getValue()['alt'];
$elements[$delta]['#data']['original_width'] = $element['#item']->getValue()['width'];
......
......@@ -55,6 +55,7 @@ class DrImageUriFormatter extends DrImageFormatter {
'upscale' => $config->get('upscale'),
'downscale' => $config->get('downscale'),
'multiplier' => $config->get('multiplier'),
'core_webp' => $config->get('core_webp'),
'imageapi_optimize_webp' => $config->get('imageapi_optimize_webp'),
'lazy_offset' => $config->get('lazy_offset'),
'subdir' => $url,
......@@ -65,6 +66,7 @@ class DrImageUriFormatter extends DrImageFormatter {
// implementing lightbox-style plugins that show the original image.
$elements[$delta]['#width'] = $file->getMetaData('width');
$elements[$delta]['#height'] = $file->getMetaData('height');
$elements[$delta]['#core_webp'] = $config->get('core_webp');
$elements[$delta]['#imageapi_optimize_webp'] = $config->get('imageapi_optimize_webp');
$elements[$delta]['#alt'] = $file->getMetaData('alt');
$elements[$delta]['#data']['original_width'] = $file->getMetaData('width');
......
......@@ -26,7 +26,7 @@
<picture>
{% endif %}
{% if imageapi_optimize_webp %}
{% if core_webp or imageapi_optimize_webp %}
<source data-format="webp" srcset="{{ dummy_image }}" type="image/webp">
{% endif %}
<img src="{{ dummy_image }}" width="{{ width }}" height="{{ height }}" alt="{{ alt }}" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment