Display saved image in BLOB

Asked

Viewed 3,036 times

0

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

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; 
}
  • Possible duplicate of http://answall.com/questions/48325/slideshow-com-imagens-blob-do-mysql

2 answers

1


To view images from the BD you have to change the header of the page for the browser to understand that it is an image. To do this create a.php generate file that will be responsible for generating the image so that the browser understands it as image and call this page in the src attribute of the html img tag. Thus:

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."”";

-1

vc has can print directly on image encoding in Base64

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

where $data he image that came straight from db

  • For this to work: echo '<img src="data:image/gif;base64,' . base64_encode($data) . '" />'; (if the BLOB is not encoded in Base64) or echo '<img src="data:image/gif;base64,' . $data . '" />'; (if the BLOB is coded in Base64). Neither of the two forms needs the echo base64_decode($data);.

Browser other questions tagged

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