143 lines
5.1 KiB
PHP
143 lines
5.1 KiB
PHP
<?php
|
|
class SiteUploadHandler {
|
|
private $uploadDir;
|
|
private $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
|
|
private $maxFileSize = 2097152; // 2MB
|
|
|
|
public function __construct() {
|
|
$this->uploadDir = __DIR__ . '/../assets/images/site/';
|
|
$this->ensureUploadDirectory();
|
|
}
|
|
|
|
public function handleLogoUpload($file) {
|
|
try {
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
throw new Exception($this->getUploadErrorMessage($file['error']));
|
|
}
|
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
$mimeType = $finfo->file($file['tmp_name']);
|
|
if (!in_array($mimeType, $this->allowedTypes)) {
|
|
throw new Exception('Type de fichier non autorisé. Types acceptés : JPG, PNG, GIF, SVG');
|
|
}
|
|
|
|
if ($file['size'] > $this->maxFileSize) {
|
|
throw new Exception('Fichier trop volumineux. Taille maximum : 2MB');
|
|
}
|
|
|
|
$extension = $this->getExtensionFromMimeType($mimeType);
|
|
$filename = 'logo.' . $extension;
|
|
$targetPath = $this->uploadDir . $filename;
|
|
|
|
$this->removeOldLogo();
|
|
|
|
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
|
|
throw new Exception('Erreur lors du déplacement du fichier uploadé');
|
|
}
|
|
|
|
// Créer le favicon si ce n'est pas un SVG
|
|
if ($mimeType !== 'image/svg+xml') {
|
|
$this->createFavicon($targetPath, $mimeType);
|
|
}
|
|
|
|
return 'assets/images/site/' . $filename;
|
|
|
|
} catch (Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function ensureUploadDirectory() {
|
|
if (!file_exists($this->uploadDir)) {
|
|
if (!mkdir($this->uploadDir, 0755, true)) {
|
|
throw new Exception('Impossible de créer le dossier d\'upload');
|
|
}
|
|
}
|
|
}
|
|
|
|
private function removeOldLogo() {
|
|
foreach (glob($this->uploadDir . 'logo.*') as $file) {
|
|
unlink($file);
|
|
}
|
|
$faviconPath = $this->uploadDir . 'favicon.png';
|
|
if (file_exists($faviconPath)) {
|
|
unlink($faviconPath);
|
|
}
|
|
}
|
|
|
|
private function createFavicon($sourcePath, $mimeType) {
|
|
try {
|
|
$source = $this->createImageFromFile($sourcePath, $mimeType);
|
|
if (!$source) {
|
|
throw new Exception('Impossible de créer l\'image source pour le favicon');
|
|
}
|
|
|
|
$favicon = imagecreatetruecolor(32, 32);
|
|
if (!$favicon) {
|
|
imagedestroy($source);
|
|
throw new Exception('Impossible de créer l\'image de destination pour le favicon');
|
|
}
|
|
|
|
imagealphablending($favicon, false);
|
|
imagesavealpha($favicon, true);
|
|
|
|
if (!imagecopyresampled(
|
|
$favicon, $source,
|
|
0, 0, 0, 0,
|
|
32, 32,
|
|
imagesx($source), imagesy($source)
|
|
)) {
|
|
imagedestroy($source);
|
|
imagedestroy($favicon);
|
|
throw new Exception('Erreur lors du redimensionnement du favicon');
|
|
}
|
|
|
|
$faviconPath = $this->uploadDir . 'favicon.png';
|
|
if (!imagepng($favicon, $faviconPath, 9)) {
|
|
throw new Exception('Impossible de sauvegarder le favicon');
|
|
}
|
|
|
|
imagedestroy($source);
|
|
imagedestroy($favicon);
|
|
|
|
} catch (Exception $e) {
|
|
error_log('Erreur lors de la création du favicon : ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function createImageFromFile($file, $mimeType) {
|
|
switch ($mimeType) {
|
|
case 'image/jpeg':
|
|
return imagecreatefromjpeg($file);
|
|
case 'image/png':
|
|
return imagecreatefrompng($file);
|
|
case 'image/gif':
|
|
return imagecreatefromgif($file);
|
|
default:
|
|
throw new Exception('Type d\'image non supporté pour le favicon');
|
|
}
|
|
}
|
|
|
|
private function getExtensionFromMimeType($mimeType) {
|
|
$map = [
|
|
'image/jpeg' => 'jpg',
|
|
'image/png' => 'png',
|
|
'image/gif' => 'gif',
|
|
'image/svg+xml' => 'svg'
|
|
];
|
|
return $map[$mimeType] ?? 'png';
|
|
}
|
|
|
|
private function getUploadErrorMessage($error) {
|
|
$errors = [
|
|
UPLOAD_ERR_INI_SIZE => 'Le fichier dépasse la taille maximale autorisée par PHP',
|
|
UPLOAD_ERR_FORM_SIZE => 'Le fichier dépasse la taille maximale autorisée par le formulaire',
|
|
UPLOAD_ERR_PARTIAL => 'Le fichier n\'a été que partiellement uploadé',
|
|
UPLOAD_ERR_NO_FILE => 'Aucun fichier n\'a été uploadé',
|
|
UPLOAD_ERR_NO_TMP_DIR => 'Dossier temporaire manquant',
|
|
UPLOAD_ERR_CANT_WRITE => 'Échec de l\'écriture du fichier sur le disque',
|
|
UPLOAD_ERR_EXTENSION => 'Une extension PHP a arrêté l\'upload'
|
|
];
|
|
return $errors[$error] ?? 'Erreur inconnue lors de l\'upload';
|
|
}
|
|
} |