Switch image conversion to Imagick #3
31
upload.php
31
upload.php
@ -136,15 +136,28 @@ try {
|
|||||||
|
|
||||||
$needsToBeSaved = true; // Whether the file needs to be saved
|
$needsToBeSaved = true; // Whether the file needs to be saved
|
||||||
|
|
||||||
if ($shouldConvertToWebp) { // Convert the image to webp if applicable
|
// Check the file type and size
|
||||||
if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold) {
|
if ($shouldConvertToWebp && in_array($fileType, ["png", "jpeg", "jpg"]) && $_FILES["sharex"]["size"] > $webpThreadhold) {
|
||||||
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
// Create an Imagick object from the uploaded file
|
||||||
$webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp";
|
$image = new Imagick($_FILES["sharex"]["tmp_name"]);
|
||||||
imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it
|
|
||||||
imagedestroy($image); // Free up memory
|
// Convert the image to WebP
|
||||||
$finalName = $webp_file;
|
$image->setImageFormat("webp");
|
||||||
$needsToBeSaved = false;
|
$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
|
if ($needsToBeSaved) { // Save the file if it has not been saved yet
|
||||||
|
Loading…
Reference in New Issue
Block a user