PHP减少了图像上的文件大小

时间:2015-06-29 02:41:18

标签: php html

<html>
<head>
</head>

<body>
<form action="tutorial.php" method="post" enctype="multipart/form-data">
<b><u>UPLOAD FILE</u></b><br />
Attachment :  <input type="file" name="file" /><br />
<input type="submit" name="submit" value="Upload" /><br />
<hr />
</body>

<?php
function compress_image($source, $destination, $quality)
{
    $info = getimagesize($source);

    if($info['mime'] == 'image/jpeg' || $info['mime'] == "image/jpg") 
    {
        $image = imagecreatefromjpeg($source);
    }
    else if($info['mime'] == 'image/gif') 
    {
        $image = imagecreatefromgif($source);
    }       
    else if($info['mime'] == 'image/png') 
    {
        $image = imagecreatefrompng($source);
    }

    imagejpeg($image, $destination, $quality);
    return $destination;
}

if(isset($_POST['submit']) && $_POST['submit'] == 'Upload')
{
    $type = $_FILES['file']['type'];
    $tmp_name = $_FILES['file']['tmp_name'];
    $name = $_FILES['file']['name'];
    $size = $_FILES['file']['size'];

    if(($type == "image/jpg") || ($type == "image/jpeg") || ($type == "image/gif") || ($type == "image/png"))
    {
        $source = $tmp_name;
        $destination = "destination.jpg";

        echo 'File size (before): '.$size.'<br />';

        $filename = compress_image($source, $destination, 75);

        echo 'File size (after): '.filesize($destination).'<br />';
    }   
    else
    {
        echo "Invalid file size. Only jpg, jpeg, gif, png allowed.";
    }
} 
?>
  

从上面的代码中,我上传jpeg文件后得到以下输出:

Original file size: 7034 
Compress file size: 7092

我真的不知道为什么压缩后会增加?如果我上传大文件大小,文件大小将减少。当我上传一个小文件大小时,文件大小会增加。我该怎么办?

0 个答案:

没有答案