Displaying a Base64 images from a database via PHP

后端 未结 6 2139
佛祖请我去吃肉
佛祖请我去吃肉 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 18:57

    The solution to your problem is here:

    How to decode a base64 string (gif) into image in PHP / HTML

    Quoting that source but modifying:

    In the case you strip out the first case and choose to decode the string, you should add this before echoing the decoded image data:

    header("Content-type: image/gif");
    $data = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
    echo base64_decode($data);
    

    In the second case, use this instead:

    echo '';
    

    The second case is bad because the browser does not perform caching if the same image is shown on multiple pages.

提交回复
热议问题