Problems displaying blob images

戏子无情 提交于 2019-12-08 12:16:05

问题


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;

回答1:


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!



来源:https://stackoverflow.com/questions/25004237/problems-displaying-blob-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!