diff options
Diffstat (limited to 'src/Model/Entity/Image.php')
| -rw-r--r-- | src/Model/Entity/Image.php | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/Model/Entity/Image.php b/src/Model/Entity/Image.php new file mode 100644 index 0000000..06230ce --- /dev/null +++ b/src/Model/Entity/Image.php | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | <?php | ||
| 2 | // src/Model/Entity/Image.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Entity; | ||
| 7 | |||
| 8 | use Doctrine\Common\Collections\Collection; | ||
| 9 | use Doctrine\ORM\Mapping as ORM; | ||
| 10 | |||
| 11 | #[ORM\Entity] | ||
| 12 | #[ORM\Table(name: TABLE_PREFIX . "image")] | ||
| 13 | class Image | ||
| 14 | { | ||
| 15 | #[ORM\Id] | ||
| 16 | #[ORM\GeneratedValue] | ||
| 17 | #[ORM\Column(type: "integer")] | ||
| 18 | private int $id_image; | ||
| 19 | |||
| 20 | #[ORM\Column(type: "string", length: 255, unique: true)] // nom d'image UNIQUE | ||
| 21 | private string $file_name; | ||
| 22 | |||
| 23 | // choisir un répertoire du genre /var/www/html/uploads/ de préférence hors de /src | ||
| 24 | #[ORM\Column(type: "string", length: 255, unique: true, nullable: true)] | ||
| 25 | private ?string $file_path; | ||
| 26 | |||
| 27 | #[ORM\Column(type: "string", length: 255, unique: true, nullable: true)] | ||
| 28 | private ?string $file_path_mini; | ||
| 29 | |||
| 30 | #[ORM\Column(type: "string", length: 255, nullable: true)] | ||
| 31 | private string $mime_type; // image/jpeg, image/png, etc | ||
| 32 | |||
| 33 | #[ORM\Column(type: "string", length: 255, nullable: true)] | ||
| 34 | private string $alt; // texte alternatif | ||
| 35 | |||
| 36 | // autre champs optionnels: file_size, date (default current timestamp) | ||
| 37 | |||
| 38 | /* étapes au téléchargement: | ||
| 39 | => Validation du type de fichier : On vérifie que le fichier est bien une image en utilisant le type MIME. On peut aussi vérifier la taille du fichier. | ||
| 40 | => Création d'un répertoire structuré : On génère un chemin dynamique basé sur la date (uploads/2024/12/24/) pour organiser les images. | ||
| 41 | => Génération d'un nom de fichier unique : On utilise uniqid() pour générer un nom unique et éviter les conflits de nom. | ||
| 42 | => Déplacement du fichier sur le serveur : Le fichier est déplacé depuis son emplacement temporaire vers le répertoire uploads/. | ||
| 43 | => Enregistrement dans la base de données : On enregistre les informations de l'image dans la base de données. */ | ||
| 44 | |||
| 45 | #[ORM\ManyToMany(targetEntity: Article::class, mappedBy: "images")] | ||
| 46 | private Collection $article; | ||
| 47 | |||
| 48 | public function __construct(string $name, ?string $path, ?string $path_mini, string $mime_type, string $alt) | ||
| 49 | { | ||
| 50 | $this->file_name = $name; | ||
| 51 | $this->file_path = $path; | ||
| 52 | $this->file_path_mini = $path_mini; | ||
| 53 | $this->mime_type = $mime_type; | ||
| 54 | $this->alt = $alt; | ||
| 55 | } | ||
| 56 | |||
| 57 | public function getFileName(): string | ||
| 58 | { | ||
| 59 | return $this->file_name; | ||
| 60 | } | ||
| 61 | public function getFilePath(): string | ||
| 62 | { | ||
| 63 | return $this->file_path; | ||
| 64 | } | ||
| 65 | public function getFilePathMini(): string | ||
| 66 | { | ||
| 67 | return $this->file_path_mini; | ||
| 68 | } | ||
| 69 | public function getAlt(): string | ||
| 70 | { | ||
| 71 | return $this->alt; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | // pour ViewBuilderController? | ||
| 76 | /*public function displayImage($imageId): void | ||
| 77 | { | ||
| 78 | //$imageId = 1; // Exemple d'ID d'image | ||
| 79 | $stmt = $pdo->prepare("SELECT file_path FROM images WHERE id = ?"); | ||
| 80 | $stmt->execute([$imageId]); | ||
| 81 | $image = $stmt->fetch(); | ||
| 82 | |||
| 83 | if ($image) { | ||
| 84 | echo "<img src='" . $image['file_path'] . "' alt='Image'>"; | ||
| 85 | } else { | ||
| 86 | echo "Image non trouvée."; | ||
| 87 | } | ||
| 88 | }*/ | ||
| 89 | } | ||
