You are here

public static function PhotosUpload::saveImage in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 src/PhotosUpload.php \Drupal\photos\PhotosUpload::saveImage()
  2. 6.0.x src/PhotosUpload.php \Drupal\photos\PhotosUpload::saveImage()

Image written to database.

3 calls to PhotosUpload::saveImage()
PhotosUpload::moveImageFiles in src/PhotosUpload.php
Assist batch operation by moving or copying image files to album.
PhotosUpload::unzip in src/PhotosUpload.php
Unzip archive of image files.
PhotosUploadForm::submitForm in src/Form/PhotosUploadForm.php
Form submission handler.

File

src/PhotosUpload.php, line 65

Class

PhotosUpload
Functions to help with uploading images to albums.

Namespace

Drupal\photos

Code

public static function saveImage($file) {

  // @todo re-write.
  // @todo check title, description, weight.
  //   - maybe pass file object and array of other vars.
  $exif = $file
    ->getMimeType() == 'image/jpeg' ? 1 : 0;
  if ($file
    ->id() && isset($file->pid)) {
    $fid = $file
      ->id();
    $pid = $file->pid;
    $db = \Drupal::database();
    $db
      ->merge('photos_image')
      ->key([
      'fid' => $file
        ->id(),
    ])
      ->fields([
      'pid' => $file->pid,
      'title' => isset($file->title) ? $file->title : $file
        ->getFilename(),
      'des' => isset($file->des) ? $file->des : '',
      'wid' => isset($file->wid) ? $file->wid : 0,
      'comcount' => 0,
      'count' => 0,
      'exif' => $exif,
    ])
      ->execute();
    if (isset($fid) && !empty($fid)) {
      if (isset($file->nid)) {
        $db
          ->insert('photos_node')
          ->fields([
          'nid' => $file->nid,
          'fid' => $file
            ->id(),
        ])
          ->execute();
      }
      if (\Drupal::config('photos.settings')
        ->get('photos_user_count_cron')) {
        $user = \Drupal::currentUser();
        PhotosAlbum::setCount('user_image', $file->getOwnerId ? $file->getOwnerId : $user
          ->id());
        PhotosAlbum::setCount('node_album', $file->pid);
        if (isset($file->nid)) {
          PhotosAlbum::setCount('node_node', $file->nid);
        }
      }

      // Save file and add file usage.
      $file_usage = \Drupal::service('file.usage');
      $file_usage
        ->add($file, 'photos', 'node', $pid);

      // Check admin setting for maximum image resolution.
      if ($photos_size_max = \Drupal::config('photos.settings')
        ->get('photos_size_max')) {

        // Will scale image if needed.
        file_validate_image_resolution($file, $photos_size_max);
      }
      return TRUE;
    }
  }
  return FALSE;
}