/** * 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']); }