Sulu\Bundle\MediaBundle\Media\FormatManager\FormatManager::returnImage PHP Method

returnImage() public method

public returnImage ( $id, $formatKey )
    public function returnImage($id, $formatKey)
    {
        $setExpireHeaders = false;
        try {
            $media = $this->mediaRepository->findMediaByIdForRendering($id, $formatKey);
            if (!$media) {
                throw new ImageProxyMediaNotFoundException('Media was not found');
            }
            $fileVersion = $this->getLatestFileVersion($media);
            if (!$this->checkMimeTypeSupported($fileVersion->getMimeType())) {
                throw new InvalidMimeTypeForPreviewException($fileVersion->getMimeType());
            }
            // Convert Media to format.
            $responseContent = $this->converter->convert($fileVersion, $formatKey);
            // HTTP Headers
            $status = 200;
            $setExpireHeaders = true;
            $finfo = new \finfo(FILEINFO_MIME_TYPE);
            $mimeType = $finfo->buffer($responseContent);
            // Save image.
            if ($this->saveImage) {
                $this->formatCache->save($responseContent, $media->getId(), $this->replaceExtension($fileVersion->getName(), $mimeType), $fileVersion->getStorageOptions(), $formatKey);
            }
        } catch (MediaException $e) {
            $responseContent = null;
            $status = 404;
            $mimeType = null;
        }
        // Set header.
        $headers = $this->getResponseHeaders($mimeType, $setExpireHeaders);
        // Return image.
        return new Response($responseContent, $status, $headers);
    }

Usage Example

Beispiel #1
0
 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());
 }