Displaying a Base64 images from a database via PHP

后端 未结 6 2203
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 18:21

I have this database that contains images as strings. Those strings look something like this:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD...
         


        
6条回答
  •  余生分开走
    2020-12-05 19:11

    /**
    * @param $base64_image_content 
    * @param $path 
    * @return bool|string
    */
    function base64_image_content($base64_image_content,$path){
      if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path."/".date('Ymd',time())."/";
        $basePutUrl = C('UPLOAD_IMG_BASE64_URL').$new_file;
    
        if(!file_exists($basePutUrl)){
            //Check if there is a folder, if not, create it and grant the highest authority.
           mkdir($basePutUrl, 0700);
        }
           $ping_url = genRandomString(8).time().".{$type}";
           $ftp_image_upload_url = $new_file.$ping_url;
           $local_file_url = $basePutUrl.$ping_url;
    
       if (file_put_contents($local_file_url, base64_decode(str_replace($result[1], '', $base64_image_content)))){
         ftp_upload(C('REMOTE_ROOT').$ftp_image_upload_url,$local_file_url);
             return $ftp_image_upload_url;
        }else{
             return false;
        }
      }else{
         return false;
     }
    }
    

提交回复
热议问题