This code is working (using Heroku):
<?php
$db = parse_url(getenv("DATABASE_URL"));
$pdo = new PDO("pgsql:" .
sprintf("host=%s;port=%s;user=%s;password=%s;dbname=%s",
$db["host"],
$db["port"],
$db["user"],
$db["pass"],
ltrim($db["path"], "/")
));
$sql = 'SELECT "Image" AS data FROM mylinks where id = 2;';
$stmt = $pdo->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data = $row['data'];
}
$stmt->closeCursor();
file_put_contents("abc.jpg", $data);
$image = 'abc.jpg';
$imageData = base64_encode(file_get_contents($image));
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;
echo '<img src="', $src, '">';
?>
If I refuse to write the image file and process the image data directly like this:
...
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data = $row['data'];
}
$stmt->closeCursor();
$imageData = base64_encode($data);
$src = 'data: jpeg;base64,'.$imageData;
echo '<img src="', $src, '">';
?>
It doesn't work. I only get this empty img tag:
<img src="data: jpeg;base64,">
It of course makes no sense to write a file and the read it again. There must be a smart way to process the $data directly but I wasn't able to figure that out. Can anybody help?