src/Sulu/Bundle/MediaBundle/Controller/MediaPreviewController.php
public function postAction($id, Request $request)
    {
        try {
            $mediaManager = $this->getMediaManager();
            $systemCollectionManager = $this->get('sulu_media.system_collections.manager');

            $locale = $this->getLocale($request);

            $media = $mediaManager->getById($id, $locale);
            /** @var MediaInterface $mediaEntity */
            $mediaEntity = $media->getEntity();

            $data = $this->getData($request, false);

            // Unset id to not overwrite original file
            unset($data['id']);

            if (null !== $mediaEntity->getPreviewImage()) {
                $data['id'] = $mediaEntity->getPreviewImage()->getId();
            }
            $data['collection'] = $systemCollectionManager->getSystemCollection('sulu_media.preview_image');
            $data['locale'] = $locale;
            $data['title'] = $media->getTitle();

            $uploadedFile = $this->getUploadedFile($request, 'previewImage');
            $previewImage = $mediaManager->save($uploadedFile, $data, $this->getUser()->getId());

            $mediaEntity->setPreviewImage($previewImage->getEntity());

            $this->getDoctrine()->getManager()->flush();

            $view = $this->view($previewImage, 200);
        } catch (MediaNotFoundException $e) {
            $view = $this->view($e->toArray(), 404);
        } catch (MediaException $e) {
            $view = $this->view($e->toArray(), 400);
        }

        return $this->handleView($view);
    }
public function postAction($id, Request $request)
    {
        try {
            $mediaManager = $this->getMediaManager();
            $systemCollectionManager = $this->get('sulu_media.system_collections.manager');

            $locale = $this->getLocale($request);

            $media = $mediaManager->getById($id, $locale);
            /** @var MediaInterface $mediaEntity */
            $mediaEntity = $media->getEntity();

            $data = $this->getData($request, false);

            // Unset id to not overwrite original file
            unset($data['id']);

            if (null !== $mediaEntity->getPreviewImage()) {
                $data['id'] = $mediaEntity->getPreviewImage()->getId();
            }
            $data['collection'] = $systemCollectionManager->getSystemCollection('sulu_media.preview_image');
            $data['locale'] = $locale;
            $data['title'] = $media->getTitle();

            $uploadedFile = $this->getUploadedFile($request, 'previewImage');
            $previewImage = $mediaManager->save($uploadedFile, $data, $this->getUser()->getId());

            $mediaEntity->setPreviewImage($previewImage->getEntity());

            $this->getDoctrine()->getManager()->flush();

            $view = $this->view($previewImage, 200);
        } catch (MediaNotFoundException $e) {
            $view = $this->view($e->toArray(), 404);
        } catch (MediaException $e) {
            $view = $this->view($e->toArray(), 400);
        }

        return $this->handleView($view);
    }
public function postAction ( $ id , Request $ request ) { try { $ mediaManager = $ this -> getMediaManager ( ) ; $ systemCollectionManager = $ this -> get ( 'sulu_media.system_collections.manager' ) ; $ locale = $ this -> getLocale ( $ request ) ; $ media = $ mediaManager -> getById ( $ id , $ locale ) ; /** @var MediaInterface $mediaEntity */ $ mediaEntity = $ media -> getEntity ( ) ; $ data = $ this -> getData ( $ request , false ) ; // Unset id to not overwrite original file unset ( $ data [ 'id' ] ) ; if ( null !== $ mediaEntity -> getPreviewImage ( ) ) { $ data [ 'id' ] = $ mediaEntity -> getPreviewImage ( ) -> getId ( ) ; } $ data [ 'collection' ] = $ systemCollectionManager -> getSystemCollection ( 'sulu_media.preview_image' ) ; $ data [ 'locale' ] = $ locale ; $ data [ 'title' ] = $ media -> getTitle ( ) ; $ uploadedFile = $ this -> getUploadedFile ( $ request , 'previewImage' ) ; $ previewImage = $ mediaManager -> save ( $ uploadedFile , $ data , $ this -> getUser ( ) -> getId ( ) ) ; $ mediaEntity -> setPreviewImage ( $ previewImage -> getEntity ( ) ) ; $ this -> getDoctrine ( ) -> getManager ( ) -> flush ( ) ; $ view = $ this -> view ( $ previewImage , 200 ) ; } catch ( MediaNotFoundException $ e ) { $ view = $ this -> view ( $ e -> toArray ( ) , 404 ) ; } catch ( MediaException $ e ) { $ view = $ this -> view ( $ e -> toArray ( ) , 400 ) ; } return $ this -> handleView ( $ view ) ; }
Creates a new preview image and saves it to the provided media.

@Post("media/{id}/preview")

@param int $id
@param Request $request

@return \Symfony\Component\HttpFoundation\Response

@throws \Sulu\Bundle\MediaBundle\Media\Exception\CollectionNotFoundException
Creates a new preview image and saves it to the provided media .