You are here

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

Gets image information.

Parameters

string $image_uri: The URI of the image. URI should begin with 'public://'.

string $image_style_id: The optional ID of the image style.

Return value

array The array containing information about the image. Return an empty array if there is a problem getting information about the image. Otherwise the image inforation array includes the following keys:

  • url
  • width
  • px
2 calls to AmpPrepareMetadataJson::getImageInformation()
AmpPrepareMetadataJson::prepareContentImage in src/Utility/AmpPrepareMetadataJson.php
Prepare content image information.
AmpPrepareMetadataJson::preparePublisher in src/Utility/AmpPrepareMetadataJson.php
Prepare publisher information.

File

src/Utility/AmpPrepareMetadataJson.php, line 331

Class

AmpPrepareMetadataJson
Class AmpPrepareMetadataJson

Namespace

Drupal\amp\Utility

Code

protected function getImageInformation($image_uri, $image_style_id = '') {
  $image_url = '';
  $image_width = '';
  $image_height = '';

  /** @var ImageInterface $image */
  $image = \Drupal::service('image.factory')
    ->get($image_uri);
  if ($image
    ->isValid()) {
    $image_url = file_create_url($image_uri);
    $image_width = $image
      ->getWidth();
    $image_height = $image
      ->getHeight();
    if (!empty($image_style_id)) {

      /** @var ImageStyleInterface $image_style */
      $image_style = ImageStyle::load($image_style_id);
      $image_dimensions = [
        'width' => $image_width,
        'height' => $image_height,
      ];
      $image_style
        ->transformDimensions($image_dimensions, $image_uri);
      $image_url = $image_style
        ->buildUrl($image_uri);
      $image_width = $image_dimensions['width'];
      $image_height = $image_dimensions['height'];
    }
  }
  if (!empty($image_url) && !empty($image_width) & !empty($image_height)) {
    return [
      'url' => $image_url,
      'width' => $image_width,
      'height' => $image_height,
    ];
  }
  else {
    return [];
  }
}