Commit c771df94 authored by simgui8's avatar simgui8 Committed by Bart Vanhoutte
Browse files

Issue #3144282 by simgui8, GaëlG, siddhant.bhosale, sonvir249, Niklan,...

Issue #3144282 by simgui8, GaëlG, siddhant.bhosale, sonvir249, Niklan, rik-dev: Drupal 9 compatibility
parent 51834a16
......@@ -2,7 +2,7 @@
Creates a WebP copy of image style derivatives to decrease loading times.
## Description
Whenever an image style derivative is created this module will also create
Whenever an image style derivative is created this module will also create
a WebP copy of the derivative to be served to supporting browsers.
## Requirements
......@@ -41,4 +41,4 @@ More detailed instructions here: admin/structure/types/manage/article/display
## Maintainers
* Bart Vanhoutte (Bart Vanhoutte) - https://www.drupal.org/user/1133754
* Alex Moreno - https://www.alexmoreno.net - https://www.drupal.org/u/alexmoreno
\ No newline at end of file
* Alex Moreno - https://www.alexmoreno.net - https://www.drupal.org/u/alexmoreno
......@@ -3,10 +3,10 @@
namespace Drupal\webp\Controller;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Image\Image;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\image\ImageStyleInterface;
use Drupal\system\FileDownloadController;
use Drupal\webp\Webp;
......@@ -50,13 +50,6 @@ class ImageStyleDownloadController extends FileDownloadController {
*/
protected $webp;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;
/**
* Constructs a ImageStyleDownloadController object.
*
......@@ -66,15 +59,15 @@ class ImageStyleDownloadController extends FileDownloadController {
* The image factory.
* @param \Drupal\webp\Webp $webp
* WebP driver.
* @param \Drupal\Core\File\FileSystem $file_system
* The file system service.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface|null $stream_wrapper_manager
* The stream wrapper manager.
*/
public function __construct(LockBackendInterface $lock, ImageFactory $image_factory, Webp $webp, FileSystem $file_system) {
public function __construct(LockBackendInterface $lock, ImageFactory $image_factory, Webp $webp, StreamWrapperManagerInterface $stream_wrapper_manager = NULL) {
parent::__construct($stream_wrapper_manager);
$this->lock = $lock;
$this->imageFactory = $image_factory;
$this->logger = $this->getLogger('image');
$this->webp = $webp;
$this->fileSystem = $file_system;
}
/**
......@@ -85,7 +78,7 @@ class ImageStyleDownloadController extends FileDownloadController {
$container->get('lock'),
$container->get('image.factory'),
$container->get('webp.webp'),
$container->get('file_system')
$container->get('stream_wrapper_manager')
);
}
......@@ -163,7 +156,7 @@ class ImageStyleDownloadController extends FileDownloadController {
// The $target variable for a derivative of a style has
// styles/<style_name>/... as structure, so we check if the $target variable
// starts with styles/.
$valid = !empty($image_style) && $this->fileSystem->validScheme($scheme);
$valid = !empty($image_style) && $this->streamWrapperManager->isValidScheme($scheme);
if (!$this->config('image.settings')->get('allow_insecure_derivatives') || strpos(ltrim($target, '\/'), 'styles/') === 0) {
$valid &= $request->query->get(IMAGE_DERIVATIVE_TOKEN) === $image_style->getPathToken($image_uri);
}
......
......@@ -35,8 +35,8 @@ class SettingsForm extends ConfigFormBase {
'#type' => 'number',
'#title' => $this->t('Image quality'),
'#description' => $this->t('Specify the image quality. This setting
will be in effect for all new image style derivatives. In order to apply
this setting to existing image style derivatives, flush image styles
will be in effect for all new image style derivatives. In order to apply
this setting to existing image style derivatives, flush image styles
through the interface, or by using Drush or Drupal Console.'),
'#default_value' => $config->get('quality'),
'#min' => 1,
......@@ -59,7 +59,7 @@ class SettingsForm extends ConfigFormBase {
parent::submitForm($form, $form_state);
$this->config('webp.settings')
->set('quality', (int)$form_state->getValue('quality'))
->set('quality', (int) $form_state->getValue('quality'))
->save();
}
......
......@@ -121,9 +121,9 @@ class Webp {
'@extension' => $pathInfo['extension'],
]);
imageSaveAlpha($sourceImage, true);
imageAlphaBlending($sourceImage, true);
imageSaveAlpha($sourceImage, true);
imagesavealpha($sourceImage, TRUE);
imagealphablending($sourceImage, TRUE);
imagesavealpha($sourceImage, TRUE);
if (@imagewebp($sourceImage, $destination, $quality)) {
// In some cases, libgd generates broken images. See
// https://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files
......@@ -158,9 +158,9 @@ class Webp {
public function deleteImageStyleDerivatives() {
// Remove the styles directory and generated images.
try {
$this->fileSystem->deleteRecursive(file_default_scheme() . '://styles');
$this->fileSystem->deleteRecursive(\Drupal::config('system.file')->get('default_scheme') . '://styles');
}
catch (FileException $e){
catch (FileException $e) {
$this->logger->error($e->getMessage());
$error = $this->t('Could not delete image style directory while uninstalling WebP. You have to delete it manually.');
$this->logger->error($error);
......@@ -170,7 +170,7 @@ class Webp {
/**
* Receives the srcset string of an image and returns the webp equivalent.
*
* @param $srcset
* @param string $srcset
* Srcset to convert to .webp version
*
* @return string
......
<?php
namespace Drupal\Tests\phpunit_example\Unit;
namespace Drupal\Tests\webp\Unit;
use Drupal\Tests\UnitTestCase;
......@@ -11,8 +11,6 @@ use Drupal\Tests\UnitTestCase;
*/
class WebpTest extends UnitTestCase {
protected $webp;
/**
* Before a test method is run, setUp() is invoked.
*
......
name: WebP
type: module
description: Generates WebP copies of image style derivatives.
core: 8.x
core_version_requirement: ^8 || ^9
package: Other
configure: webp.settings_form
......
......@@ -16,7 +16,7 @@ function webp_requirements($phase) {
// installation or show an error on the status page.
$gd_info = gd_info();
// Let's convert to capital case, so we ensure we always cover both.
$supported_formats = array();
$supported_formats = [];
if (extension_loaded('imagick')) {
$supported_formats = array_map('strtoupper', Imagick::queryFormats());
}
......
......@@ -4,4 +4,3 @@ webp.settings_form:
description: 'Configure WebP module.'
parent: system.admin_config_media
weight: 99
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment