You are here

public function BackgroundImage::getImageUrl in Background Image 8

Same name and namespace in other branches
  1. 2.x src/Entity/BackgroundImage.php \Drupal\background_image\Entity\BackgroundImage::getImageUrl()
  2. 2.0.x src/Entity/BackgroundImage.php \Drupal\background_image\Entity\BackgroundImage::getImageUrl()

Retrieves the URL for the image.

Parameters

string $styleName: Optional. An image style name to use.

array $options: Optional. An array of options used to construct the URL object.

bool $parents: Flag indicating whether to use parent image if this image is not set.

Return value

string The image style URL.

Overrides BackgroundImageInterface::getImageUrl

File

src/Entity/BackgroundImage.php, line 362

Class

BackgroundImage
Defines the Background Image entity.

Namespace

Drupal\background_image\Entity

Code

public function getImageUrl($styleName = NULL, array $options = [], $parents = TRUE) {
  if ($styleName === '_empty image_') {

    // The smallest data URI for a 1px square transparent GIF image.
    // http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
    return 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
  }
  $fileUri = $this
    ->getImageFile($parents)
    ->getFileUri();
  if ($styleName && ($imageStyle = ImageStyle::load($styleName)) && $imageStyle instanceof ImageStyle) {
    $uri = $imageStyle
      ->buildUrl($fileUri);
  }
  else {
    $uri = file_create_url($fileUri);
  }
  $options += [
    'absolute' => TRUE,
  ];
  return Url::fromUri($uri, $options)
    ->toString();
}