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);
?>