You are here

public function PhotosImage::load in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 src/PhotosImage.php \Drupal\photos\PhotosImage::load()

Load image file and album data.

1 call to PhotosImage::load()
PhotosImage::view in src/PhotosImage.php
Return render array to view image.

File

src/PhotosImage.php, line 37

Class

PhotosImage
Create images object.

Namespace

Drupal\photos

Code

public function load() {
  $fid = $this->fid;

  // Query image data.
  // @todo check access. Is ->addTag('node_access') needed here? If so, rewrite query.
  //   - I think access is already checked before we get here.
  $db = \Drupal::database();
  $image = $db
    ->query('SELECT f.fid, f.uri, f.filemime, f.created, f.filename, n.title as node_title, a.data, u.uid, u.name, p.*
      FROM {file_managed} f
      INNER JOIN {photos_image} p ON f.fid = p.fid
      INNER JOIN {node_field_data} n ON p.pid = n.nid
      INNER JOIN {photos_album} a ON a.pid = n.nid
      INNER JOIN {users_field_data} u ON f.uid = u.uid
      WHERE p.fid = :fid', [
    ':fid' => $fid,
  ])
    ->fetchObject();

  // Set image height and width.
  if (!isset($image->height) && isset($image->uri)) {

    // The image.factory service will check if our image is valid.
    $image_info = \Drupal::service('image.factory')
      ->get($image->uri);
    if ($image_info
      ->isValid()) {
      $image->width = $image_info
        ->getWidth();
      $image->height = $image_info
        ->getHeight();
    }
    else {
      $image->width = $image->height = NULL;
    }
  }
  return $image;
}