3 namespace Drupal\image\PathProcessor;
5 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
7 use Symfony\Component\HttpFoundation\Request;
10 * Defines a path processor to rewrite image styles URLs.
12 * As the route system does not allow arbitrary amount of parameters convert
13 * the file path to a query parameter on the request.
15 * This processor handles two different cases:
16 * - public image styles: In order to allow the webserver to serve these files
17 * directly, the route is registered under the same path as the image style so
18 * it took over the first generation. Therefore the path processor converts
19 * the file path to a query parameter.
20 * - private image styles: In contrast to public image styles, private
21 * derivatives are already using system/files/styles. Similar to public image
22 * styles, it also converts the file path to a query parameter.
24 class PathProcessorImageStyles implements InboundPathProcessorInterface {
27 * The stream wrapper manager service.
29 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
31 protected $streamWrapperManager;
34 * Constructs a new PathProcessorImageStyles object.
36 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
37 * The stream wrapper manager service.
39 public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) {
40 $this->streamWrapperManager = $stream_wrapper_manager;
46 public function processInbound($path, Request $request) {
47 $directory_path = $this->streamWrapperManager->getViaScheme('public')->getDirectoryPath();
48 if (strpos($path, '/' . $directory_path . '/styles/') === 0) {
49 $path_prefix = '/' . $directory_path . '/styles/';
51 elseif (strpos($path, '/system/files/styles/') === 0) {
52 $path_prefix = '/system/files/styles/';
58 // Strip out path prefix.
59 $rest = preg_replace('|^' . preg_quote($path_prefix, '|') . '|', '', $path);
61 // Get the image style, scheme and path.
62 if (substr_count($rest, '/') >= 2) {
63 list($image_style, $scheme, $file) = explode('/', $rest, 3);
65 // Set the file as query parameter.
66 $request->query->set('file', $file);
68 return $path_prefix . $image_style . '/' . $scheme;