You are here

public function ImageStyle::buildUrl in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/image/src/Entity/ImageStyle.php \Drupal\image\Entity\ImageStyle::buildUrl()
  2. 9 core/modules/image/src/Entity/ImageStyle.php \Drupal\image\Entity\ImageStyle::buildUrl()

Returns the URL of this image derivative for an original image path or URI.

Parameters

string $path: The path or URI to the original image.

mixed $clean_urls: (optional) Whether clean URLs are in use.

Return value

string The absolute URL where a style image can be downloaded, suitable for use in an <img> tag. Requesting the URL will cause the image to be created.

Overrides ImageStyleInterface::buildUrl

See also

\Drupal\image\Controller\ImageStyleDownloadController::deliver()

\Drupal\Core\File\FileUrlGeneratorInterface::transformRelative()

File

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

Class

ImageStyle
Defines an image style configuration entity.

Namespace

Drupal\image\Entity

Code

public function buildUrl($path, $clean_urls = NULL) {
  $uri = $this
    ->buildUri($path);

  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');

  // 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 $e) {
    }
  }

  // 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 .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
  }
  return $file_url;
}