PHP - 上传时压缩图像

时间:2016-08-02 18:02:28

标签: php html image post compression

我有一些我的网站,用户可以将图像上传到服务器。但是,图像大约为5MB,这将加快速度。如何在上传时压缩这些图像,使它们至少小一点,而不会损失太多质量?

这是我的上传代码:

if (isset($_FILES['image'])) {

        $file_size = $_FILES['image']['size'];
        $file_tmp = $_FILES['image']['tmp_name'];
        $file_type = $_FILES['image']['type'];
        $file_ext = strtolower(end(explode('.', $_FILES['image']['name'])));
        $file_name = str_replace(" ", "-", strtolower($_POST['title'])) . "." . $file_ext;

        $extensions = array("jpeg", "jpg", "png");

        if (!in_array($file_ext, $extensions)) {
            $errors[] = "Extension not allowed, please choose a JPEG or PNG file.";
        }

        if (empty($errors)) {
            // $uploaded = move_uploaded_file($file_tmp, "../../uploads/" . $file_name);
            $uploaded = compress_image($file_tmp, "../../uploads/" . $file_name, 80);
        }
    } 

0 个答案:

没有答案