douhoushou8385 2013-12-01 17:33
浏览 45

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

I am using the following as the html input:

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

then this to upload multiple instances of the above input and attempt to compress by changing the image quality after each file is checked for errors:

$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
                }
            }
        }
    }
}

The files are uploaded just fine, however the compression doesn't take place. How can I compress the image filesize upon upload or each file?

  • 写回答

1条回答 默认 最新

  • douchen7366 2013-12-01 18:54
    关注

    As mentioned in my comment, you're calling compress_image in error 4, which won't do you much good as that's only the case when there's no image uploaded.

    Additionally, you don't give your compress_image function call a destination. You pass NULL which would actually send the compressed image to the browser if it had actually run.

    This example of a functioning, though incomplete, script assumed that you wanted to discard images that were larger than the maxsize. Any files smaller were re-saved as jpegs at 90 quality.

    <?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
                    }
                }
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度