From bd6b581e9496de9825e67092995547fb62642562 Mon Sep 17 00:00:00 2001 From: Fascinated Date: Wed, 12 Apr 2023 03:09:53 +0100 Subject: [PATCH] cleanup code --- upload.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/upload.php b/upload.php index 6361f8d..83c017a 100644 --- a/upload.php +++ b/upload.php @@ -1,7 +1,6 @@ $webpThreadhold && $shouldConvertToWebp) { - $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); - $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; - imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it - imagedestroy($image); // Free up memory - $finalName = $webp_file; - $saved = true; + if ($shouldConvertToWebp) { // Convert the image to webp if applicable + if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold) { + $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); + $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; + imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it + imagedestroy($image); // Free up memory + $finalName = $webp_file; + $needsToBeSaved = false; + } } - if (!$saved) { // If the file wasn't saved (e.g. webp conversion) + if ($needsToBeSaved) { // Save the file if it has not been saved yet // Move the file to the uploads folder $success = move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName); if (!$success) {