[在PHP OOP中上传期间压缩png和jpg图像

问题描述 投票:0回答:2

我有一个电子商务网站,在“用户”系统中,当用户上传/更新他/她的个人资料图片时,应按以下方式进行压缩:如果实际图像大小为1MB,则上传后应进行压缩并保存,最小为50kb,最大为200kb,我就是这样,这是我的代码:

public function updateProfilePic($file, $userid) {
    $filename = $file['user_img']['name'];
    $filetmp = $file['user_img']['tmp_name'];
    $valid_ext = array('png', 'jpeg', 'jpg');
    $location = "user/profilepic/" . $filename;
    $file_extension = pathinfo($location, PATHINFO_EXTENSION);
    $file_extensionstr = strtolower($file_extension);

    if(!empty($filename)){
        if (in_array($file_extensionstr, $valid_ext)) {
            //Here i am compressing image
            $this->compressImage($filetmp, $location, 9);
            return $this->updateProfilePicture($filename, $userid);
        } else {
            $msg = '<div class="alert alert-danger" role="alert">Invalid file type. You can upload only:-' . implode(', ', $valid_ext) . '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
            return $msg;
        }
    } else {
        $msg = '<div class="alert alert-danger" role="alert">Please upload your profile picture.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
        return $msg;
    }
}

public function updateProfilePicture($filename, $userid){
    $update = "UPDATE users SET user_img = '$filename' WHERE user_id = '$userid'";
    $result = $this->db->update($update);
    if($result){
        $msg = '<div class="alert alert-success" role="alert">Profile picture uploaded successfully <a href="profile.php">Go back</a><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
        return $msg;
    } else {
        $msg = '<div class="alert alert-danger" role="alert">Error while uploading profile picture. Pleas try again!<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
        return $msg;
    }
}

public function compressImage($source, $destination, $quality) {
    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg'){
        $image = imagecreatefromjpeg($source);
    } elseif ($info['mime'] == 'image/png'){
        $image = imagecreatefrompng($source);
        imagealphablending($image, false);
        imagesavealpha($image, true);
    }

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

}

[您可以看到我的compressImage()函数,如果我编写了imagejpeg()函数,则它可以压缩得很好,但是在png(透明)图像中,它以黑色背景保存。因此,我替换为imagepng()函数,但是它并没有压缩图像,而是保存到Database后增加了图像的大小,就像我的图像大小为2 MB,当我上传时,它会保存为大小11 MB。我不知道发生了什么要清楚:我只想压缩png和jpg图像,而透明图像不应保存为黑色背景。 请帮助我

php compression image-compression
2个回答
1
投票

我还没有测试过,但是据我所知,您只需要将imagealphablending($image, false);更改为imagealphablending($image, true);,但是潜在地,您必须像这样显式设置透明背景:https://stackoverflow.com/a/2611911/2989952


0
投票

如果您要压缩图像,则必须将其另存为JPG。 PNG是无损格式,因此没有空间可以保存为PNG。

© www.soinside.com 2019 - 2024. All rights reserved.