Skip to content
Snippets Groups Projects

Resolve #3188912 "Incompatibility with webp"

8 files
+ 352
32
Compare changes
  • Side-by-side
  • Inline
Files
8
 
<?php
 
 
namespace Drupal\avif\PathProcessor;
 
 
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
 
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
 
use Symfony\Component\HttpFoundation\Request;
 
 
/**
 
* Defines a path processor to rewrite image styles URLs for avif.
 
*
 
* @see \Drupal\image\PathProcessor\PathProcessorImageStyles
 
*/
 
class PathProcessorAvifImageStyles implements InboundPathProcessorInterface {
 
 
/**
 
* The stream wrapper manager service.
 
*
 
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
 
*/
 
protected $streamWrapperManager;
 
 
/**
 
* Constructs a new PathProcessorAvifImageStyles object.
 
*
 
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
 
* The stream wrapper manager service.
 
*/
 
public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) {
 
$this->streamWrapperManager = $stream_wrapper_manager;
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public function processInbound($path, Request $request) {
 
$directory_path = $this->streamWrapperManager->getViaScheme('public')->getDirectoryPath();
 
if (strpos($path, '/avif/' . $directory_path . '/styles/') === 0) {
 
$path_prefix = '/avif/' . $directory_path . '/styles/';
 
}
 
else {
 
return $path;
 
}
 
 
// Strip out path prefix.
 
$rest = preg_replace('|^' . preg_quote($path_prefix, '|') . '|', '', $path);
 
 
// Get the image style, scheme and path.
 
if (substr_count($rest, '/') >= 2) {
 
[$image_style, $scheme, $file] = explode('/', $rest, 3);
 
 
// Set the file as query parameter.
 
$request->query->set('file', $file);
 
 
return $path_prefix . $image_style . '/' . $scheme;
 
}
 
else {
 
return $path;
 
}
 
}
 
 
}
Loading