上传时无法减小 php 中的图像文件大小

Unable to reduce image file size in php while uploading

我下面的代码可以上传多张图片并在上传时重命名它们并且它有效 fine.Then 我试图实现 image compress 但这不起作用,有人可以帮助我解决这个问题。

无压缩方式

if (!empty($_POST)) {
    $newname = md5(rand() * time());
    if (isset($_FILES['files'])) {
        $uploadedFiles = array();
        foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
            $errors = array();
            $file_name = md5(uniqid("") . time());
            $file_size = $_FILES['files']['size'][$key];
            $file_tmp = $_FILES['files']['tmp_name'][$key];
            $file_type = $_FILES['files']['type'][$key];
            if ($file_type == "image/gif") {
                $sExt = ".gif";
            } elseif ($file_type == "image/jpeg" || $file_type == "image/pjpeg") {
                $sExt = ".jpg";
            } elseif ($file_type == "image/png" || $file_type == "image/x-png") {
                $sExt = ".png";
            }
            if (!in_array($sExt, array('.gif', '.jpg', '.png'))) {
                $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
            }
            if ($file_size > 2097152000) {
                $errors[] = 'File size must be less than 2 MB';
            }
            $desired_dir = "upload/";
            if (empty($errors)) {
                if (is_dir($desired_dir) == false) {
                    mkdir("$desired_dir", 0700);
                }
                if (move_uploaded_file($file_tmp, "$desired_dir/" . $file_name . $sExt)) {
                    $uploadedFiles[$key] = array($file_name . $sExt, 1);
                } else {
                    echo "Couldn't upload file " . $_FILES['files']['name'][$key];
                    $uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
                }
            } else {

            }
        }
        foreach ($uploadedFiles as $key => $row) {
            if (!empty($row[1])) {
                $codestr = '$file' . ($key + 1) . ' = $row[0];';
                eval($codestr);
            } else {
                $codestr = '$file' . ($key + 1) . ' = NULL;';
                eval($codestr);
            }
        }
    }

采用压缩方式

if (!empty($_POST)) {
    $newname = md5(rand() * time());
    if (isset($_FILES['files'])) {
        $uploadedFiles = array();
        foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
            $errors = array();
            $file_name = md5(uniqid("") . time());
            $file_size = $_FILES['files']['size'][$key];
            $file_tmp = $_FILES['files']['tmp_name'][$key];
            $file_type = $_FILES['files']['type'][$key];
            if ($file_type == "image/gif") {
                $sExt = ".gif";
            } elseif ($file_type == "image/jpeg" || $file_type == "image/pjpeg") {
                $sExt = ".jpg";
            } elseif ($file_type == "image/png" || $file_type == "image/x-png") {
                $sExt = ".png";
            }
            if (!in_array($sExt, array('.gif', '.jpg', '.png'))) {
                $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
            }
            if ($file_size > 2097152000) {
                $errors[] = 'File size must be less than 2 MB';
            }
            $desired_dir = "upload/";
            if (empty($errors)) {
                if (is_dir($desired_dir) == false) {
                    mkdir("$desired_dir", 0700);
                }
                if (move_uploaded_file($file_tmp, "$desired_dir/" . $file_name . $sExt)) {
                    $uploadedFiles[$key] = array($file_name . $sExt, 1);
                } else {
                    echo "Couldn't upload file " . $_FILES['files']['name'][$key];
                    $uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
                }
            } else {

            }
        }

function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') {
                $image = imagecreatefromjpeg($source);
            } elseif ($info['mime'] == 'image/gif') {
                $image = imagecreatefromgif($source);
            } elseif ($info['mime'] == 'image/png') {
                $image = imagecreatefrompng($source);
            }

            imagejpeg($image, $destination, $quality);

    return $destination;
}
$source_img = $uploadedFiles;
$destination_img = 'compres/';

$d = compress($source_img, $destination_img, 60);

        foreach ($uploadedFiles as $key => $row) {
            if (!empty($row[1])) {
                $codestr = '$file' . ($key + 1) . ' = $row[0];';
                eval($codestr);
            } else {
                $codestr = '$file' . ($key + 1) . ' = NULL;';
                eval($codestr);
            }
        }
    }

您的代码存在的问题是 $file_name 对于所有图像都是相同的。你也没有得到正确的图像资源。这是工作解决方案:

<?php 
if (!empty($_POST)) {
    $newname = md5(rand() * time());
    if (isset($_FILES['files'])) {
        $uploadedFiles = array();
        foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
            $errors = array();
            $file_name = md5(uniqid("") . time());
            sleep(1);//We give a second for breathing
            $file_size = $_FILES['files']['size'][$key];
            $file_tmp = $_FILES['files']['tmp_name'][$key];
            $file_type = $_FILES['files']['type'][$key];
            $filename = $_FILES["files"]["name"][$key];

            if ($file_type == "image/gif") {
                $sExt = ".gif";
            } elseif ($file_type == "image/jpeg" || $file_type == "image/pjpeg") {
                $sExt = ".jpg";
            } elseif ($file_type == "image/png" || $file_type == "image/x-png") {
                $sExt = ".png";
            }
            if (!in_array($sExt, array('.gif', '.jpg', '.png'))) {
                $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
            }
            if ($file_size > 2097152000) {
                $errors[] = 'File size must be less than 2 MB';
            }
            $desired_dir = "upload/";
            if (empty($errors)) {
                if (is_dir($desired_dir) == false) {
                    mkdir("$desired_dir", 0700);
                }
                if (move_uploaded_file($file_tmp, $desired_dir . $file_name . $sExt)) {
                    $uploadedFiles[$key] = array($file_name . $sExt, 1);
                } else {
                    echo "Couldn't upload file " . $_FILES['files']['name'][$key];
                    $uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
                }
            } else {
                print_r($errors);
            }
        }        

function compress($filename, $destination, $quality) {        
            $info = getimagesize($filename);
            if ($info['mime'] == 'image/jpeg') {
                $image = imagecreatefromjpeg($filename);
            } elseif ($info['mime'] == 'image/gif') {
                $image = imagecreatefromgif($filename);
            } elseif($info['mime'] == 'image/png') {
                $image = imagecreatefrompng($filename);
            }else{
                $image =null;
            }                    
        return imagejpeg($image, $destination, $quality);
}
$compressed = 0;
$destination_img = 'compressed/';
foreach($uploadedFiles as $km=>$val){ 
   $photos = $desired_dir . $val[0];   
   $compressed += compress($photos, $destination_img.$val[0], 60);   
}
    if($compressed>=1){
        echo $compressed.' Images compresses';
    }else{
        echo 'No Image Compressed';
    }       
    }
}
    ?>
<form method="POST" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="file" name="files[]">File 1<br>
    <input type="file" name="files[]">File 2<br>
    <input type="file" name="files[]">File 3<br>
    <input type="submit" name="filesubmit" value="Submit">
</form>

查看以下链接以更好地了解图像处理和生成的方式:

  1. imagejpeg()
  2. getimagesize()

希望我的回答对您有所帮助