convert BLOB image value to.jpg image again

Asked

Viewed 192 times

-2

I need to make the code below not show the blob value of the image q was saved in the database, but show it in figure.jpg to list on the screen. how can I convert inside that same code?

<?php

require_once "conexao.class.php";

try
 {
   $conecta = new PDO('mysql:host=localhost;dbname=informacoes;charset=utf8', 'root', '');
   $conecta->exec("set names utf8"); //permite caracteres latinos.

   $consultaSQL = "SELECT imagem FROM escolhida1 WHERE 1";
   $exComando = $conecta->prepare($consultaSQL); //testar o comando
   $exComando->execute(array());
   foreach($exComando as $resultado)
   {
     $png = "png";
     $conteudo = $resultado['imagem'];
     header("Content-Type: $png");
     echo $conteudo;
   } 
  }catch(PDOException $erro)
   {
     echo("Errrooooo! foi esse: " . $erro->getMessage());
   }
?>
  • personal pardon, not jpg, in question, is PNG same

1 answer

0

Content-Type for PNG images is "image/png", not "png". MIME types

If you are sure that the BLOB that is stored in the database is an image, you can "simply" write this BLOB to a file. The BLOB is the binary image.

If you want to display in an HTML page, then either you save in a file and use the file path as attribute src tag , or use the HTML5 date:

<img src="data:Content-Type: image/<?php echo $png; ?>;base64,<?php echo base64_encode($conteudo); ?>" />

I would do some tests before trying to render an image this way to make sure that it is an image in fact and that there is no virus in the image BLOB.

Browser other questions tagged

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