You are here

public static function BlazyMedia::imageItem in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyMedia.php \Drupal\blazy\BlazyMedia::imageItem()

Gets the faked image item out of file entity, or ER, if applicable.

This should only be called for type video as file image has all the needed info to get the image from.

Parameters

object $file: The expected file entity, or ER, to get image item from.

Return value

object The image item or FALSE.

2 calls to BlazyMedia::imageItem()
BlazyFormatter::extractFirstItem in src/BlazyFormatter.php
BlazyVideoTrait::getImageItem in src/Plugin/Field/FieldFormatter/BlazyVideoTrait.php
Gets the faked image item out of file entity, or ER, if applicable.

File

src/BlazyMedia.php, line 96

Class

BlazyMedia
Provides extra utilities to work with Media.

Namespace

Drupal\blazy

Code

public static function imageItem($file) {

  // Prevents edge case EntityMalformedException: Missing bundle property.
  if (!isset($file->uri)) {
    return FALSE;
  }
  try {
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);

    // No need for checking MediaReadOnlyStreamWrapper.
    if (!is_object($wrapper)) {
      throw new \Exception('Unable to find matching wrapper');
    }

    // If a video, uri points to a video scheme, not local thumbnail.
    $uri = $file->type == 'image' ? $file->uri : $wrapper
      ->getLocalThumbnailPath();
  } catch (\Exception $e) {

    // Ignore.
  }
  if (!isset($uri)) {
    return FALSE;
  }
  list($type) = explode('/', file_get_mimetype($uri), 2);
  if ($type == 'image' && ($image = image_get_info($uri))) {
    $item = new \stdClass();
    $item->target_id = $file->fid;
    $item->width = $image['width'];
    $item->height = $image['height'];
    $item->alt = $file->filename;
    $item->uri = $uri;
    return $item;
  }
  return FALSE;
}