上传图片时调整图片大小

时间:2019-08-23 10:43:20

标签: php html css

有人可以帮我吗 我正在使用此代码调整图片大小,但上传的图片保持相同大小。我的意思是MB。为什么它不压缩呢? ini_set(“ error_reporting”,1);

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

if (isset($_POST['submit'])) {
    $name_img = $_FILES["file"]["name"];
    $source_img = $_FILES["file"]["tmp_name"];
    $destination_url = 'uploads/' . $name_img;

    move_uploaded_file($source_img, $destination_url);

    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"))
    {

    $fianl_file = compress($name_img, $destination_url, 50);
    $error = "Image Compressed successfully";

    }else {
        $error = "Uploaded image should be jpg or gif or png";
    }
}

1 个答案:

答案 0 :(得分:0)

https://github.com/diloabininyeri/Image-resize

我写的

使用作曲家安装

composer require zeus/image

使用


use Image\Image;


class test
{
    public function testResize()
    {


        $image = new Image();

        $image->load((string) "input.jpg")
            ->resize(800, 600)
            ->save("output.jpg", 80);


    }

}