用于MySQL数据库保存的base 64图像大小调整

时间:2019-05-17 06:13:10

标签: php mysql

调整图像大小,然后将其转换为base64格式。

我搜索了很多网站,但没有找到解决方法


if(isset($_FILES['uploadFile']['name']) && !empty($_FILES['uploadFile']['name'])) {
        //Allowed file type
        $allowed_extensions = array("jpg","jpeg","png","gif");

        //File extension
        $ext = strtolower(pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION));

        //Check extension
        if(in_array($ext, $allowed_extensions)) {
           //Convert image to base64
           $encoded_image = base64_encode(file_get_contents($_FILES['uploadFile']['tmp_name']));
           $encoded_image = 'data:image/' . $ext . ';base64,' . $encoded_image;
           $query = "INSERT INTO images (id, encoded_image) VALUES (NULL, '$encoded_image');";

           mysqli_query($con, $query);
           echo "File name : " . $_FILES['uploadFile']['name'];
           if(mysqli_affected_rows($con) > 0) {
              echo "Status : Uploaded";
              $last_insert_id = mysqli_insert_id($con); 
           } else {
              echo "Status : Failed to upload!";
           }
       } else {
           echo "File not allowed";
       }
  }

0 个答案:

没有答案