getMessage()); header("Location: error.php?error=" . urlencode("System Error On Page Load")); } } // Iterate over the array and create directories if needed foreach ($directories as $directory) { if (!file_exists($directory)) { createDirectory($directory, $owner, $permissions); logMessage("$directory does not exist! Making it now!"); } } // Function to clean up old zip files function cleanupZipFiles($zip_dir) { $files = glob($zip_dir . "*.zip"); $now = time(); $one_minute_ago = $now - 60; // 60 seconds = 1 minute foreach ($files as $file) { if (filemtime($file) < $one_minute_ago) { unlink($file); logMessage("Removed old zip file: " . $file); } } } // Call the cleanup function at the start of the script $zip_dir = 'zips/'; // Assuming this is your zip directory global $zip_dir; global $upload_dir; global $converted_dir; global $allowed_formats; $zip_dir='zips/'; $upload_dir='uploads/'; $converted_dir='converted/'; function purgeOldZipFiles() { logMessage("Purging Old Zips from the Server!"); $directory = '/var/www/html/convert/zips/'; $maxAgeMinutes = 15; $now = time(); // Get all files in the directory $files = scandir($directory); // Iterate through the files foreach ($files as $file) { // Skip "." and ".." if ($file == '.' || $file == '..') { continue; } // Check if it's a zip file if (substr($file, -4) == '.zip') { $filePath = $directory . $file; // Get the last modification time of the file $lastModified = filemtime($filePath); // Check if it's older than the specified max age if ($now - $lastModified > $maxAgeMinutes * 60) { // Delete the file unlink($filePath); } } } } // Call the function when index.php loads purgeOldZipFiles(); $user_files = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $resp = ''; $format = $_POST['format']; logMessage("In POST"); ob_start(); // Start output buffering logMessage("Creating new zip file"); $zip = new ZipArchive(); $zip_path = 'zips/' . time() . '.zip'; // Full path to the zip file if ($zip->open($zip_path, ZipArchive::CREATE) !== TRUE) { header("Location: error.php?error=" . urlencode("Zip Creation Failed!")); } foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) { logMessage("In foreach loop processing images!"); $uploadedFile = [ 'name' => $_FILES['images']['name'][$key], 'type' => $_FILES['images']['type'][$key], 'tmp_name' => $_FILES['images']['tmp_name'][$key], 'error' => $_FILES['images']['error'][$key], 'size' => $_FILES['images']['size'][$key] ]; $file_name = $uploadedFile['name']; $file_tmp = $uploadedFile['tmp_name']; $allowed_formats = ['jpg', 'png', 'bmp', 'gif', 'ico']; $fileExtension = strtolower(pathinfo($uploadedFile['name'], PATHINFO_EXTENSION)); if (!in_array($fileExtension, $allowed_formats)) { logMessage("File type not allowed!"); header("Location: error.php?error=" . urlencode("File Type Not Allowed For " . $fileExtension)); } // Validate file if (!is_uploaded_file($file_tmp)) { logMessage("Error uploading file! " . $file_tmp); header("Location: error.php?error=" . urlencode("File Upload Failed For " . $file_tmp)); } // Validate image if (!getimagesize($file_tmp)) { logMessage("Invalid image file! " . $file_tmp); header("Location: error.php?error=" . urlencode("Invalid Image File " . $file_tmp)); } // Error handling for adding files to zip // Move uploaded file to upload directory move_uploaded_file($file_tmp, $upload_dir.$file_name); logMessage("Moved uploaded file!"); // Convert image and save to converted directory convertImage($upload_dir.$file_name, $converted_dir.pathinfo($file_name, PATHINFO_FILENAME) . '.' . $format, $format); logMessage("Converted image to new format!"); // Add converted image to zip $local_file_name = pathinfo($file_name, PATHINFO_FILENAME) . '.' . $format; // Construct local file name $zip->addFile($converted_dir . $local_file_name, $local_file_name); // Add with local name logMessage("Image added to zip file!"); // Add uploaded and converted files to user files array array_push($user_files, $upload_dir.$file_name); array_push($user_files, $converted_dir.pathinfo($file_name, PATHINFO_FILENAME) . '.' . $format); } // Close the zip file after all files have been added $zip->close(); // Construct the download button HTML in $resp $resp .= ''; ob_end_clean(); cleanupFiles($user_files); logMessage("Removing users files from server!"); } function convertImage($source, $destination, $format) { $allowedFormats = ['jpg', 'jpeg', 'png', 'gif', 'bmp']; $imageInfo = getimagesize($source); global $allowed_formats; if (!$imageInfo) { header("Location: error.php?error=" . urlencode("Invalid image file: " . $source)); exit; } $originalFormat = strtolower($imageInfo[2]); $originalFormat = image_type_to_extension($originalFormat, false); if (!in_array($originalFormat, $allowedFormats)) { header("Location: error.php?error=" . urlencode("Unsupported image format: " . $originalFormat)); exit; } if (!in_array($format, $allowedFormats)) { header("Location: error.php?error=" . urlencode("Invalid target format: " . $format)); exit; } try { $image = new Imagick($source); $image->setImageFormat($format); file_put_contents($destination, $image); logMessage("Image converted from $originalFormat to $format!"); } catch (ImagickException $e) { header("Location: error.php?error=" . urlencode("Error converting image: " . $e->getMessage())); exit; } } function cleanupFiles($files) { foreach ($files as $file) { unlink($file); logMessage("Removed file " . $file); } logMessage("Source files deleted!"); } ?> Image Converter

Welcome to the Image Format Converter

Currently converts BMP, JPG, PNG, GIF, and ICO Images.