new restylt

This commit is contained in:
2026-08-02 13:53:56 +02:00
parent c8ab259217
commit 9ccdc34d4a
391 changed files with 2760 additions and 83108 deletions
+226 -50
View File
@@ -1,58 +1,234 @@
<?php
include('include/headscript.php'); ?>
<?php
if (!empty($_FILES['images'])) {
// File upload configuration
if (isset($_POST["idtrf"])) {
$idtrf = $_POST["idtrf"];
}
$targetDir = "uploadimages/";
$allowTypes = array('jpg', 'png', 'jpeg', 'gif');
include('include/headscript.php');
$lang = $_SESSION['langselect'] ?? 'it';
$images_arr = array();
foreach ($_FILES['images']['name'] as $key => $val) {
$image_name = $_FILES['images']['name'][$key];
$tmp_name = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$type = $_FILES['images']['type'][$key];
$error = $_FILES['images']['error'][$key];
$messages = [
'it' => [
'missing_trf' => 'ID TRF mancante.',
'no_image' => 'Nessuna immagine selezionata.',
'upload_error' => 'Errore durante il caricamento.',
'too_large' => 'Immagine troppo grande. Dimensione massima consentita: 8 MB.',
'invalid_extension' => 'Estensione non valida. Sono consentiti solo JPG, JPEG e PNG.',
'invalid_type' => 'Tipo immagine non valido. Sono consentiti solo JPG, JPEG e PNG.',
'invalid_image' => 'File immagine non valido.',
'unsupported_type' => 'Tipo immagine non supportato.',
'process_error' => 'Impossibile elaborare limmagine.',
'save_error' => 'Impossibile salvare limmagine ridimensionata.'
],
'en' => [
'missing_trf' => 'Missing TRF ID.',
'no_image' => 'No image selected.',
'upload_error' => 'Upload error.',
'too_large' => 'Image too large. Maximum allowed size is 8 MB.',
'invalid_extension' => 'Invalid file extension. Only JPG, JPEG and PNG are allowed.',
'invalid_type' => 'Invalid image type. Only JPG, JPEG and PNG are allowed.',
'invalid_image' => 'Invalid image file.',
'unsupported_type' => 'Unsupported image type.',
'process_error' => 'Unable to process image.',
'save_error' => 'Unable to save resized image.'
]
];
// File upload path
$code = time();
$fileName = basename($_FILES['images']['name'][$key]);
$fileName = $idtrf . '-' . $code . $fileName;
$targetFilePath = $targetDir . $fileName;
$msg = $messages[$lang] ?? $messages['it'];
ini_set('display_errors', 0);
error_reporting(E_ALL);
// =======================
// Upload settings
// =======================
$targetDir = "uploadimages/";
$targetDirPath = __DIR__ . "/uploadimages/";
$maxUploadSize = 8 * 1024 * 1024; // 8 MB
$maxWidth = 1600;
$maxHeight = 1600;
$jpgQuality = 82;
// Check whether file type is valid
$fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
if (in_array($fileType, $allowTypes)) {
// Store images on the server
if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $targetFilePath)) {
$images_arr[] = $targetFilePath;
$allowedExtensions = ['jpg', 'jpeg', 'png'];
$allowedMimeTypes = ['image/jpeg', 'image/png'];
$UpdateQuery = new WA_MySQLi_Query($cmctrfdb);
$UpdateQuery->Action = "update";
$UpdateQuery->Table = "`trf-details`";
$UpdateQuery->bindColumn("photofilename", "s", "$fileName", "WA_DEFAULT");
$UpdateQuery->addFilter("idtrfdetails", "=", "i", "" . ($idtrf) . "");
$UpdateQuery->execute();
$UpdateGoTo = "";
$UpdateQuery->redirect($UpdateGoTo);
}
}
}
// Generate gallery view of the images
if (!empty($images_arr)) { ?>
<ul>
<?php foreach ($images_arr as $image_src) { ?>
<img src="<?php echo $image_src; ?>" alt="" width="200">
<?php } ?>
</ul>
<?php }
// =======================
// Basic checks
// =======================
if (empty($_POST["idtrf"])) {
http_response_code(400);
echo $msg['missing_trf'];
exit;
}
?>
$idtrf = intval($_POST["idtrf"]);
if (empty($_FILES['images']) || empty($_FILES['images']['name'][0])) {
http_response_code(400);
echo $msg['no_image'];
exit;
}
if (!is_dir($targetDirPath)) {
mkdir($targetDirPath, 0755, true);
}
// We use only the first image, as this page manages the main TRF photo
$key = 0;
$imageName = $_FILES['images']['name'][$key];
$tmpName = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$error = $_FILES['images']['error'][$key];
if ($error !== UPLOAD_ERR_OK) {
http_response_code(400);
echo $msg['upload_error'];
exit;
}
if ($size > $maxUploadSize) {
http_response_code(400);
echo $msg['too_large'];
exit;
}
// =======================
// Extension check
// =======================
$extension = strtolower(pathinfo($imageName, PATHINFO_EXTENSION));
if (!in_array($extension, $allowedExtensions)) {
http_response_code(400);
echo $msg['invalid_extension'];
exit;
}
// =======================
// Real MIME check
// =======================
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $tmpName);
finfo_close($finfo);
if (!in_array($mimeType, $allowedMimeTypes)) {
http_response_code(400);
echo $msg['invalid_type'];
exit;
}
// =======================
// Image validation
// =======================
$imageInfo = getimagesize($tmpName);
if ($imageInfo === false) {
http_response_code(400);
echo $msg['invalid_image'];
exit;
}
$originalWidth = $imageInfo[0];
$originalHeight = $imageInfo[1];
// =======================
// Create source image
// =======================
if ($mimeType === 'image/jpeg') {
$sourceImage = imagecreatefromjpeg($tmpName);
} elseif ($mimeType === 'image/png') {
$sourceImage = imagecreatefrompng($tmpName);
} else {
http_response_code(400);
echo $msg['unsupported_type'];
exit;
}
if (!$sourceImage) {
http_response_code(400);
echo $msg['process_error'];
exit;
}
// =======================
// Resize calculation
// =======================
$ratio = min($maxWidth / $originalWidth, $maxHeight / $originalHeight, 1);
$newWidth = (int)($originalWidth * $ratio);
$newHeight = (int)($originalHeight * $ratio);
// =======================
// Create resized image
// =======================
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
// White background for PNG transparency
$white = imagecolorallocate($resizedImage, 255, 255, 255);
imagefill($resizedImage, 0, 0, $white);
imagecopyresampled(
$resizedImage,
$sourceImage,
0,
0,
0,
0,
$newWidth,
$newHeight,
$originalWidth,
$originalHeight
);
// =======================
// New filename
// =======================
$code = time();
$fileName = $idtrf . '-' . $code . '.jpg';
$targetFilePath = $targetDirPath . $fileName;
// =======================
// Save as JPG
// =======================
$saved = imagejpeg($resizedImage, $targetFilePath, $jpgQuality);
imagedestroy($sourceImage);
imagedestroy($resizedImage);
if (!$saved || !file_exists($targetFilePath)) {
http_response_code(500);
echo $msg['save_error'];
exit;
}
// =======================
// Delete previous photo if exists
// =======================
$oldPhoto = "";
$oldPhotoQuery = new WA_MySQLi_RS("oldPhotoQuery", $cmctrfdb, 1);
$oldPhotoQuery->setQuery("SELECT photofilename FROM `trf-details` WHERE idtrfdetails = $idtrf");
$oldPhotoQuery->execute();
$oldPhoto = $oldPhotoQuery->getColumnVal("photofilename");
if (!empty($oldPhoto)) {
$oldPhotoPath = $targetDirPath . basename($oldPhoto);
if (file_exists($oldPhotoPath) && $oldPhoto !== $fileName) {
unlink($oldPhotoPath);
}
}
// =======================
// Update database
// =======================
$UpdateQuery = new WA_MySQLi_Query($cmctrfdb);
$UpdateQuery->Action = "update";
$UpdateQuery->Table = "`trf-details`";
$UpdateQuery->bindColumn("photofilename", "s", "$fileName", "WA_DEFAULT");
$UpdateQuery->addFilter("idtrfdetails", "=", "i", "" . ($idtrf) . "");
$UpdateQuery->execute();
// Important: no redirect here because this is an AJAX upload
// =======================
// Return gallery HTML
// =======================
?>
<img src="<?php echo $targetDir . htmlspecialchars($fileName); ?>" alt="" class="uploaded-preview">