简体   繁体   English

在保存到数据库之前如何压缩图像大小而不会降低质量

[英]How to compress image size before saving to database without losing quality

i have a from and from that i forced user to upload a image and other details etc....And in my root directory i created one folder called(user_images) Every thing is fine , but the problem is..user may insert large files so that loading of webpage is slow... how to resize before uploading to database... 我有一个from and from那我强迫用户上传图像和其他详细信息等。。。在我的根目录中,我创建了一个名为(user_images)的文件夹。一切都很好,但是问题是..user可能会插入大文件,以便网页加载缓慢...如何在上载到数据库之前调整大小...

here is my code... 这是我的代码...

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

    }
}

Any changes in this code to compress image size before uploading.. thanks in advance.. 在此代码中进行任何更改以压缩图像大小,然后再上传。.在此先感谢。

AFAIK, you can't maintain the quality while reducing the size of the image. AFAIK,您无法在缩小图像尺寸的同时保持质量。 You could use imagejpeg , to reduce the size of the image by reducing it quality. 您可以使用imagejpeg ,通过降低图像质量来减小图像的大小。

When you are about to save the image, you need to reduce the quality of image first. 当您要保存图像时,需要首先降低图像质量。

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

It is actually super easy to compress images. 实际上,压缩图像非常容易。 Just change them all over to png: 只需将它们全部更改为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)

That's really it! 就是这样! Png is a lossless compression method so you won't lose any quality in the process Png是一种无损压缩方法,因此您不会在此过程中损失任何质量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在php pdo mysql中上传到数据库之前,如何压缩图像而不降低其质量? - how to compress image without loosing its quality before uploading to database in php pdo mysql? PHP:如何压缩图像而不会失去可见的质量(自动)? - PHP: How to compress images without losing visible quality (automatically)? 如何在不损失图像质量的情况下裁剪图像? - How to crop an image without losing its quality? 我怎样才能在不损失质量的情况下减小上传的图像大小(很多) - How can i reduce uploaded image size without losing quality(much) 如何解码和编码base64图像,而不会失去质量和尺寸 - how to decode and encode base64 image without losing the quality and the size 如何使用php或任何其他语言减小图像文件大小而又不损失质量或分辨率? - How to reduce Image file size without losing quality or resolution using php or any other language? 压缩图像质量(从4MB到2MB)而不会丢失其尺寸 - Compress image quality (from 4MB to 2MB) without losing its dimension 上传图片而不会丢失质量 - Upload image without losing quality 在不损失质量的情况下调整图像大小 - Resizing image without losing quality PHP7-优化图像(压缩而不损失质量) - PHP7 - optimize images ( Compress without losing quality )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM