View saved image in BLOB [duplicate]

0

I would like to know how to display a saved image in the database in BLOB:

code:

$nome = $_POST["nome"];
$preco = $_POST["preco"];
$cor = $_POST["cor"];
$descricao = $_POST["descricao"];

if(is_uploaded_file($_FILES['imagem']['tmp_name']))
{
 $imgData = file_get_contents($_FILES['imagem']['tmp_name']);
 $sizeData = getimagesize($_FILES['imagem']['tmp_name']);
 $imagem = $imgData;
}
if(insereProduto($conexao, $nome, $preco, $imagem, $descricao, $cor)) { 

    ?><p class="text-success">Produto  <?=$nome ?>, que custa <?=$preco?> adicionado com sucesso!</p><?php


}

function insereProduto($conexao, $nome, $preco, $imagem, $descricao, $cor) {
   $query = "insert into produtos (nome, preco, imagem, descricao, cor) values 
   ('{$nome}', '{$preco}','{$imagem}', '{$descricao}','{$cor}')"; 
   echo $query;
   $resultadoDaInsercao = mysqli_query($conexao, $query); 
   return $resultadoDaInsercao; 
}
    
asked by anonymous 21.11.2016 / 18:37

2 answers

1

To display images from the DB you have to change the header of the page so the browser understands it to be an image. To do this create a gera.php file that will be responsible for generating the image so that the browser understands it as image and calls that page in the src attribute of the html img tag. This way:

Gera.php

  <?
//RECEBE
PARÂMETRO 
$id = $_GET["id"];

//CONECTA
AO MYSQL 
$conn = mysqli_connect("localhost", "usuario",
"senha", "base de dados ");

//EXIBE
IMAGEM 
$sql = mysqli_query($conn, "SELECT foto,tipo FROM
fotos WHERE id = ".$id."");

$row = mysqli_fetch_array($sql,
MYSQLI_ASSOC); 
$tipo = $row["tipo"]; 
$bytes = $row["foto"];

//EXIBE
IMAGEM 
header("Content-type: ".$tipo."");

echo $bytes;
?>

Display:

  echo "<img src=”gera.php?id=".$id."”";
    
22.11.2016 / 02:02
-1

You can print directly on the base64 encoding image

echo base64_decode($data);
echo '<img src="data:image/gif;base64,' . $data . '" />';

where $ data he has the image that came straight from db

    
23.12.2017 / 00:42