Redimentionne et ajoute un watermark aux photos uploadées

This commit is contained in:
leosw
2026-04-12 14:46:21 +02:00
parent 79711d9551
commit 6d70e8236a

View File

@@ -235,13 +235,64 @@ switch ($controller->splitted_url[1]) {
$Comment->comment = $_POST['comment'];
if (!empty($_FILES['photo']['tmp_name'])) {
$tmp = $_FILES['photo']['tmp_name'];
$filename = uniqid('comment_').'.jpg'; // ou extension dynamique
$info = getimagesize($tmp);
if ($info === false) {
die("Fichier non valide");
}
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$src = imagecreatefromjpeg($tmp);
break;
case 'image/png':
$src = imagecreatefrompng($tmp);
break;
case 'image/webp':
$src = imagecreatefromwebp($tmp);
break;
default:
die("Format non supporté");
}
$width = imagesx($src);
$height = imagesy($src);
// Taille max
$max = 1500;
$ratio = min($max / $width, $max / $height, 1);
$newWidth = (int)($width * $ratio);
$newHeight = (int)($height * $ratio);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// --- Watermark ---
$watermarkPath = $config['medias_folder'] . '/watermark.png';
if (file_exists($watermarkPath)) {
$wm = imagecreatefrompng($watermarkPath);
imagealphablending($wm, true);
imagesavealpha($wm, true);
$wmWidth = imagesx($wm);
$wmHeight = imagesy($wm);
$x = $newWidth - $wmWidth - 20;
$y = $newHeight - $wmHeight - 20;
// Fusion
imagecopy($dst, $wm, $x, $y, 0, 0, $wmWidth, $wmHeight);
}
$filename = uniqid('comment_') . '.jpg';
$destination = $config['medias_folder'] . 'comment_photos/' . $filename;
move_uploaded_file($tmp, $destination);
imagejpeg($dst, $destination, 70);
// URL publique
$Comment->photo = $filename;
}