PHP图像压缩然后上传

时间:2015-12-27 19:40:16

标签: php image file-upload compression

以下代码是两个来源的组合。

图片上传:http://www.w3schools.com/php/php_file_upload.asp 图片压缩:https://www.apptha.com/blog/how-to-reduce-image-file-size-while-uploading-using-php-code/

图像上传功能可直接上传到服务器。压缩之一压缩图像然后下载它。我正在尝试将此上传功能集成到我的上传功能中,因为它已嵌入我们的网站。

以下代码执行两项操作:

  1. 压缩上传和下载
  2. 将原始图像上传到未压缩的服务器
  3. 我想要做的是压缩上传并发送到服务器。我不需要下载。我只是将这两个功能集成在一起时遇到了麻烦。

    <?php 
        $name = ''; $type = ''; $size = ''; $error = ''; 
        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); 
            imagejpeg($image, $destination_url, $quality); 
            return $destination_url; 
        } 
    if ($_POST) { 
        if ($_FILES["file"]["error"] > 0) { 
            $error = $_FILES["file"]["error"]; 
        } 
        else if (($_FILES["file"]["type"] == "image/gif") || 
                 ($_FILES["file"]["type"] == "image/jpeg") || 
                 ($_FILES["file"]["type"] == "image/png") || 
                 ($_FILES["file"]["type"] == "image/pjpeg")) { 
            $url = 'destination .jpg'; 
    
            $filename = compress_image($_FILES["file"]["tmp_name"], $url, 80); 
            $buffer = file_get_contents($url); 
    
            /* Force download dialog... */
            header("Content-Type: application/force-download"); 
            header("Content-Type: application/octet-stream"); 
            header("Content-Type: application/download");
    
            /* Don't allow caching... */ 
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    
            /* Set data type, size and filename */ 
            header("Content-Type: application/octet-stream"); 
            header("Content-Transfer-Encoding: binary"); 
            header("Content-Length: " . strlen($buffer)); 
            header("Content-Disposition: attachment; filename=$url"); 
    
            /* Send our file... */ 
            echo $buffer; 
    //**The following is the image upload to the server**********
    $target_dir = "newuploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);{
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "";}
        }
    }
    
        else { 
            $error = "Uploaded image should be jpg or gif or png"; 
        } 
    } 
    ?> 
    <html> 
        <head> 
            <title>Php code compress the image</title> 
        </head> 
    
        <body> 
            <div class="message"> 
                <?php 
                if($_POST){ 
                    if ($error) { 
                ?> 
                <label class="error"><?php echo $error; ?></label> 
                <?php 
                    } 
                } 
                ?> 
            </div> 
            <fieldset class="well"> 
                <legend>Upload Image:</legend> 
                <form action="" name="myform" id="myform" method="post" enctype="multipart/form-data"> 
                    <ul> 
                        <li> 
                            <label>Upload:</label> 
                            <input type="file" name="file" id="file"/> 
                        </li> 
                        <li> 
                            <input type="submit" name="submit" id="submit" class="submit btn-success"/> 
                        </li> 
                    </ul> 
                </form> 
            </fieldset> 
        </body> 
    </html>
    

1 个答案:

答案 0 :(得分:0)

您需要做的就是删除将该数据抛回浏览器的代码部分,然后删除

    $buffer = file_get_contents($url); 

    /* Force download dialog... */
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download");

    /* Don't allow caching... */ 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

    /* Set data type, size and filename */ 
    header("Content-Type: application/octet-stream"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: " . strlen($buffer)); 
    header("Content-Disposition: attachment; filename=$url"); 

    /* Send our file... */ 
    echo $buffer;