src/Controller/MediaController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Media;
  4. use Psr\Log\LoggerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Vich\UploaderBundle\Handler\DownloadHandler;
  10. #[Route('/media')]
  11. class MediaController extends AbstractController
  12. {
  13.     private $logger;
  14.     public function __construct(
  15.         LoggerInterface $logger,
  16.     ) {
  17.         $this->logger $logger;
  18.     }
  19.     #[Route('/{id}/display'name'app_media_display')]
  20.     public function mediaDisplay(Media $mediaDownloadHandler $downloadHandler): Response
  21.     {
  22.         $this->logger->debug(__METHOD__);
  23.         if ($media->getFilename()) {
  24.             $response $downloadHandler->downloadObject($media'file'nullnullfalse);
  25.             if($media->isVideo()) {
  26.                 $response->headers->set('Accept-Ranges''bytes');
  27.                 $response->headers->set('Content-Length'$media->getSize());
  28.                 $response->setEtag("version1");
  29.                 $response->headers->set('Cache-Control''max-age=31536000');
  30.             }
  31.             return $response;
  32.         } else {
  33.             return new Response(''Response::HTTP_NOT_FOUND);
  34.         }
  35.     }
  36.     /**
  37.      * TODO:
  38.      */
  39.     #[Route('/{id}/thumbnail-display'name'app_media_thumbnail_display')]
  40.     public function mediaThumbnailDisplay(Media $mediaDownloadHandler $downloadHandler): Response
  41.     {
  42.         $this->logger->debug(__METHOD__);
  43.         if ($media->getFilename()) {
  44.             return $downloadHandler->downloadObject($media$fileField 'file');
  45.         } else {
  46.             return new Response(''Response::HTTP_NOT_FOUND);
  47.         }
  48.     }
  49.     /**
  50.      * TODO:
  51.      */
  52.     #[Route('/share/{filename}'name'app_media_thumbnail_display_notifications')]
  53.     public function mediaThumbnailDisplayNotifications(Media $mediaDownloadHandler $downloadHandler): Response
  54.     {
  55.         $this->logger->debug(__METHOD__);
  56.         if ($media->getFilename()) {
  57.             $response $this->redimImage($media$downloadHandler);
  58.             if ($response === false) {
  59.                 $response $downloadHandler->downloadObject($media$fileField 'file'nullnullfalse);
  60.             }
  61.             return $response;
  62.         } else {
  63.             return new Response(''Response::HTTP_NOT_FOUND);
  64.         }
  65.     }
  66.     private function redimImage(Media $mediaDownloadHandler $downloadHandler){
  67.         $this->logger->debug(__METHOD__);
  68.         $this->logger->debug("archivo name ".$media->getFilename());
  69.         // Tipo de contenido
  70.         header('Content-Type: '.$media->getMimetype());
  71.         $ancho 200;
  72.         $alto 200;
  73.         // Obtener las nuevas dimensiones
  74.         $uri $this->generateUrl('app_media_display', array('id' => $media->getId()), UrlGeneratorInterface::ABSOLUTE_URL);
  75.         $this->logger->debug("uri ----".$uri);
  76.         list($ancho_orig$alto_orig) = getimagesize($uri);
  77.         if($alto_orig>0){
  78.             $ratio_orig $ancho_orig/$alto_orig;
  79.             if ($ancho/$alto $ratio_orig) {
  80.             $ancho $alto*$ratio_orig;
  81.             } else {
  82.             $alto $ancho/$ratio_orig;
  83.             }
  84.     
  85.             switch($media->getMimetype()){
  86.                 case 'image/jpeg':
  87.                      // Redimensionar
  88.                     $image_p imagecreatetruecolor($ancho$alto);
  89.                     $image imagecreatefromjpeg($uri);
  90.                     imagecopyresampled($image_p$image0000$ancho$alto$ancho_orig$alto_orig);
  91.                     // Imprimir
  92.                     return(imagejpeg($image_pnull100));
  93.                     break;
  94.                 case 'image/png':
  95.                     $image_p imagecreatetruecolor($ancho$alto);
  96.                     $image imagecreatefrompng($uri);
  97.                     imagecopyresampled($image_p$image0000$ancho$alto$ancho_orig$alto_orig);
  98.                     // Imprimir
  99.                     return(imagejpeg($image_pnull100));
  100.                     break;
  101.                 default;
  102.                     return false;
  103.             }
  104.            
  105.     
  106.             
  107.         }else{
  108.             return $downloadHandler->downloadObject($media$fileField 'file'nullnullfalse);
  109.         }
  110.         
  111.     }
  112. }