src/Controller/MediaController.php
public function viewImageAction(Request $request, $id, $format = MediaProviderInterface::FORMAT_REFERENCE)
    {
        $media = $this->getMedia($id);

        if (!$media) {
            throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id));
        }

        if (!$this->get('sonata.media.pool')->getDownloadStrategy($media)->isGranted($media, $request)) {
            throw new AccessDeniedException();
        }
        $provider = $this->getProvider($media);

        return new RedirectResponse($provider->generatePublicUrl($media, $format));
    }
public function viewImageAction(Request $request, $id, $format = MediaProviderInterface::FORMAT_REFERENCE)
    {
        $media = $this->getMedia($id);

        if (!$media) {
            throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id));
        }

        if (!$this->get('sonata.media.pool')->getDownloadStrategy($media)->isGranted($media, $request)) {
            throw new AccessDeniedException();
        }
        $provider = $this->getProvider($media);

        return new RedirectResponse($provider->generatePublicUrl($media, $format));
    }
public function viewImageAction ( Request $ request , $ id , $ format = MediaProviderInterface :: FORMAT_REFERENCE ) { $ media = $ this -> getMedia ( $ id ) ; if ( ! $ media ) { throw new NotFoundHttpException ( sprintf ( 'unable to find the media with the id : %s' , $ id ) ) ; } if ( ! $ this -> get ( 'sonata.media.pool' ) -> getDownloadStrategy ( $ media ) -> isGranted ( $ media , $ request ) ) { throw new AccessDeniedException ( ) ; } $ provider = $ this -> getProvider ( $ media ) ; return new RedirectResponse ( $ provider -> generatePublicUrl ( $ media , $ format ) ) ; }
output image direct to browser, retrieve from cache if activated.

@param Request $request
@param $id
@param string $format

@return Response
output image direct to browser retrieve from cache if activated .