src/Entity/Picture.php line 14

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use OlaSoft\Common;
  6. use App\Entity\OSFile;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\PictureRepository")
  9.  * @ORM\HasLifecycleCallbacks()
  10.  */
  11. class Picture extends OSFile
  12. {
  13.     public function __construct(?string $path null)
  14.     {
  15.         parent::__construct($path);
  16.         $this->hasThumbnail true;
  17.         $this->thumbnailDir 'upload/thumbnails/';
  18.         $this->validsMimetypes = ["image/png""image/jpeg""image/jpg""image/svg+xml""image/vnd.sealedmedia.softseal.jpg""image/vnd.sealed.png"];
  19.         if(!$path$this->dir 'upload/images/';
  20.     }
  21.     /**
  22.      * @ORM\Column(name="target", type="string", length=255, nullable=true)
  23.      * Assert\Image(
  24.      *  mimeTypes = "image/*",
  25.      *  mimeTypesMessage = "Veuillez choisir des images valides s'il vous plaît : .png, .jpeg, .jpg, .svg et .gif. Vérifiez également le MimeType."
  26.      * )
  27.      */
  28.     protected $target;
  29.     /**
  30.      * @ORM\Column(name="thumbnailDir", type="string", length=255, nullable=true)
  31.      */
  32.     protected $thumbnailDir;
  33.     /**
  34.      * @ORM\Column(name="hasThumbnail", type="boolean", nullable=true)
  35.      */
  36.     protected $hasThumbnail;
  37.     private $width;
  38.     private $height;
  39.     public function setHasThumbnail(?bool $hasThumbnail true){
  40.         $this->hasThumbnail $hasThumbnail;
  41.         return $this;
  42.     }
  43.     public function getThumbnailDir(): ?string
  44.     {
  45.         return $this->thumbnailDir;
  46.     }
  47.     public function setThumbnailDir(?string $dir){
  48.         $this->thumbnailDir $dir;
  49.         return $this;
  50.     }
  51.     protected function getThumbnailRootDir(): ?string
  52.     {
  53.         return $this->getRoot().$this->getThumbnailDir();
  54.     }
  55.     public function getThumbnail(){
  56.         return $this->getThumbnailDir().'/'.$this->target;
  57.     }
  58.     public function getWidth(): ?int
  59.     {
  60.         return $this->file getimagesize($file)[0] : null;
  61.     }
  62.     public function getHeight(): ?int
  63.     {
  64.         return $this->file getimagesize($file)[1] : null;
  65.     }
  66.     /**
  67.      * @ORM\PrePersist()
  68.      */
  69.     public function persistFile()
  70.     {
  71.         parent::persistFile();
  72.     }
  73.     /**
  74.      * @ORM\PostPersist()
  75.      * @ORM\PostUpdate()
  76.      */
  77.     public function postPersistFile()
  78.     {
  79.         if($this->file){
  80.             if ($this->autoResize)
  81.                 Common::resizeImage($this->getRootFile());
  82.             if($this->hasThumbnail){
  83.                 Common::thumbnailImage($this->getRootFile(),$this->getThumbnailRootDir());
  84.                 if($this->getPrevThumbnail() && $this->getThumbnail() != $this->getPrevThumbnail() && is_file($this->getPrevThumbnail()))
  85.                     unlink($this->getPrevThumbnail());
  86.             }
  87.         }
  88.     }
  89.     protected function getPrevThumbnail(){
  90.         return $this->getThumbnailRootDir().'/'.$this->previousTarget;
  91.     }
  92.     /**
  93.      * @ORM\PreRemove()
  94.      */
  95.     public function deleteFile()
  96.      {
  97.          if(is_file($this->getRoot().$this->getThumbnail()))
  98.              unlink($this->getRoot().$this->getThumbnail());
  99.          parent::deleteFile();
  100.      }
  101.     public function getHasThumbnail(): ?bool
  102.     {
  103.         return $this->hasThumbnail;
  104.     }
  105.     public function setTarget($target)
  106.     {
  107.         return parent::setTarget($target);
  108.     }
  109. }