如何在上传前使用 php 压缩图片大小

How to compress image size before uploading using php

我已经搜索了很多东西,其中一些来自 Whosebug。但是 none 他们帮助了我。我想要做的是减小图像文件的大小,并且在减小图像后现在将被上传。这是我当前的代码:

<?php

include 'cloud_functions.php';


// GET THE USER'S ID
$userid = _clean($con, $_POST['userid']);


if(!is_dir("uploads/user/".$userid."/")){
        mkdir("uploads/user/".$userid."/", 0755);
}


$temp = explode(".", _clean($con,$_FILES["file"]["name"]));


$targetPath = "uploads/user/".$userid."/";
// RENAME THE IMAGE USING ROUND();
$newFN = round(microtime(true)) . '.' . end($temp);
$targetPath = $targetPath .  $newFN;
// GET THE FILE EXTENSION
$type = pathinfo(_clean($con, $_POST['filename']), PATHINFO_EXTENSION);

$all_types = array('jpg', 'png', 'jpeg');
$type = strtolower($type);

if(in_array($type, $all_types)){

    if(move_uploaded_file($_FILES['file']['tmp_name'], $targetPath)){
        // image uploaded w/o compressing size
        echo "1/".$newFN;
    }else{
        echo $targetPath;
        echo "There was an error uploading the file, please try again!";
    }

}else{
    echo "Please upload a valid image.";
}


?>

这样我就可以在不压缩图片的情况下成功上传图片了。请告诉我如何在上传前压缩图像大小。谢谢

https://github.com/brunobar79/J-I-C - js 库

我认为更好的方法是上传后在服务器上压缩图像,以减少使用用户内存。这对于速度较慢的智能手机尤为重要。

您需要在客户端压缩图像。您在此处发布的所有代码都是服务器端的。此代码将在将图像上传到服务器后 运行。 为此有很多客户端库。 This link 将进一步提供帮助。您可以选择使用您选择的任何库。