Display image stored in Database

Asked

Viewed 1,232 times

1

I’m creating a digital menu that contains pictures of the dishes. I stored the photo in the Mysql BD using the following code:

HTML code

<form enctype="multipart/form-data" method="post" action="./processos/cadastra_item.php">
<p align="left">Imagem<br>
    <input type="hidden" name="MAX_FILE_SIZE" value="99999999"/>
    <input name="imagem" type="file"/>
</form>

PHP code

$imagem = $_FILES['imagem']['tmp_name'];
$tamanho = $_FILES['imagem']['size'];

if ( $imagem != "none" )
{
  $fp = fopen($imagem, "rb");
  $item->itemImg = fread($fp, $tamanho);
  $item->itemImg = addslashes($item->itemImg);
  fclose($fp);
}

After that I perform the INSERT normally. The field that stores the image is of type LONGBLOB. I checked by Workbench and the image was stored successfully but I can not display it on the page.

The code I used to try to display the image follows below:

echo "<center><img src='./processos/verImg.php?itemCod=\".$cod.\"' width='100' height='50' border='1'></center>"

PHP (File verImg.php)

$itemCod = $_GET['itemCod'];

    //Encontra o item no banco utilizando o código
    $sql = mysqli_query("SELECT itemCod, itemImg FROM Cardapio WHERE itemCod = ".$itemCod."");

    $row = mysqli_fetch_object($sql, MYSQLI_ASSOC);
    //itemImg é o campo onde esta armazenada a imagem
    $bytes  = $row->itemImg;
    header( "Content-type: image/gif");              
    echo $bytes;

I also tried to use mysqli_fetch_array but failed to display the image.

  • I forgot to mention that when I open the page, the place where the image should appear only appears an empty square. I tried with JPG, PNG and JPEG images.

  • In this passage, SELECT itemCod FROM, where is the image field? It should not be SELECT itemImg FROM?

  • 1

    @Danielomine really. This was an error when transposing the code into the Stack. I’ve corrected the question. The error is occurring with select done in the correct way :D

1 answer

1


It may be that the error is in your html to show the image, you are not specifying that it is a Base64. Try to change the last line to:

echo 'data:image/jpeg;base64,'.base64_encode($bytes);
  • 1

    Before the excerpt .base64_encode($bytes) .' "; would be double quotes even?

  • I tried what you said, and unfortunately it didn’t work :/ will be that the problem lies in the inclusion of the image in the database?

  • I commented the header and the page returned some warnings that I did not understand very well: Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: xampp htdocs restaurant processes verImg.php on line 11 Warning: mysqli_fetch_object() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs restaurant processes verImg.php on line 13 Notice: Trying to get Property of non-object in C: xampp htdocs restaurant processes verImg.php on line 14 data:image/jpeg;Base64,'

  • I corrected the double quotes error. If this does not solve it may actually be the way you are saving the image. has a look at this thread: https://stackoverflow.com/questions/20556773/php-display-image-blob-from-mysql

  • I used your suggestion and suggestions provided in the topic https://stackoverflow.com/questions/20556773/php-display-image-blob-from-mysql. Worked properly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.