You are here

public function Image::getField in Media entity image 8

Gets a media-related field/value.

Parameters

MediaInterface $media: Media object.

string $name: Name of field to fetch.

Return value

mixed Field value or FALSE if data unavailable.

Overrides MediaTypeInterface::getField

File

src/Plugin/MediaEntity/Type/Image.php, line 104

Class

Image
Provides media type plugin for Image.

Namespace

Drupal\media_entity_image\Plugin\MediaEntity\Type

Code

public function getField(MediaInterface $media, $name) {
  $source_field = $this->configuration['source_field'];

  // Get the file, image and exif data.

  /** @var \Drupal\file\FileInterface $file */
  $file = $media->{$source_field}->entity;
  $image = $this->imageFactory
    ->get($file
    ->getFileUri());
  $uri = $file
    ->getFileUri();

  // Return the field.
  switch ($name) {
    case 'mime':
      return !$file->filemime
        ->isEmpty() ? $file
        ->getMimeType() : FALSE;
    case 'width':
      $width = $image
        ->getWidth();
      return $width ? $width : FALSE;
    case 'height':
      $height = $image
        ->getHeight();
      return $height ? $height : FALSE;
    case 'size':
      $size = $file
        ->getSize();
      return $size ? $size : FALSE;
  }
  if (!empty($this->configuration['gather_exif']) && function_exists('exif_read_data')) {
    switch ($name) {
      case 'model':
        return $this
          ->getExifField($uri, 'Model');
      case 'created':
        $date = new DrupalDateTime($this
          ->getExifField($uri, 'DateTimeOriginal'));
        return $date
          ->getTimestamp();
      case 'iso':
        return $this
          ->getExifField($uri, 'ISOSpeedRatings');
      case 'exposure':
        return $this
          ->getExifField($uri, 'ExposureTime');
      case 'aperture':
        return $this
          ->getExifField($uri, 'FNumber');
      case 'focal_length':
        return $this
          ->getExifField($uri, 'FocalLength');
    }
  }
  return FALSE;
}