在使用PHP上传到文件夹之前,如何压缩图像大小?

时间:2019-07-10 05:21:15

标签: php image compression

在将图像尺寸上传到文件夹之前,如何压缩图像尺寸。我的代码低于目前为止的代码,如何压缩图像?谢谢!

$temp = explode(".", $_FILES["Image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
echo $newfilename;

if (!!$_FILES['Image']['tmp_name']) {
    $info = explode('.', strtolower($_FILES['Image']['name'])); 
    if (in_array( end($info), $allow)) 
        if(move_uploaded_file( $_FILES['Image']['tmp_name'], $todir . $newfilename  ) )        {
          echo "success";
        }
    }
    else {
      echo "error";
    }

我找到了这段代码,但是不确定如何用我的代码实现。

function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

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

    return $destination;
}

$source_img = 'source.jpg';
$destination_img = 'destination .jpg';

$d = compress($source_img, $destination_img, 90);

1 个答案:

答案 0 :(得分:-1)