/**
  * Execute the job.
  *
  * @param FileManager $fileManager
  *
  * @throws Exception
  */
 public function handle(FileManager $fileManager)
 {
     $this->logger = $this->getJobLogger();
     $this->logger->debug("Generating thumbnail for album {$this->album->getId()}" . " attempt {$this->attempts()}", $this->album->toArray());
     // Get a fresh copy from the DB (checks if it's deleted)
     if (!($this->album = $this->album->fresh())) {
         throw new Exception("Album no longer exists.");
     }
     // Get first 4 album pictures
     /** @var Image[] $images */
     $images = $this->album->images()->orderBy('imageId', 'DESC')->take(4)->get();
     $filenames = [];
     foreach ($images as $image) {
         $imageFile = $image->getImageFile();
         try {
             // This will make sure the file exists locally
             $picture = $fileManager->getPictureForImageFile($imageFile);
         } catch (Exception $e) {
             echo "Unable to download image file {$imageFile->getPath()} " . $e->getMessage() . PHP_EOL;
             continue;
         }
         if (@file_exists($imageFile->getAbsolutePath())) {
             // The [0] at the end is to cater for animated gifs
             // - use the first frame
             $filenames[] = '"' . $imageFile->getAbsolutePath() . '[0]"';
         } else {
             echo "Unable to download image file {$imageFile->getPath()}" . PHP_EOL;
         }
     }
     if (empty($filenames)) {
         return false;
     }
     print_r($filenames);
     $thumbFilename = $fileManager->prepareLocalFile('albumthumb', 'jpg');
     $thumbAbsolutePath = Config::get('app.data_dir') . 'albumthumb/' . $thumbFilename;
     $cmd = "montage " . implode(' ', $filenames) . " -gravity center -resize \"50^\" -crop 50x50+0+0 -geometry 50x50 -tile 2x2 " . $thumbAbsolutePath;
     echo $cmd . PHP_EOL;
     passthru($cmd);
     if ($fileSize = filesize($thumbAbsolutePath)) {
         $imageFile = new ImageFile(['directory' => 'albumthumb', 'filename' => $thumbFilename, 'width' => 100, 'height' => 100, 'size' => $fileSize]);
         $imageFile->save();
         $this->dispatch(new OptimizeFileJob($imageFile));
         $this->album->thumbnailImageFileId = $imageFile->imageFileId;
         $this->album->save();
     } else {
         throw new Exception("Unable to create montage");
     }
 }
Esempio n. 2
0
 /**
  * Save the Image model to the database.
  * If the fileId is changed (meaning the picture has changed) delete
  * the old file and generate a new thumbnail. Delete the old thumbnail file if one exists.
  *
  * @param array $options
  *
  * @return bool
  */
 public function save(array $options = [])
 {
     $this->table = 'images';
     /**
      * Remember these IDs now because the parent::save method clears
      * the isDirty flag and the original data.
      */
     $generateNewThumbnail = false;
     $originalImageFileId = null;
     $originalThumbnailImageFileId = null;
     if ($this->isDirty('imageFileId') || !$this->exists) {
         $originalImageFileId = $this->getOriginal('imageFileId');
         $this->setThumbnailImageFile(null);
         $originalThumbnailImageFileId = $this->getOriginal('thumbnailImageFileId');
         $generateNewThumbnail = true;
     }
     // Save the model
     $result = parent::save($options);
     if ($originalImageFileId && ($originalImageFile = ImageFile::find($originalImageFileId))) {
         if ($originalImageFileId != $this->uncroppedImageFileId) {
             $originalImageFile->delete();
         }
     }
     if ($originalThumbnailImageFileId && ($originalThumbnailImageFile = ImageFile::find($originalThumbnailImageFileId))) {
         $originalThumbnailImageFile->delete();
     }
     if ($generateNewThumbnail && ($imageFile = ImageFile::find($this->imageFileId))) {
         /** @var ImageFile $imageFile */
         $this->dispatch(new MakeThumbnailJob($imageFile));
     }
     $this->table = 'view_images';
     return $result;
 }
Esempio n. 3
0
 /**
  * Delete the old thumbnail image on save if changing it
  * @param array $options
  *
  * @return bool
  * @throws \Exception
  */
 public function save(array $options = [])
 {
     /**
      * Remember these IDs now because the parent::save method clears
      * the isDirty flag and the original data.
      */
     $originalThumbnailImageFileId = null;
     if ($this->isDirty('thumbnailImageFileId')) {
         $originalThumbnailImageFileId = $this->getOriginal('thumbnailImageFileId');
     }
     // Save the model
     $result = parent::save($options);
     if ($originalThumbnailImageFileId && ($originalThumbnailImageFile = ImageFile::find($originalThumbnailImageFileId))) {
         $originalThumbnailImageFile->delete();
     }
     return $result;
 }
Esempio n. 4
0
 public function moveFile(ImageFile $file, $newDirectory)
 {
     $oldPath = $file->getPath();
     $newPath = $newDirectory . '/' . $file->getFilename();
     // Move the local file
     if ($this->localFileExists($oldPath)) {
         $this->createLocalDirectory(dirname($newPath));
         rename($this->localDataDirectory . $oldPath, $this->localDataDirectory . $newPath);
         clearstatcache();
     }
     // Move the remote file
     $this->dispatch(new MoveRemoteFileJob($oldPath, $newPath));
     $file->directory = $newDirectory;
     $file->save();
     $cacheManager = new CacheManager();
     $cacheManager->purge($oldPath);
     $cacheManager->purge($newPath);
 }