Exemple #1
0
 /**
  * Upload an image
  *
  * @param UploadedFile $file File to upload
  *
  * @return ImageInterface|null Uploaded image or false is error
  *
  * @throws InvalidImageException File is not an image
  */
 public function uploadImage(UploadedFile $file)
 {
     $image = $this->imageManager->createImage($file);
     $this->mediaEventDispatcher->dispatchImagePreUploadEvent($image);
     $this->imageObjectManager->persist($image);
     $this->imageObjectManager->flush($image);
     $this->fileManager->uploadFile($image, file_get_contents($file->getRealPath()), true);
     $this->mediaEventDispatcher->dispatchImageOnUploadEvent($image);
     return $image;
 }
 /**
  * Dynamic upload action
  *
  * @return Response
  */
 public function uploadAction()
 {
     $request = $this->requestStack->getCurrentRequest();
     /**
      * @var $file UploadedFile
      */
     $file = $request->files->get($this->uploadFieldName);
     try {
         $image = $this->imageManager->createImage($file);
     } catch (InvalidImageException $exception) {
         return new JsonResponse(['status' => 'ko']);
     }
     $this->imageObjectManager->persist($image);
     $this->imageObjectManager->flush($image);
     $this->fileManager->uploadFile($image, file_get_contents($file->getRealPath()), true);
     return new JsonResponse(['status' => 'ok']);
 }
Exemple #3
0
 /**
  * Given an image, resize it.
  *
  * @param ImageInterface $image  Image
  * @param int            $height Height
  * @param int            $width  Width
  * @param int            $type   Type
  *
  * @return ImageInterface New Image instance
  */
 public function resize(ImageInterface $image, $height, $width, $type = ElcodiMediaImageResizeTypes::FORCE_MEASURES)
 {
     $imageData = $this->fileManager->downloadFile($image)->getContent();
     if (ElcodiMediaImageResizeTypes::NO_RESIZE === $type) {
         $image->setContent($imageData);
         return $image;
     }
     $resizedImageData = $this->resizeAdapter->resize($imageData, $height, $width, $type);
     /**
      * We need to physically store the new resized
      * image in order to access its metadata, such as
      * file size, image dimensions, mime type etc.
      * Ideally, we should be doing this in memory.
      */
     $resizedFile = new File(tempnam(sys_get_temp_dir(), '_generated'));
     file_put_contents($resizedFile, $resizedImageData);
     $image = $this->createImage($resizedFile);
     $image->setContent($resizedImageData);
     unlink($resizedFile);
     return $image;
 }