-1

I made an API which sends me many pieces of data. One of them is a blob from my MySQL database. I can see the blob encoded to base64 but I can't use it in an img tag for displaying my image.

I already watched others questions. By the way, I think that the img tag is correct. Maybe it's the JSON which does something with my data...

My data from the API

array (size=2)
  0 => 
    object(stdClass)[1]
      public 'LId' => string '1' (length=1)
      public 'LNom' => string 'Visual Paradigm' (length=15)
      public 'IImage' => string '\/9j\/4AAQSkZJRgABAQAAAQABAAD'... (length=8455)
  1 => 
    object(stdClass)[2]
      public 'LId' => string '2' (length=1)
      public 'LNom' => string 'Visual Studio' (length=13)
      public 'IImage' => string '\/9j\/4AAQSkZJRgABAQEAYABgAAD'... (length=44843)

API call :

$url = "localhost/projet/api/fake/getSoftwaresFAKE.php";
     $data = CallAPI('GET', $url);

     $json = json_decode($data);

     echo '<img src="data:image/jpeg;base64,'.($json[1]->IImage).'"/>';

API code

<?php
// on se connecte à MySQL et on sélectionne la base
$mysqli = new mysqli("192.168.1.23", "API", 'Pa$$w0rd', "AtoutProtectDB");

if ($mysqli->connect_errno) {
    echo "Echec lors de la connexion à MySQL : (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// on crée la requête SQL 
$req = $mysqli->query('SELECT LId, LNom, IImage FROM Logiciels, Images WHERE Logiciels.IId = Images.IId'); 


$req->data_seek(0);
while ($row = $req->fetch_assoc()) { 
    $data [] = ['LId' => $row['LId'],'LNom' => $row['LNom'],'IId' => base64_encode($row['IImage'])];
}

echo json_encode($data);
?>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Guillaume
  • 108
  • 10

2 Answers2

1

You should check if the value returned by $json[1]->IImage is what you are waiting and if it is correclty working (here for example).

I don't know the file type of your images, but make sure data:image/jpeg is correct.

Paul Evans
  • 434
  • 2
  • 13
-1

OK, i find the problem. I was making a fake php API for my team mate on this project. So i did a copy and past of the data ecoded to base64 in a variable in VSCode. This was for that my team mate could test his website with the right data format from the API without use the server on a virtual machine cause his PC is too weak for use VMWare...

And my issue was here. When i had past the data, VSCode add a '\' after each special character of the string... It's probably and AddOn of VSCode wich did that.

So thanx you and pay attention with your AddOns on VSCode

Guillaume
  • 108
  • 10