Beispiel #1
0
 /**
  * @return \Intervention\Image\Image
  */
 private function getImage()
 {
     $base64 = base64_encode(file_get_contents(__DIR__ . '/assets/puppy.jpg'));
     return $this->imageFactory->createFromBase64String($base64);
 }
 /**
  * @api            {post} /images/{imageId}/image?action=annotate Create an Annotation
  * @apiGroup       Manipulating Images
  * @apiDescription Superimpose the given image (a base64 encoded string) on top of the existing image.
  * @apiParam {string} base64 Base64 encoded image to use as the annotation. This does not have to have the same
  *     dimensions as the image, but it must be the same ratio. It will be resized to the size of the image.
  * @apiUse         RequiresEditableImage
  * @apiUse         ImageSuccessResponse
  *
  * @param Image          $image
  * @param FileManager    $fileManager
  * @param PictureFactory $pictureFactory
  *
  * @throws \Exception
  * @return Response
  */
 protected function annotate(Image $image, FileManager $fileManager, PictureFactory $pictureFactory)
 {
     $this->requireEditableImage($image);
     $this->validate($this->request, ['base64' => 'required|string']);
     if ($image->operationInProgress) {
         throw new HttpException(409, "Another operation is currently in progress for this image.");
     }
     $image->operationInProgress = true;
     $image->save();
     $originalImageFile = $image->getImageFile();
     $annotationPicture = $pictureFactory->createFromBase64String($this->request->input('base64'));
     // Make sure the annotation can be resized to the image's size nicely
     $annotationRatio = round($annotationPicture->width() / $annotationPicture->height(), 2);
     $imageRatio = round($originalImageFile->width / $originalImageFile->height, 2);
     if ($annotationRatio !== $imageRatio) {
         throw new HttpException(422, "The annotation's ratio ({$annotationRatio}) is not the same as the image's ratio ({$imageRatio}).");
     }
     $originalPicture = $fileManager->getPictureForImageFile($originalImageFile);
     $image->moveOriginalFile($fileManager);
     // Resize annotation to the size of the image
     $annotationPicture->resize($originalImageFile->width, $originalImageFile->height);
     // Add the annotation on top of the original
     $originalPicture->insert($annotationPicture);
     $newFile = $fileManager->savePicture($originalPicture, FileManager::IMAGE_DIR, null, $image->getUncroppedImageFile());
     $image->setImageFile($newFile);
     $image->operationInProgress = false;
     $success = $image->save();
     return $this->response(['success' => $success, 'image' => $image->fresh()]);
 }