【问题标题】:Reduce Image size in php while uploading上传时减小php中的图像大小
【发布时间】:2015-07-24 17:38:34
【问题描述】:

PHP - 我正在使用此代码上传图像文件,但我不知道如何减小图像文件的大小,但应该保持质量。

$todir = 'members/'.$_SESSION[' valid_user '].'/';
$RandomNum   = rand(0, 9999999999);
$info = explode('.', strtolower( $_FILES['Image']['name']) ); // whats the extension of the file
 if(move_uploaded_file( $_FILES['Image']['tmp_name'], $todir . basename($_FILES['Image']['name']-$RandomNum) ) )
 {
 $handle= 'members/'.$_SESSION[' valid_user '].'/'.basename($_FILES['Image']['name']-$RandomNum);
 echo 'uploaded';
 }

【问题讨论】:

  • 质量应该保持是什么意思?如果你缩小尺寸,你会丢失一些信息。

标签: php image size reduce


【解决方案1】:

您是否尝试过使用 ImageMagick 将图像缩小到更合适的尺寸? (http://php.net/manual/en/imagick.setimagecompressionquality.php)

【讨论】:

    【解决方案2】:
         $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 = $_FILES["file"]["name"];
                $filename = compress_image($_FILES["file"]["tmp_name"], $url, 30);
                $buffer = file_get_contents($url);                             
            }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>
            <legend>Upload Image:</legend>
            <form action="" name="myform" id="myform" method="post" enctype="multipart/form-data">                
                <label>Upload:</label>
                <input type="file" name="file" id="file"/>          
                <input type="submit" name="submit" id="submit" class="submit btn-success"/>                    
            </form>
        </body>
    </html>
    

    【讨论】:

    • 请解释你的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2014-11-06
    • 2012-01-10
    • 2018-11-05
    • 2017-01-10
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多