<?php
namespace App\Controller;
use App\Entity\Media;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Vich\UploaderBundle\Handler\DownloadHandler;
#[Route('/media')]
class MediaController extends AbstractController
{
private $logger;
public function __construct(
LoggerInterface $logger,
) {
$this->logger = $logger;
}
#[Route('/{id}/display', name: 'app_media_display')]
public function mediaDisplay(Media $media, DownloadHandler $downloadHandler): Response
{
$this->logger->debug(__METHOD__);
if ($media->getFilename()) {
$response = $downloadHandler->downloadObject($media, 'file', null, null, false);
if($media->isVideo()) {
$response->headers->set('Accept-Ranges', 'bytes');
$response->headers->set('Content-Length', $media->getSize());
$response->setEtag("version1");
$response->headers->set('Cache-Control', 'max-age=31536000');
}
return $response;
} else {
return new Response('', Response::HTTP_NOT_FOUND);
}
}
/**
* TODO:
*/
#[Route('/{id}/thumbnail-display', name: 'app_media_thumbnail_display')]
public function mediaThumbnailDisplay(Media $media, DownloadHandler $downloadHandler): Response
{
$this->logger->debug(__METHOD__);
if ($media->getFilename()) {
return $downloadHandler->downloadObject($media, $fileField = 'file');
} else {
return new Response('', Response::HTTP_NOT_FOUND);
}
}
/**
* TODO:
*/
#[Route('/share/{filename}', name: 'app_media_thumbnail_display_notifications')]
public function mediaThumbnailDisplayNotifications(Media $media, DownloadHandler $downloadHandler): Response
{
$this->logger->debug(__METHOD__);
if ($media->getFilename()) {
$response = $this->redimImage($media, $downloadHandler);
if ($response === false) {
$response = $downloadHandler->downloadObject($media, $fileField = 'file', null, null, false);
}
return $response;
} else {
return new Response('', Response::HTTP_NOT_FOUND);
}
}
private function redimImage(Media $media, DownloadHandler $downloadHandler){
$this->logger->debug(__METHOD__);
$this->logger->debug("archivo name ".$media->getFilename());
// Tipo de contenido
header('Content-Type: '.$media->getMimetype());
$ancho = 200;
$alto = 200;
// Obtener las nuevas dimensiones
$uri = $this->generateUrl('app_media_display', array('id' => $media->getId()), UrlGeneratorInterface::ABSOLUTE_URL);
$this->logger->debug("uri ----".$uri);
list($ancho_orig, $alto_orig) = getimagesize($uri);
if($alto_orig>0){
$ratio_orig = $ancho_orig/$alto_orig;
if ($ancho/$alto > $ratio_orig) {
$ancho = $alto*$ratio_orig;
} else {
$alto = $ancho/$ratio_orig;
}
switch($media->getMimetype()){
case 'image/jpeg':
// Redimensionar
$image_p = imagecreatetruecolor($ancho, $alto);
$image = imagecreatefromjpeg($uri);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $ancho, $alto, $ancho_orig, $alto_orig);
// Imprimir
return(imagejpeg($image_p, null, 100));
break;
case 'image/png':
$image_p = imagecreatetruecolor($ancho, $alto);
$image = imagecreatefrompng($uri);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $ancho, $alto, $ancho_orig, $alto_orig);
// Imprimir
return(imagejpeg($image_p, null, 100));
break;
default;
return false;
}
}else{
return $downloadHandler->downloadObject($media, $fileField = 'file', null, null, false);
}
}
}