为什么在php上传...增加文件大小?

时间:2014-09-06 10:19:47

标签: php

我有图片和视频的上传器.. 我尝试上传52个图像文件,大小为1.38 Mb 但是当我在服务器上检查它的尺寸时,我发现它是5.2 Mb .. 这是php uploader的代码..我能知道哪里有问题吗?

<?php
    if (isset($_FILES['upload_img'])) {
        $uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/upload/img/'; //path you wish to store you uploaded files
        echo '<h2>Image(s) Uploaded</h2><ul style="overflow: scroll;height: 200px;">';
        //Loop through each file
        for($i = 0; $i < count($_FILES['upload_img']['name']); $i++) {
            //Get the temp file path
            $tmpFilePath = $_FILES['upload_img']['tmp_name'][$i];

            //Make sure we have a filepath
            if ($tmpFilePath != ""){
                //Setup our new file path
                $uploadedFile = $uploadDir . basename($_FILES['upload_img']['name'][$i]);

                //Upload the file into the temp dir
                $dir = pathinfo($uploadedFile,PATHINFO_DIRNAME);
                $actual_name = pathinfo($uploadedFile,PATHINFO_FILENAME);
                $original_name = $actual_name;
                $extension = pathinfo($uploadedFile, PATHINFO_EXTENSION);
                $j = 1;
                while(file_exists($uploadedFile))
                {           
                    $actual_name = (string)$original_name.$j;
                    $uploadedFile = $dir."/".$actual_name.".".$extension;
                    $j++;
                }

                $size = getimagesize($tmpFilePath);
                $ratio = $size[0]/$size[1]; // width/height

                if( $ratio > 1) {
                    $width = 300;
                    $height = 300/$ratio;
                }
                else {
                    $width = 300*$ratio;
                    $height = 300;
                }
                $src = imagecreatefromstring(file_get_contents($tmpFilePath));
                $dst = imagecreatetruecolor($width,$height);
                imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
                imagedestroy($src);
//              if(move_uploaded_file($tmpFilePath, $uploadedFile)) {               
                if(imagepng($dst,$uploadedFile)) {
                    $fpath = '/upload/img/' . $actual_name.".".$extension;
                    echo '<li style="list-style:none">' . $fpath . '</li>';
                    //save to db
                    save_db_img($fpath, $_POST['cat_value_img']);
                } else {
                    echo '<h2>There was a problem saving the uploaded file</h2>';
                }
                imagedestroy($dst);
            }
        }
        echo '</ul>';
    }

    if (isset($_FILES['upload_video'])) {
        $uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/upload/video/'; //path you wish to store you uploaded files
        echo '<h2>Video(s) Uploaded</h2><ul style="overflow: scroll;height: 200px;">';
        //Get the temp file path
        $tmpVideoPath = $_FILES['upload_video']['tmp_name'];
        $tmpImgPath = $_FILES['upload_timg']['tmp_name'];

        //Setup our new file path
        $uploadedVideoFile = $uploadDir . basename($_FILES['upload_video']['name']);
        $uploadedImgFile = $uploadDir . basename($_FILES['upload_timg']['name']);

        $dir = pathinfo($uploadedVideoFile,PATHINFO_DIRNAME);
        $actual_name = pathinfo($uploadedVideoFile,PATHINFO_FILENAME);
        $original_name = $actual_name;
        $extension = pathinfo($uploadedVideoFile, PATHINFO_EXTENSION);
        $j = 1;
        while(file_exists($uploadedVideoFile))
        {           
            $actual_name = (string)$original_name.$j;
            $uploadedVideoFile = $dir."/".$actual_name.".".$extension;
            $j++;
        }

        $dir_img = pathinfo($uploadedImgFile,PATHINFO_DIRNAME);
        $actual_name_img = pathinfo($uploadedImgFile,PATHINFO_FILENAME);
        $original_name_img = $actual_name_img;
        $extension_img = pathinfo($uploadedImgFile, PATHINFO_EXTENSION);
        $k = 1;
        while(file_exists($uploadedImgFile))
        {           
            $actual_name_img = (string)$original_name_img.$k;
            $uploadedImgFile = $dir."/".$actual_name_img.".".$extension_img;
            $k++;
        }

        //Upload the file into the temp dir
        if(move_uploaded_file($tmpVideoPath, $uploadedVideoFile)) {             
            $fvideo_path = '/upload/video/' . $actual_name.".".$extension;
            echo '<li style="list-style:none">' . $fvideo_path . '</li>';

                $size = getimagesize($tmpImgPath);
                $ratio = $size[0]/$size[1]; // width/height

                if( $ratio > 1) {
                    $width = 300;
                    $height = 300/$ratio;
                }
                else {
                    $width = 300*$ratio;
                    $height = 300;
                }
                $src = imagecreatefromstring(file_get_contents($tmpImgPath));
                $dst = imagecreatetruecolor($width,$height);
                imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
                imagedestroy($src);
                if(imagepng($dst,$uploadedImgFile)) {
                    $fimg_path = '/upload/video/' . $actual_name_img.".".$extension_img;
                    echo '<li style="list-style:none"> with Image Title of ' . $fimg_path . '</li>';
                }
                imagedestroy($dst);
            //save to db
            save_db_video($fvideo_path, $fimg_path, $_POST['cat_value_video'], $_POST['video_title']);
        } else {
            echo '<h2>There was a problem saving the uploaded file</h2>';
        }

        echo '</ul>';

    }

    echo '<button class="btn_back"><a href="members.php"><h2>Back to Uploader</h2></a></button>';

1 个答案:

答案 0 :(得分:0)

尝试在创建新PNG时调用imagepng时设置压缩级别并使用filters

imagepng($dst, $uploadedFile, 9, PNG_ALL_FILTERS)

旧图像可能比您正在创建的新图像压缩得更多,因此它更大。原始图像也是PNG吗?如果他们是另一种格式,特别是非无格式,您可能无法阻止文件大小的增加。