| Current Path : /home/h/a/p/happyrenas/myreco.online/administration/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/h/a/p/happyrenas/myreco.online/administration/upload_photo_visite.php |
<?
//set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../');
include("../configuration.php");
include("../includes/fonctions.php");
setlocale(LC_TIME, 'fr_FR.UTF-8');
// Fonction de redimensionnement
function redimensionnerJPG($source_path, $destination_path, $max_width, $max_height) {
$image = imagecreatefromjpeg($source_path);
if (!$image) return false;
$width = imagesx($image);
$height = imagesy($image);
if ($width <= $max_width && $height <= $max_height) {
// Image déjà petite : copier telle quelle
return move_uploaded_file($source_path, $destination_path);
}
$ratio = min($max_width / $width, $max_height / $height);
$new_width = (int)($width * $ratio);
$new_height = (int)($height * $ratio);
$resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$result = imagejpeg($resized, $destination_path, 85); // Qualité JPEG à 85%
imagedestroy($image);
imagedestroy($resized);
return $result;
}
header('Content-Type: application/json');
// Paramètres
$root_upload_dir = DOSSIER_RACINE.'/upload/visite/';
$web_base_url = 'https://myreco.online/upload/visite/';
$max_file_size = 2 * 1024 * 1024; // 2 Mo = 2 097 152 octets
$max_width = 1200;
$max_height = 1200;
$response = ['message' => '', 'photo_url' => null];
// Vérifications initiales
if (!isset($_FILES['photo']) || empty($_POST['token'])) {
$response['message'] = 'Paramètres manquants.';
echo json_encode($response);
exit;
}
// Récupérer le token
$token = preg_replace('/[^a-f0-9]/', '', $_POST['token']);
$prefix = strtoupper(substr($token, 0, 2));
// Vérifier taille
if ($_FILES['photo']['size'] > $max_file_size) {
$response['message'] = 'Fichier trop volumineux (max 2 Mo).';
echo json_encode($response);
exit;
}
// Vérifier type MIME réel
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['photo']['tmp_name']);
finfo_close($finfo);
if ($mime !== 'image/jpeg') {
$response['message'] = 'Seuls les fichiers JPG sont acceptés.';
echo json_encode($response);
exit;
}
// Générer chemins
$filename = $token . '.jpg';
$dir = $root_upload_dir . $prefix . '/';
$absolute_path = $dir . $filename;
$photo_url = $web_base_url . $prefix . '/' . $filename;
// Créer dossier si besoin
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
// Supprimer ancienne photo si présente
if (file_exists($absolute_path)) {
unlink($absolute_path);
}
// Redimensionne et enregistre
if (redimensionnerJPG($_FILES['photo']['tmp_name'], $absolute_path, $max_width, $max_height)) {
$response['message'] = 'Photo JPG redimensionnée et enregistrée avec succès.'.$token;
$response['photo_url'] = $photo_url;
$photo_locale='/upload/visite/'.$prefix . '/' . $filename;
if ($_POST['table']=='vis_proprio')
{
$db->query("UPDATE vis_proprio SET photo_locale = '".$photo_locale."' where token='".$token."'");
}
if ($_POST['table']=='vis')
{
$db->query("UPDATE vis SET photo_locale = '".$photo_locale."' where token='".$token."'");
}
} else {
$response['message'] = 'Erreur lors du traitement ou du redimensionnement.';
}
echo json_encode($response);
?>