how to display images from database in php?

后端 未结 3 1809
予麋鹿
予麋鹿 2020-12-07 05:10

i write bellow code to display images from database



        
相关标签:
3条回答
  • 2020-12-07 05:28

    You also might use base64 encoding to build in the image. Like

    <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAWgBaAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..." />
    

    UPDATE, base64 encoding example

    You can do that easily:

    <?php
    $imageId = intval($_GET["id"]);
    
    $query = mysql_query("SELECT img FROM images WHERE id = ". $imageId);
    $row = mysql_fetch_array($query);
    
    $mime = null;
    // place $type init. here
    if ($type=="pjpeg") // <<< where do you get $type btw?
        $mime = "image/jpeg";
    
    $b64Src = "data:".$mime.";base64," . base64_encode($row["img"]);
    echo '<img src="'.$b64Src.'" alt="" />';
    ?>
    
    0 讨论(0)
  • 2020-12-07 05:42

    As above + be sure to pass the replace = true parameter to the header function.

    header( 'Content-Type: image/jpeg', true );

    0 讨论(0)
  • 2020-12-07 05:49

    jpeg is not a valid Content-Type, it should be image/jpeg

    0 讨论(0)
提交回复
热议问题