public function testReturnImage() { $mediaRepository = $this->prophesize('Sulu\\Bundle\\MediaBundle\\Entity\\MediaRepository'); $originalStorage = $this->prophesize('Sulu\\Bundle\\MediaBundle\\Media\\Storage\\StorageInterface'); $formatCache = $this->prophesize('Sulu\\Bundle\\MediaBundle\\Media\\FormatCache\\FormatCacheInterface'); $converter = $this->prophesize('Sulu\\Bundle\\MediaBundle\\Media\\ImageConverter\\ImageConverterInterface'); $videoThumbnailService = $this->prophesize('Sulu\\Bundle\\MediaBundle\\Media\\Video\\VideoThumbnailServiceInterface'); $ghostScriptPath = ''; $saveImage = true; $previewMimeTypes = ['gif']; $responseHeaders = []; $formats = ['640x480' => ['name' => '640x480', 'commands' => [['action' => 'resize', 'parameters' => ['x' => 640, 'y' => 480]]], 'options' => ['jpeg_quality' => 70, 'png_compression_level' => 6]]]; $image = $this->prophesize('Imagine\\Image\\ImageInterface'); $image->strip()->willReturn(null); $image->layers()->willReturn(null); $image->interlace(ImageInterface::INTERLACE_PLANE)->willReturn(null); $image->get('gif', $formats['640x480']['options'])->willReturn('Image-Content'); $media = new Media(); $reflection = new \ReflectionClass(get_class($media)); $property = $reflection->getProperty('id'); $property->setAccessible(true); $property->setValue($media, 1); $file = new File(); $file->setVersion(1); $fileVersion = new FileVersion(); $fileVersion->setVersion(1); $fileVersion->setName('dummy.gif'); $fileVersion->setMimeType('gif'); $fileVersion->setStorageOptions(['a' => 'b']); $file->addFileVersion($fileVersion); $media->addFile($file); $mediaRepository->findMediaById(1)->willReturn($media); $originalStorage->load('dummy.gif', 1, ['a' => 'b'])->willReturn(dirname(__DIR__) . '/../../Fixtures/image/data/dummy.gif'); $converter->convert(Argument::type('string'), $formats['640x480'])->willReturn($image->reveal()); $formatCache->save(Argument::type('string'), 1, 'dummy.gif', ['a' => 'b'], '640x480')->willReturn(null); $formatManager = new FormatManager($mediaRepository->reveal(), $originalStorage->reveal(), $formatCache->reveal(), $converter->reveal(), $videoThumbnailService->reveal(), $ghostScriptPath, $saveImage, $previewMimeTypes, $responseHeaders, $formats); $result = $formatManager->returnImage(1, '640x480'); $this->assertEquals('Image-Content', $result->getContent()); $this->assertEquals(200, $result->getStatusCode()); }
public function setUpMediaEntities($contact) { $mediaType = new MediaType(); $mediaType->setName('document'); $mediaType->setDescription('This is a document'); $imageType = new MediaType(); $imageType->setName('image'); $imageType->setDescription('This is an image'); $videoType = new MediaType(); $videoType->setName('video'); $videoType->setDescription('This is a video'); $audioType = new MediaType(); $audioType->setName('audio'); $audioType->setDescription('This is an audio'); $media = new Media(); $media->setType($imageType); $this->media = $media; $media2 = new Media(); $media2->setType($imageType); $this->media2 = $media2; $contact->addMedia($media2); // create file $file = new File(); $file->setVersion(1); $file->setMedia($media); $file2 = new File(); $file2->setVersion(1); $file2->setMedia($media2); // create file version $fileVersion = new FileVersion(); $fileVersion->setVersion(1); $fileVersion->setName('photo.jpeg'); $fileVersion->setMimeType('image/jpg'); $fileVersion->setFile($file); $fileVersion->setSize(1124214); $fileVersion->setDownloadCounter(2); $fileVersion->setStorageOptions('{"segment":"1","fileName":"photo.jpeg"}'); $file->addFileVersion($fileVersion); // create file version $fileVersion = new FileVersion(); $fileVersion->setVersion(1); $fileVersion->setName('photo.jpeg'); $fileVersion->setMimeType('image/jpg'); $fileVersion->setFile($file2); $fileVersion->setSize(1124214); $fileVersion->setDownloadCounter(2); $fileVersion->setStorageOptions('{"segment":"1","fileName":"photo.jpeg"}'); $file2->addFileVersion($fileVersion); $collection = new Collection(); $this->setUpCollection($collection); $media->setCollection($collection); $media2->setCollection($collection); $this->em->persist($media); $this->em->persist($media2); $this->em->persist($collection); $this->em->persist($file); $this->em->persist($file2); $this->em->persist($videoType); $this->em->persist($imageType); $this->em->persist($audioType); $this->em->persist($mediaType); }
public function initLogo() { $collectionType = new CollectionType(); $collectionType->setName('My collection type'); $this->em->persist($collectionType); $collection = new Collection(); $collection->setType($collectionType); $this->em->persist($collection); $imageType = new MediaType(); $imageType->setName('image'); $imageType->setDescription('This is an image'); $this->em->persist($imageType); $file = new File(); $file->setVersion(1); $fileVersion = new FileVersion(); $fileVersion->setVersion(1); $fileVersion->setName('logo.jpeg'); $fileVersion->setMimeType('image/jpg'); $fileVersion->setFile($file); $fileVersion->setSize(1124214); $fileVersion->setDownloadCounter(2); $fileVersion->setChanged(new \DateTime('1937-04-20')); $fileVersion->setCreated(new \DateTime('1937-04-20')); $file->addFileVersion($fileVersion); $this->em->persist($fileVersion); $this->logo = new Media(); $this->logo->setType($imageType); $this->logo->setCollection($collection); $this->logo->addFile($file); $file->setMedia($this->logo); $this->em->persist($this->logo); $this->em->persist($file); }
/** * @param int $mediaId * @param FileVersion $fileVersion * @param string $locale * * @return array */ protected function getPreviewsFromFileVersion($mediaId, $fileVersion, $locale) { $title = ''; /* * @var FileVersionMeta */ foreach ($fileVersion->getMeta() as $key => $meta) { if ($meta->getLocale() == $locale) { $title = $meta->getTitle(); break; } elseif ($key == 0) { // fallback title $title = $meta->getTitle(); } } $mediaFormats = $this->formatManager->getFormats($mediaId, $fileVersion->getName(), $fileVersion->getStorageOptions(), $fileVersion->getVersion(), $fileVersion->getMimeType()); foreach ($mediaFormats as $formatName => $formatUrl) { if ($formatName == $this->collectionPreviewFormat) { return ['url' => $formatUrl, 'title' => $title]; break; } } return []; }
/** * Create a new media. * * @param $data * @param $user * * @return Media */ protected function createMedia($data, $user) { $mediaEntity = new MediaEntity(); $mediaEntity->setCreator($user); $mediaEntity->setChanger($user); $file = new File(); $file->setCreator($user); $file->setChanger($user); $file->setVersion(1); $file->setMedia($mediaEntity); $fileVersion = new FileVersion(); $fileVersion->setCreator($user); $fileVersion->setChanger($user); $fileVersion->setVersion(1); $fileVersion->setFile($file); $file->addFileVersion($fileVersion); $mediaEntity->addFile($file); $media = new Media($mediaEntity, $data['locale'], null); $media = $this->setDataToMedia($media, $data, $user); $fileVersion->setDefaultMeta($fileVersion->getMeta()->first()); $mediaEntity = $media->getEntity(); $this->em->persist($mediaEntity); $this->em->flush(); return $media; }
protected function createMedia($name) { $media = new Media(); $media->setType($this->imageType); // create file $file = new File(); $file->setVersion(1); $file->setMedia($media); // create file version $fileVersion = new FileVersion(); $fileVersion->setVersion(1); $fileVersion->setName($name . '.jpeg'); $fileVersion->setMimeType('image/jpg'); $fileVersion->setFile($file); $fileVersion->setSize(1124214); $fileVersion->setDownloadCounter(2); $fileVersion->setChanged(new \DateTime('1937-04-20')); $fileVersion->setCreated(new \DateTime('1937-04-20')); $fileVersion->setStorageOptions('{"segment":"1","fileName":"' . $name . '.jpeg"}'); if (!file_exists(__DIR__ . '/../../uploads/media/1')) { mkdir(__DIR__ . '/../../uploads/media/1', 0777, true); } copy($this->getImagePath(), __DIR__ . '/../../uploads/media/1/' . $name . '.jpeg'); // create meta $fileVersionMeta = new FileVersionMeta(); $fileVersionMeta->setLocale('en-gb'); $fileVersionMeta->setTitle($name); $fileVersionMeta->setDescription($this->mediaDefaultDescription); $fileVersionMeta->setFileVersion($fileVersion); $fileVersion->addMeta($fileVersionMeta); $fileVersion->setDefaultMeta($fileVersionMeta); $file->addFileVersion($fileVersion); $media->addFile($file); $media->setCollection($this->collection); $this->em->persist($media); $this->em->persist($file); $this->em->persist($fileVersionMeta); $this->em->persist($fileVersion); $this->em->flush(); return $media; }
protected function createMedia($id) { $mediaIdReflection = new \ReflectionProperty(Media::class, 'id'); $mediaIdReflection->setAccessible(true); $media = new Media(); $mediaIdReflection->setValue($media, $id); $file = new File(); $fileVersion = new FileVersion(); $fileVersion->setName('Media' . $id); $file->addFileVersion($fileVersion); $media->addFile($file); return $media; }
/** * @param FileVersion $fileVersion * @param string $locale * @param string $dispositionType * * @return BinaryFileResponse */ protected function getFileResponse($fileVersion, $locale, $dispositionType = ResponseHeaderBag::DISPOSITION_ATTACHMENT) { $cleaner = $this->get('sulu.content.path_cleaner'); $fileName = $fileVersion->getName(); $fileSize = $fileVersion->getSize(); $storageOptions = $fileVersion->getStorageOptions(); $mimeType = $fileVersion->getMimeType(); $version = $fileVersion->getVersion(); $path = $this->getStorage()->load($fileName, $version, $storageOptions); $response = new BinaryFileResponse($path); $pathInfo = pathinfo($fileName); // Prepare headers $disposition = $response->headers->makeDisposition($dispositionType, $fileName, $cleaner->cleanup($pathInfo['filename'], $locale) . '.' . $pathInfo['extension']); // Set headers $response->headers->set('Content-Type', !empty($mimeType) ? $mimeType : 'application/octet-stream'); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-length', $fileSize); return $response; }
/** * Purges a file-version of a media with a given id. * * @param int $mediaId * @param FileVersion $fileVersion */ private function purgeMedia($mediaId, FileVersion $fileVersion) { $this->formatManager->purge($mediaId, $fileVersion->getName(), $fileVersion->getStorageOptions()); }
/** * @param FileVersion $fileVersion * @param string $locale * @param string $dispositionType * * @return StreamedResponse */ protected function getFileResponse($fileVersion, $locale, $dispositionType = ResponseHeaderBag::DISPOSITION_ATTACHMENT) { $cleaner = $this->get('sulu.content.path_cleaner'); $fileName = $fileVersion->getName(); $fileSize = $fileVersion->getSize(); $storageOptions = $fileVersion->getStorageOptions(); $mimeType = $fileVersion->getMimeType(); $version = $fileVersion->getVersion(); $path = $this->getStorage()->load($fileName, $version, $storageOptions); $response = new StreamedResponse(function () use($path) { flush(); // send headers $handle = fopen($path, 'r'); while (!feof($handle)) { $buffer = fread($handle, 1024); echo $buffer; flush(); // buffered output } fclose($handle); }); $pathInfo = pathinfo($fileName); // Prepare headers $disposition = $response->headers->makeDisposition($dispositionType, $fileName, $cleaner->cleanup($pathInfo['filename'], $locale) . '.' . $pathInfo['extension']); // Set headers $response->headers->set('Content-Type', !empty($mimeType) ? $mimeType : 'application/octet-stream'); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-length', $fileSize); return $response; }
/** * Crops the given image according to the focus point defined in the file version. * * @param ImageInterface $image * @param FileVersion $fileVersion * @param array $scale * * @return ImageInterface */ private function applyFocus(ImageInterface $image, FileVersion $fileVersion, array $scale) { return $this->modifyAllLayers($image, function (ImageInterface $layer) use($fileVersion, $scale) { return $this->focus->focus($layer, $fileVersion->getFocusPointX(), $fileVersion->getFocusPointY(), $scale['x'], $scale['y']); }); }
private function createFileVersion(File $file, $title, $version) { $fileVersion = new FileVersion(); $fileVersion->setName($title . '.png'); $fileVersion->setVersion($version); $fileVersion->setSize(0); $fileVersion->setFile($file); $fileVersionMeta = new FileVersionMeta(); $fileVersionMeta->setTitle($title); $fileVersionMeta->setLocale('en'); $fileVersionMeta->setFileVersion($fileVersion); $this->em->persist($fileVersion); $this->em->persist($fileVersionMeta); }