function ImageStyle::buildUrl

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

Overrides ImageStyleInterface::buildUrl

File

core/modules/image/src/Entity/ImageStyle.php, line 209

Class

ImageStyle
Defines an image style configuration entity.

Namespace

Drupal\image\Entity

Code

public function buildUrl($path, $clean_urls = NULL) {
    
    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    $uri = $stream_wrapper_manager->normalizeUri($this->buildUri($path));
    // The token query is added even if the
    // 'image.settings:allow_insecure_derivatives' configuration is TRUE, so
    // that the emitted links remain valid if it is changed back to the default
    // FALSE. However, sites which need to prevent the token query from being
    // emitted at all can additionally set the
    // 'image.settings:suppress_itok_output' configuration to TRUE to achieve
    // that (if both are set, the security token will neither be emitted in the
    // image derivative URL nor checked for in
    // \Drupal\image\ImageStyleInterface::deliver()).
    $token_query = [];
    if (!\Drupal::config('image.settings')->get('suppress_itok_output')) {
        // The passed $path variable can be either a relative path or a full URI.
        if (!$stream_wrapper_manager::getScheme($path)) {
            $path = \Drupal::config('system.file')->get('default_scheme') . '://' . $path;
        }
        $original_uri = $stream_wrapper_manager->normalizeUri($path);
        $token_query = [
            IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri),
        ];
    }
    if ($clean_urls === NULL) {
        // Assume clean URLs unless the request tells us otherwise.
        $clean_urls = TRUE;
        try {
            $request = \Drupal::request();
            $clean_urls = RequestHelper::isCleanUrl($request);
        } catch (ServiceNotFoundException) {
        }
    }
    // If not using clean URLs, the image derivative callback is only available
    // with the script path. If the file does not exist, use Url::fromUri() to
    // ensure that it is included. Once the file exists it's fine to fall back
    // to the actual file path, this avoids bootstrapping PHP once the files are
    // built.
    if ($clean_urls === FALSE && $stream_wrapper_manager::getScheme($uri) == 'public' && !file_exists($uri)) {
        $directory_path = $stream_wrapper_manager->getViaUri($uri)
            ->getDirectoryPath();
        return Url::fromUri('base:' . $directory_path . '/' . $stream_wrapper_manager::getTarget($uri), [
            'absolute' => TRUE,
            'query' => $token_query,
        ])->toString();
    }
    
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $file_url = $file_url_generator->generateAbsoluteString($uri);
    // Append the query string with the token, if necessary.
    if ($token_query) {
        $file_url .= (str_contains($file_url, '?') ? '&' : '?') . UrlHelper::buildQuery($token_query);
    }
    return $file_url;
}

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