php - 上传图片时调整图片大小

标签 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";
    }
}

最佳答案

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

我写的

用 Composer 安装

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


    }

}

关于php - 上传图片时调整图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57624664/

相关文章:

php - 过程函数发生相反的结果

javascript - 为什么当我尝试通过添加类来使用 jQuery 隐藏我的 CSS 三 Angular 形时它不起作用?

CSS位置问题

css - li 水平悬停整条线

PHP array_unique 没有索引

PHP 创建受密码保护的文件夹

java - 用于从字符串中删除 HTML 标记的正则表达式

html - 如何使表格变小并将表格放在中间

css - IE7 : background-img on multipleline text

php - 来自 MySQL 数据库的特殊字符的 json_encode