在多个文件上传时压缩图像文件大小


compressing image filesize on multiple file upload

我使用以下内容作为html输入:

<input type="file" name="files[]" multiple="multiple" accept="image/*">

然后上传上述输入的多个实例,并在每个文件检查错误后尝试通过更改图像质量进行压缩:

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*300; //300 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        // Loop $_FILES to execute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {

// Compress the image files
function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
    // save file
    imagejpeg($image, $destination_url, $quality);
    // return destination file
    return $destination_url;
}
                compress_image($_FILES['files']['name'], NULL, 90);
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) {
                    $count++; // Number of successfully uploaded files
                }
            }
        }
    }
}

文件上传得很好,但没有进行压缩。如何在上传或每个文件时压缩图像文件大小?

正如我的评论中所提到的,您在错误4中调用compress_image,这对您没有多大好处,因为只有在没有上传图像的情况下才会出现这种情况。

此外,您没有为compress_image函数调用指定目的地。您传递NULL,如果浏览器已经实际运行,它将实际向浏览器发送压缩图像。

这个函数的例子虽然不完整,但脚本假设您想要丢弃大于最大大小的图像。任何较小的文件都以90质量重新保存为jpegs。

<?php
//I removed the zip entry as you don't have any code to handle them here.
$valid_formats = array("jpg", "png", "gif");
//Edit: compress_image doesn't handle bmp files either, though it would
//easy enough to add with another elseif.
$max_file_size = 1024*300; //300 kb
$path = "uploads/"; // Upload directory
$count = 0;
// Compress the image files
function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
    // save file
    imagejpeg($image, $destination_url, $quality);
    // return destination file
    return $destination_url;
}
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        // Loop $_FILES to execute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] != 0) {
            continue; // Skip file if any error found
        }
        if ($_FILES['files']['error'][$f] == 0) {
            if ($_FILES['files']['size'][$f] > $max_file_size ) {
                $message[] = "$name is too large!";
                continue; // Skip large files.
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                //All smaller files to be compressed.
                if(is_uploaded_file($_FILES["files"]["tmp_name"][$f])) {
                    //Add a '.jpg' to the name because I'm lazy.
                    compress_image($_FILES["files"]["tmp_name"][$f], $path.basename($name).'.jpg', 90);
                    $count ++; // Number of successfully uploaded files
                }
            }
        }
    }
}