/** * @param $imageFileObject * @param $finalMimeType * * @dataProvider imagesAsOctetStreamProvider */ public function testCreateImagesFromApplicationOctetStream(File $imageFileObject, $finalMimeType) { $this->assertEquals('application/octet-stream', $imageFileObject->getMimeType()); $image = $this->imageManager->createImage($imageFileObject); $this->assertInstanceOf('Elcodi\\Component\\Media\\Entity\\Interfaces\\ImageInterface', $image); $this->assertEquals($finalMimeType, $image->getContentType()); }
/** * 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; }
/** * Resizes an image * * @return Response * * @throws EntityNotFoundException Requested image does not exist */ public function resizeAction() { $request = $this->requestStack->getCurrentRequest(); $id = $request->get('id'); /** * We retrieve image given its id */ $image = $this->imageRepository->find($id); if (!$image instanceof ImageInterface) { throw new EntityNotFoundException($this->imageRepository->getClassName()); } $response = new Response(); $height = $request->get('height'); $width = $request->get('width'); $type = $request->get('type'); $response->setEtag($this->imageEtagTransformer->transform($image, $height, $width, $type))->setLastModified($image->getUpdatedAt())->setStatusCode(304)->setPublic(); /** * If the object has not been modified, we return the response. * Symfony will automatically put a 304 status in the response * in that case */ if ($response->isNotModified($request)) { return $response; } $image = $this->imageManager->resize($image, $height, $width, $type); $imageData = $image->getContent(); $response->setStatusCode(200)->setMaxAge($this->maxAge)->setSharedMaxAge($this->sharedMaxAge)->setContent($imageData); $response->headers->add(array('Content-Type' => $image->getContentType())); return $response; }
/** * 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']); }
/** * Create new response given a request and an image. * * Fill some data to this response given some Image properties and check if * created Response has changed. * * @param Request $request Request * @param ImageInterface $image Image * * @return Response Created response */ private function buildResponseFromImage(Request $request, ImageInterface $image) { $response = new Response(); $height = $request->get('height'); $width = $request->get('width'); $type = $request->get('type'); $response->setEtag($this->imageEtagTransformer->transform($image, $height, $width, $type))->setLastModified($image->getUpdatedAt())->setPublic(); /** * If the object has not been modified, we return the response. * Symfony will automatically put a 304 status in the response * in that case */ if ($response->isNotModified($request)) { return $response->setStatusCode(304); } $image = $this->imageManager->resize($image, $height, $width, $type); $imageData = $image->getContent(); $response->setStatusCode(200)->setMaxAge($this->maxAge)->setSharedMaxAge($this->sharedMaxAge)->setContent($imageData); $response->headers->add(['Content-Type' => $image->getContentType()]); return $response; }
/** * Steps necessary to store an image * * @param ObjectManager $imageObjectManager Image ObjectManager * @param ImageManager $imageManager ImageManager * @param Filesystem $filesystem Filesystem * @param fileIdentifierTransformer $fileIdentifierTransformer fileIdentifierTransformer * @param ProductInterface $product Product * @param string $imageName Image name * * @return $this Self object */ protected function storeImage(ObjectManager $imageObjectManager, ImageManager $imageManager, Filesystem $filesystem, FileIdentifierTransformerInterface $fileIdentifierTransformer, ProductInterface $product, $imageName) { $imagePath = realpath(dirname(__FILE__) . '/images/' . $imageName); $image = $imageManager->createImage(new File($imagePath)); $image->setPath('products'); $imageObjectManager->persist($image); $imageObjectManager->flush($image); $filesystem->write($fileIdentifierTransformer->transform($image), file_get_contents($imagePath), true); $product->addImage($image); $product->setPrincipalImage($image); return $this; }