예제 #1
0
 /**
  * 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");
     }
 }
예제 #2
0
 /**
  * Execute the job.
  *
  * @param FileManager $fileManager
  *
  * @throws Exception
  */
 public function handle(FileManager $fileManager)
 {
     $this->logger = $this->getJobLogger();
     $this->logger->debug("Generating thumbnail for image {$this->imageFile->getId()} {$this->imageFile->getPath()}" . " attempt {$this->attempts()}", $this->imageFile->toArray());
     // Get a fresh copy from the DB (checks if it's deleted)
     if (!($this->imageFile = $this->imageFile->fresh())) {
         throw new Exception("ImageFile no longer exists.");
     }
     // Get full size image
     $picture = $fileManager->getPictureForImageFile($this->imageFile);
     $filename = $this->imageFile->getFilename();
     // Generate 200x200 thumbnail
     $picture->fit(200, 200, function (Constraint $constraint) {
         $constraint->upsize();
     });
     if ($thumbnailImageFile = $fileManager->savePicture($picture, 'thumb', $filename, $this->imageFile)) {
         Image::where('imageFileID', $this->imageFile->getId())->update(['thumbnailImageFileId' => $thumbnailImageFile->getId()]);
     }
 }
예제 #3
0
 /**
  * @api            {put} /images/{imageId}/image?action=rotate Rotate an Image
  * @apiGroup       Manipulating Images
  * @apiDescription Rotate an image clockwise or counter-clockwise.
  * @apiParam {string=rotate} action Action to perform.
  * @apiParam {int=90,180,270} degrees Degrees to rotate by.
  * @apiParam {string=cw,ccw} [direction=cw] Direction to rotate in (clockwise or counter-clockwise respectively).
  * @apiUse         RequiresEditableImage
  * @apiUse         ImageSuccessResponse
  *
  * @param Image       $image
  * @param FileManager $fileManager
  *
  * @throws Exception
  * @return Response
  */
 protected function rotate(Image $image, FileManager $fileManager)
 {
     $success = false;
     $this->validate($this->request, ['degrees' => 'required|integer|in:90,180,270', 'direction' => 'in:cw,ccw']);
     if ($image->operationInProgress) {
         throw new HttpException(409, "Another operation is currently in progress for this image.");
     }
     $image->operationInProgress = true;
     $image->save();
     $degrees = (int) $this->request->input('degrees');
     $direction = $this->request->has('direction') ? $this->request->input('direction') : 'cw';
     if ($direction === 'cw') {
         $degrees = -$degrees;
     }
     $picture = $fileManager->getPictureForImageFile($image->imageFile);
     $picture->rotate($degrees);
     if ($newImageFile = $fileManager->savePicture($picture)) {
         $image->setImageFile($newImageFile);
         $image->operationInProgress = false;
         $success = $image->save();
     }
     return $this->response(['success' => $success, 'image' => $image->fresh()]);
 }