0

This is my actual code, i'm starting to using php and i want to know how to display a blob image on my website. But until now i can't find how to do it. This is my php code...

$query=mysqli_query($link, "SELECT image, titulo, tecnica, medidas FROM upload ");

$numrows = mysqli_num_rows($query);

$posts = array();

if ($numrows != 0)
{
 while ($row = mysqli_fetch_assoc($query))
  {
      $post = new stdClass();
      $post->image = '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';    
      $post->titulo  = $row['titulo'];
      $post->tecnica  = $row['tecnica'];
      $post->medidas  = $row['medidas'];

      array_push($posts, $post);
   } 
   }

  $jsonData = json_encode($posts);

  header("Content-Type: image/jpeg");

  echo $jsonData;
cdv86
  • 1
  • 3
  • 1
    Your not outputting an image, your returning json, which is fine but set the appropriate header `application/json` not `image/jpeg`, Some simple examples here: [Upload into blob](http://stackoverflow.com/questions/10012355/can-i-load-a-picture-into-mysql-server-side/10012471#10012471) | [Display from blob](http://stackoverflow.com/questions/8468338/i-want-to-show-image-on-php-which-is-store-in-mysql-database-as-blob-type/8468369#8468369) – Lawrence Cherone Jul 28 '14 at 21:04
  • thanks Loz Cherone ツ!!! thats just what i need to do to display the images!! – cdv86 Jul 29 '14 at 01:10

1 Answers1

0

Try this:

<?php

    //Connect to the database
  $databasehost = "YOUR DATABASE HOST";
  $databasename = "YOUR DATABASE NAME";
  $databaseusername ="YOUR DATABASE USERNAME";
  $databasepassword = "YOUR DATABASE PASSWORD";

  $con = mysqli_connect($databasehost, $databaseusername, $databasepassword, $databasename) or die(mysqli_error($con));
  mysqli_set_charset ($con, "utf8");

$sql = "SELECT image, titulo, tecnica, medidas FROM upload;";
$res = mysqli_query($con, $sql);
$result = array();

while($row = mysqli_fetch_array($res)) {
array_push($result,
array(
'image'=>base64_encode($row[0]),
'titulo'=>$row[1],
'tecnica'=>$row[2],
'medidas'=>$row[3]
));
}
echo json_encode(array($result));

mysqli_close($con);

This will print your data like this:

{[{"image":"IMAGE ENCODED IN BASE 64","titulo":"SOME DATA","tecnica":"SOME DATA", "medidas":"SOME DATA"},

{"image":"IMAGE ENCODED IN BASE 64","titulo":"SOME DATA","tecnica":"SOME DATA", "medidas":"SOME DATA"},

{"image":"IMAGE ENCODED IN BASE 64","titulo":"SOME DATA","tecnica":"SOME DATA", "medidas":"SOME DATA"}]}

Hope it helps! Good Luck! Happy Coding!

Shreshth Kharbanda
  • 1,777
  • 14
  • 24