Display image stored in the Database

1

I'm creating a digital menu that contains photos 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 this I perform the INSERT normally. The field that stores the image is of type LONGBLOB. I checked the Workbench and the image was successfully stored 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 (verImg.php file)

$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 using mysqli_fetch_array but I was not successful in displaying the image.

    
asked by anonymous 01.06.2017 / 03:13

1 answer

1

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

echo 'data:image/jpeg;base64,'.base64_encode($bytes);
    
01.06.2017 / 14:38