【问题标题】:PHP Image Compression then UploadPHP图像压缩然后上传
【发布时间】:2015-12-27 19:40:15
【问题描述】:

我拥有的以下代码是来自两个来源的组合。

图片上传: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. 将未压缩的原始图像上传到服务器

我想要做的是压缩上传并发送到服务器。我不需要它下载。我只是无法将这两个功能集成在一起。

<?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>

【问题讨论】:

    标签: php image file-upload compression


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您负责下载。但压缩变量与上传变量不同。我应该如何将压缩集成到上传功能中?现在它将解压缩上传到服务器。
    • 在文件到达服务器之前,您无法压缩文件。
    • 所以你是说这段代码行不通?如果没有,你能建议我应该怎么做吗?
    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2018-07-27
    相关资源
    最近更新 更多