【问题标题】:How to compress image size before saving to database without losing quality如何在保存到数据库之前压缩图像大小而不损失质量
【发布时间】:2017-10-01 18:07:58
【问题描述】:

我有一个来自和来自那个我强迫用户上传图像和其他详细信息等......在我的根目录中我创建了一个名为(user_images)的文件夹,一切都很好,但问题是......用户可能会插入大文件,从而导致网页加载缓慢... 如何在上传到数据库之前调整大小...

这是我的代码...

if(isset($_POST['btnsave']))
{
    $camname = $_POST['cam_name'];// user name
    $modelname = $_POST['model'];// user email
    $rentpday = $_POST['rent_pday'];// user name
    $usermob = $_POST['mob'];// user email
    $useraddrs = $_POST['addrs'];// user email
    $upd_date =date('Y-m-d H:i:s');//upl_date

    $imgFile = $_FILES['user_image']['name'];
    $tmp_dir = $_FILES['user_image']['tmp_name'];
    $imgSize = $_FILES['user_image']['size'];


    if(empty($camname)){
        $errMSG = "Please Enter Cam name.";
    }
    else if(empty($usermob)){
        $errMSG = "Please mobile number";
    }
    else if(empty($camname)){
        $errMSG = "Please enter cam_name";
    }
    else if(empty($modelname)){
        $errMSG = "Please enter model";
    }
    else if(empty($rentpday)){
        $errMSG = "Please enter rent per day";
    }
    else if(empty($imgFile)){
        $errMSG = "Please Select Image File.";
    }
    else
    {
        $upload_dir = 'user_images/'; // upload directory

        $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

        // valid image extensions
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

        // rename uploading image
        $userpic = rand(1000,1000000).".".$imgExt;

        // allow valid image file formats
        if(in_array($imgExt, $valid_extensions)){           
            // Check file size '5MB'
            if($imgSize < 5000000)              {
                move_uploaded_file($tmp_dir,$upload_dir.$userpic);
            }
            else{
                $errMSG = "Sorry, your file is too large.";
            }
        }
        else{
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
        }
    }


    // if no error occured, continue ....
    if(!isset($errMSG))
    {


        $stmt = $user_home->runQuery("UPDATE post_data 
                                      set cam_name=?,
                                      cam_model =?, 
                                      cam_rent=?,
                                      cam_img=?,
                                      mobile=?,
                                      address=?,
                                      upd_date=? 
                                      where userID=?
                                      ");

        $stmt->bindParam(1,$camname);
        $stmt->bindParam(2,$modelname);
        $stmt->bindParam(3,$rentpday);
        $stmt->bindParam(4,$userpic);
        $stmt->bindParam(5,$usermob);
        $stmt->bindParam(6,$useraddrs);
        $stmt->bindParam(7,$upd_date);
        $stmt->bindParam(8,$id);

        if($stmt->execute())
        {

            $successMSG = "Record saved success";
        }
        else
        {
            $errMSG = "error while inserting....";
        }

    }
}

此代码中的任何更改以在上传之前压缩图像大小.. 提前谢谢..

【问题讨论】:

  • 您可以将它们限制为特定大小
  • 我设置为 5mb...但是如果用户输入 4.2mb 图像文件...它不会给出任何错误,插入数据库但图像文件在我检索时在网页中不可见..

标签: php image image-resizing


【解决方案1】:

AFAIK,您无法在减小图像尺寸的同时保持质量。您可以使用 imagejpeg,通过降低图像质量来减小图像的大小。

当你要保存图片时,需要先降低图片质量。

        if($imgSize < 5000000) {
            $image = imagecreatefromjpeg($upload_dir.$userpic);
            imagejpeg($image, $new_image_path, 70);
            move_uploaded_file($tmp_dir,$new_image_path);
        }

【讨论】:

  • 哦..在我上面的哪里使用
  • @lohithkumar 另外,请注意我们正在降低质量。
  • 警告:imagecreatefromjpeg(user_images/768709.jpg):无法打开流:没有这样的文件或目录
  • @lohithkumar 不是有效路径。你能早点保存图像吗?我确定你不是。因为,您正在保存userpic,这似乎只是图像名称
  • 我明白了兄弟......在将该 img 文件保存到 user_images 文件夹之前......我们如何打开该特定文件的流......是否可能......从我上面我在检查文件大小之前不会保存 $userpic .....
【解决方案2】:

压缩图像实际上非常容易。只需将它们全部更改为 png:

$image = imagecreatefromstring(file_get_contents($tmp_dir));
$saveLocation = “user_images/”.md5(uniqid()).”png”;
//Please use this instead of your current file naming method to remove the possibility of a duplicate (which rand will sometimes yield)
imagepng($image,$saveLocation,9);
//9 represents the compression level (0 no compression - 9 max compression)

原来如此! Png 是一种无损压缩方法,因此您不会在此过程中损失任何质量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2012-06-22
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多