$name) {
$size = $_FILES['gallery_pic']['size'][$key];
$temp_name = $_FILES['gallery_pic']['tmp_name'][$key];
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if ($size < $max_size) {
// Generate a unique name for the image
$image_name = uniqid() . '.webp';
$upload_path = $image_path . $image_name;
// Check the image type and create an image resource from the uploaded file
switch ($extension) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($temp_name);
break;
case 'png':
$image = imagecreatefrompng($temp_name);
break;
case 'gif':
$image = imagecreatefromgif($temp_name);
break;
default:
$message = "Unsupported image format for file: $name";
$success = false;
continue 2; // Skip this file and continue with the next one
}
// Convert the image to WebP format and save it
if ($image) {
if (imagewebp($image, $upload_path)) {
imagedestroy($image); // Free up memory
// Image successfully converted and saved as WebP, now store the new image name in the database
$query = "INSERT INTO `image`(`g_image`) VALUES ('$image_name')";
$result = mysqli_query($con, $query);
if (!$result) {
$message = "Failed to add image: $name";
$success = false;
} else {
$success = true; // Set success flag to true if any image is successfully added
}
} else {
$message = "Failed to convert and save the image: $name";
$success = false;
}
} else {
$message = "Failed to create image resource for file: $name";
$success = false;
}
} else {
$message = "File size exceeds the maximum limit of 5MB for file: $name";
$success = false;
}
}
if ($success) {
$message = "Gallery added successfully!";
}
}
include('includes/header.php');
include('includes/sidebar.php');
?>