src/Entity/App/Imagen.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity\App;
  3. use App\Utils\CommonFunctions;
  4. use App\Utils\Constants;
  5. use App\Utils\Traits\ActivoTrait;
  6. use App\Utils\Traits\PosicionTrait;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * Imagen
  13.  *
  14.  * @ORM\Table(name="imagen", indexes={@ORM\Index(name="IDX_FILENAME", columns={"filename"})})
  15.  * @ORM\Entity(repositoryClass="App\Repository\App\ImagenRepository")
  16.  * @ORM\HasLifecycleCallbacks()
  17.  */
  18. class Imagen
  19. {
  20.     const MIN_DIMENSIONS = array('width' => 1200'height' => 400);
  21.     const ASPECT_RATIO = array('width' => 2.83'height' => 1'diff' => 0.40);
  22.     /**
  23.      * @var integer|null
  24.      *
  25.      * @ORM\Column(name="id", type="integer")
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue
  28.      */
  29.     private $id;
  30.     /**
  31.      * @var File|null
  32.      *
  33.      * @Assert\Image(
  34.      *     mimeTypes={"image/jpg", "image/jpeg", "image/png"},
  35.      *     mimeTypesMessage="El tipo de imagen tiene que ser JPG o PNG",
  36.      *     maxSize="2M"
  37.      * )
  38.      */
  39.     private $file;
  40.     /**
  41.      * @var string|null
  42.      *
  43.      * @ORM\Column(name="filename", type="string", length=255)
  44.      * @Assert\Length(
  45.      *      max = 255,
  46.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  47.      * )
  48.      */
  49.     private $filename;
  50.     /**
  51.      * @var string|null
  52.      *
  53.      * @ORM\Column(name="link", type="string", length=255, nullable=true)
  54.      * @Assert\Regex(
  55.      *     pattern="/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}[\/]?/",
  56.      *     message="Ingrese un link correcto"
  57.      * )
  58.      * @Assert\Length(
  59.      *      max = 255,
  60.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  61.      * )
  62.      */
  63.     private $link;
  64.     /**
  65.      * @var string|null
  66.      *
  67.      * @ORM\Column(name="descripcion", type="string", length=255, nullable=true)
  68.      * @Assert\Length(
  69.      *      max = 255,
  70.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  71.      * )
  72.      */
  73.     private $descripcion;
  74.     use ActivoTrait;
  75.     use PosicionTrait;
  76.     use TimestampableEntity;
  77.     /**
  78.      * @return int|null
  79.      */
  80.     public function getId(): ?int
  81.     {
  82.         return $this->id;
  83.     }
  84.     /**
  85.      * @return File|null
  86.      */
  87.     public function getFile(): ?File
  88.     {
  89.         return $this->file;
  90.     }
  91.     /**
  92.      * @param File|null $file
  93.      */
  94.     public function setFile(?File $file): void
  95.     {
  96.         $this->file $file;
  97.     }
  98.     /**
  99.      * @return string|null
  100.      */
  101.     public function getFilename(): ?string
  102.     {
  103.         return $this->filename;
  104.     }
  105.     /**
  106.      * @param string|null $filename
  107.      */
  108.     public function setFilename(?string $filename): void
  109.     {
  110.         $this->filename $filename;
  111.     }
  112.     /**
  113.      * @return string|null
  114.      */
  115.     public function getLink(): ?string
  116.     {
  117.         return $this->link;
  118.     }
  119.     /**
  120.      * @param string|null $link
  121.      */
  122.     public function setLink(?string $link): void
  123.     {
  124.         $this->link $link;
  125.     }
  126.     public function getDescripcion(): ?string
  127.     {
  128.         return $this->descripcion;
  129.     }
  130.     /**
  131.      * @param string|null $descripcion
  132.      */
  133.     public function setDescripcion(?string $descripcion): void
  134.     {
  135.         $this->descripcion $descripcion;
  136.     }
  137.     private function getDirectoryFile()
  138.     {
  139.         $class CommonFunctions::getClassMethod(__CLASS__);
  140.         $class CommonFunctions::camelCaseToSnakeCase($class'-');
  141.         $dir CommonFunctions::getParameter('BASE_PATH_IMAGENES');
  142.         return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
  143.     }
  144.     public function getAbsolutePath()
  145.     {
  146.         return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getFilename());
  147.     }
  148.     public function guardarImagen()
  149.     {
  150.         $name 'imagen_' $this->getId();
  151.         $filename CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
  152.         if ($filename && $filename != $this->getFilename()) {
  153.             $this->setFilename($filename);
  154.         }
  155.     }
  156.     /**
  157.      * @ORM\PrePersist
  158.      */
  159.     public function prePersist()
  160.     {
  161.         $this->setFilename(Constants::NO_IMAGE_ICON);
  162.     }
  163.     /**
  164.      * @ORM\PreRemove
  165.      */
  166.     public function preRemove()
  167.     {
  168.         CommonFunctions::removeFile($this->getAbsolutePath());
  169.     }
  170.     public function getImagenBase64()
  171.     {
  172.         $image null;
  173.         if ($this->getId()) {
  174.             $image CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
  175.         }
  176.         return $image;
  177.     }
  178.     public function __toString()
  179.     {
  180.         return $this->getFilename();
  181.     }
  182. }