You are here

protected function FileEntity::fetchImageDimensions in File Entity (fieldable files) 8.2

Fetch the dimensions of an image and store them in the file metadata array.

1 call to FileEntity::fetchImageDimensions()
FileEntity::preSave in src/Entity/FileEntity.php
Acts on an entity before the presave hook is invoked.

File

src/Entity/FileEntity.php, line 165

Class

FileEntity
Replace for the core file entity class.

Namespace

Drupal\file_entity\Entity

Code

protected function fetchImageDimensions() {

  // Prevent PHP notices when trying to read empty files.
  // @see http://drupal.org/node/681042
  if (!$this
    ->getSize()) {
    return;
  }

  // Do not bother proceeding if this file does not have an image mime type.
  if ($this
    ->getMimeTypeType() != 'image') {
    return;
  }

  // We have a non-empty image file.
  $image = \Drupal::service('image.factory')
    ->get($this
    ->getFileUri());
  if ($image) {
    $this
      ->setMetadata('width', $image
      ->getWidth());
    $this
      ->setMetadata('height', $image
      ->getHeight());
  }
}