douhuo0884 2017-12-06 05:48
浏览 71

如何在php pdo mysql上传到数据库之前压缩图像而不失其质量?

I'm presently working in online project..where users upload their images and that images will be first stored in server folder like "uploads" directory...and once the file will moved into that uploads directory..then the path will inserted into the mysql database using mysql query. But the problem is..the images takes longer time to load on webpage...i want to reduce the image size before uploading into database and server..

here is my code

$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 < 1000000){

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

And my query looks like..

if(!isset($errMSG))
    {


        $stmt = $user_home->runQuery("INSERT INTO cam_info (userID,
                                                            cam_name,
                                                cam_model,
                                            pixels,
                                    photoshoot,
                                    cam_rent,
                                    cam_img,
                                    mobile,
                                    state,
                                    address,
                                    upd_date,
                                    code )
                                                            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

        $stmt->bindParam(1,$id);
        $stmt->bindParam(2,$cam_name);
        $stmt->bindParam(3,$modelname);
        $stmt->bindParam(4,$pixel);
        $stmt->bindParam(5,$photoshoot);
        $stmt->bindParam(6,$rentpday);
        $stmt->bindParam(7,$userpic);
        $stmt->bindParam(8,$usermob);
        $stmt->bindParam(9,$state);
        $stmt->bindParam(10,$useraddrs);
        $stmt->bindParam(11,$upd_date);
        $stmt->bindParam(12,$ucode);

        if($stmt->execute())
        {

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

2条回答 默认 最新

  • dongyi1982 2017-12-06 06:07
    关注

    Generally speaking it is not good idea to upload images into the database - the database will grow up very rapidly and may leads to performance and maintain issues in the future.

    Use database to keep the metadata about the pictures and keep the images on file system.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图