function PathProcessorImageStyles::processInbound

Same name in other branches
  1. 9 core/modules/image/src/PathProcessor/PathProcessorImageStyles.php \Drupal\image\PathProcessor\PathProcessorImageStyles::processInbound()
  2. 8.9.x core/modules/image/src/PathProcessor/PathProcessorImageStyles.php \Drupal\image\PathProcessor\PathProcessorImageStyles::processInbound()
  3. 10 core/modules/image/src/PathProcessor/PathProcessorImageStyles.php \Drupal\image\PathProcessor\PathProcessorImageStyles::processInbound()

Overrides InboundPathProcessorInterface::processInbound

File

core/modules/image/src/PathProcessor/PathProcessorImageStyles.php, line 46

Class

PathProcessorImageStyles
Defines a path processor to rewrite image styles URLs.

Namespace

Drupal\image\PathProcessor

Code

public function processInbound($path, Request $request) {
    $directory_path = $this->streamWrapperManager
        ->getViaScheme('public')
        ->getDirectoryPath();
    if (str_starts_with($path, '/' . $directory_path . '/styles/')) {
        $path_prefix = '/' . $directory_path . '/styles/';
    }
    elseif (str_contains($path, '/system/files/styles/')) {
        $path_prefix = '/system/files/styles/';
        $path = substr($path, strpos($path, $path_prefix), strlen($path));
    }
    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;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.