src/Entity/Image.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Entity\Formation\Activity;
  5. use App\Entity\Training\Training;
  6. use App\Repository\ImageRepository;
  7. use App\Traits\Actions;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassImageRepository::class)]
  10. #[ApiResource]
  11. class Image
  12. {
  13.     use Actions;
  14.     #[ORM\Column(name"title"type"string"length255nullabletrue)]
  15.     private ?string $title;
  16.     #[ORM\Column(name"url"type"string"length255)]
  17.     private ?string $url;
  18.     private $file;
  19.     private ?string $type;
  20.     #[ORM\ManyToOne(targetEntityTraining::class, inversedBy"images")]
  21.     #[ORM\JoinColumn(nullabletrue)]
  22.     private Training $training;
  23.     #[ORM\ManyToOne(targetEntityActivity::class, inversedBy"images")]
  24.     #[ORM\JoinColumn(nullabletrue)]
  25.     private Activity $activity;
  26.     public function __construct()
  27.     {
  28.         // date_default_timezone_set('Africa/Casablanca');
  29.         $this->createAt=new \DateTime('now');
  30.         $this->published=false;
  31.     }
  32.     public function getTitle(): ?string
  33.     {
  34.         return $this->title;
  35.     }
  36.     public function setTitle(?string $title): self
  37.     {
  38.         $this->title $title;
  39.         return $this;
  40.     }
  41.     public function getUrl(): ?string
  42.     {
  43.         return $this->url;
  44.     }
  45.     public function setUrl(?string $url): self
  46.     {
  47.         $this->url $url;
  48.         return $this;
  49.     }
  50.     public function getFile()
  51.     {
  52.         return $this->file;
  53.     }
  54.     public function setFile($file): self
  55.     {
  56.         $this->file $file;
  57.         return $this;
  58.     }
  59.     public function getType(): ?string
  60.     {
  61.         return $this->type;
  62.     }
  63.     public function setType(?string $type): self
  64.     {
  65.         $this->type $type;
  66.         return $this;
  67.     }
  68.     public function getTraining()
  69.     {
  70.         return $this->training;
  71.     }
  72.     public function setTraining($training): self
  73.     {
  74.         $this->training $training;
  75.         return $this;
  76.     }
  77.     public function getActivite()
  78.     {
  79.         return $this->activite;
  80.     }
  81.     public function setActivite($activite): self
  82.     {
  83.         $this->activite $activite;
  84.         return $this;
  85.     }
  86.     public function createThumb($folder$desired_width=100)
  87.     {
  88.         $src='images/'.$folder.'/'.$this->url;
  89.         $source_image imagecreatefromjpeg($src);
  90.         $width imagesx($source_image);
  91.         $height imagesy($source_image);
  92.         // Calculer la hauteur souhaitée de la miniature en fonction de la largeur souhaitée
  93.         $desired_height floor($height * ($desired_width $width));
  94.         // Créer une nouvelle image vide pour la miniature
  95.         $thumbnail imagecreatetruecolor($desired_width$desired_height);
  96.         // Copier et redimensionner l'image source vers la miniature
  97.         imagecopyresized($thumbnail$source_image0000$desired_width$desired_height$width$height);
  98.         // Sauvegarder la miniature dans le même dossier avec un suffixe -thumb
  99.         $thumbnail_path pathinfo($src);
  100.         $thumbnail_name $thumbnail_path['filename'] . '-thumb.' $thumbnail_path['extension'];
  101.         imagejpeg($thumbnail$thumbnail_name);
  102.         // Libérer la mémoire
  103.         imagedestroy($source_image);
  104.         imagedestroy($thumbnail);
  105.         return $thumbnail_name;
  106.     }
  107.     public function getThumb()
  108.     {
  109.         $info=pathinfo($this->url);
  110.         return $info['filename'].'-thumb.'.$info['extension'];
  111.     }
  112.     public function getActivity(): Activity
  113.     {
  114.         return $this->activity;
  115.     }
  116.     public function setActivity(Activity $activity): void
  117.     {
  118.         $this->activity $activity;
  119.     }
  120. }