From 29620ea48beef0937da764a75d6284cf574e9d7b Mon Sep 17 00:00:00 2001 From: Fascinated Date: Thu, 6 Jul 2023 02:28:20 +0100 Subject: [PATCH 1/2] add imagick --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a263f60..b4beec2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM ubuntu:23.04 # Install dependencies RUN apt update RUN DEBIAN_FRONTEND=noninteractive \ -apt install nginx php8.1 php8.1-fpm php8.1-gd -y +apt install nginx php8.1 php8.1-fpm php8.1-gd php8.1-imagick -y # Clean up RUN apt clean From 49499b8013ded9dfc66fccf9b0bd38295a49729a Mon Sep 17 00:00:00 2001 From: Fascinated Date: Thu, 6 Jul 2023 02:34:51 +0100 Subject: [PATCH 2/2] use imagick for image conversion (should be a bit faster) --- upload.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/upload.php b/upload.php index 22822fe..6f51731 100644 --- a/upload.php +++ b/upload.php @@ -136,15 +136,28 @@ try { $needsToBeSaved = true; // Whether the file needs to be saved - 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; - } + // Check the file type and size + if ($shouldConvertToWebp && in_array($fileType, ["png", "jpeg", "jpg"]) && $_FILES["sharex"]["size"] > $webpThreadhold) { + // Create an Imagick object from the uploaded file + $image = new Imagick($_FILES["sharex"]["tmp_name"]); + + // Convert the image to WebP + $image->setImageFormat("webp"); + $image->setImageCompressionQuality($webpQuality); + + // Set the output filename + $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; + + // Save the converted image + $image->writeImage($webp_file); + + // Free up memory + $image->clear(); + $image->destroy(); + + // Update the final filename + $finalName = $webp_file; + $needsToBeSaved = false; } if ($needsToBeSaved) { // Save the file if it has not been saved yet