You are here

protected function AmpPrepareMetadataJson::getUriFromImageTokenString in Accelerated Mobile Pages (AMP) 8

Get an image URI from a string containing an image token.

Parameters

string $image_token_string: The string containing an image token.

Return value

string $image_uri The URI of the image.

1 call to AmpPrepareMetadataJson::getUriFromImageTokenString()
AmpPrepareMetadataJson::prepareContentImage in src/Utility/AmpPrepareMetadataJson.php
Prepare content image information.

File

src/Utility/AmpPrepareMetadataJson.php, line 287

Class

AmpPrepareMetadataJson
Class AmpPrepareMetadataJson

Namespace

Drupal\amp\Utility

Code

protected function getUriFromImageTokenString($image_token_string) {
  $image_url = $this->token
    ->replace($image_token_string, [
    'node' => $this->node,
  ]);

  // Provide backup parsing of image element if token does not output a URL.
  if (strip_tags($image_url) != $image_url) {

    // Force path to be absolute.
    if (strpos($image_url, 'img src="/') !== FALSE) {
      global $base_root;
      $image_url = str_replace('img src="/', 'img src="' . $base_root . '/', $image_url);
    }
    $matches = [];
    preg_match('/src="([^"]*)"/', $image_url, $matches);
    if (!empty($matches[1])) {
      $image_url = $matches[1];
    }
  }
  $image_url = PlainTextOutput::renderFromHtml($image_url);
  $public_stream_base_url = PublicStream::baseUrl();
  $image_uri = '';
  if (substr($image_url, 0, strlen($public_stream_base_url)) == $public_stream_base_url) {
    $image_uri = file_build_uri(substr($image_url, strlen($public_stream_base_url)));
  }
  return $image_uri;
}