I am using a file that retrieves image data from a database and displays it using an image header.
I need to insert the image into an html image tag and am getting defeated because the header fails since I have to send a bunch of html before inserting the image file.
I think I can use output buffering to achieve my goals here but am not sure just how to do that. There may also be another solution that I am not aware of.
The files involved are included below. The line of code where the first file using the header is inserted is in the second file below and is followed by a comment like this // ************* to identify the line of code.
Any help would be greatly appreciated.
Thanks,
–Kenoli
The file that retrieves and displays the image is here:
<?php // get_image.php
// ::::::::::::::::::::::::::::::::::::::::::::::::::::://
// //
// Retrieve image data from database and display //
// //
// ::::::::::::::::::::::::::::::::::::::::::::::::::::://
include('../includes/config.inc.php');
include(DB);
$id = $_GET['id'];
$sql = "SELECT image FROM images WHERE id_image = '$id'";
$stmt = $db->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
header("Content-type: image/jpeg");
echo "{$row['image']}";
?>
I am using this file in the src attribute of an <img> tag to display it on the page. As mentioned above, it fails since it is attempting to send a header after other content has been sent.
<?php
//include ('includes/config.inc.php');
//include (DB);
$sql = "SELECT id_image, name, size, description FROM images";
$stmt = $db->query($sql);
echo '<table width="100%">';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id_image'];
//echo 'In the right place.';
print <<<STUFF
<tr>
<td width="220px"><img src="__functions/get_image.php?id=$id" width="200px" ></td> // *************
<td><table cellpadding="0">
<tr>
<td valign="bottom">ID: $id</td>
</tr><tr>
<td valign="bottom">Name: {$row['name']}</td>
</tr>
<tr>
<td>{$row['description']}</td>
<tr>
<td valign="middle">Image Size: {$row['size']}</td>
</tr>
<tr>
<td valign="top"><form><input type="text" value="<img src='__functions/get_image.php?id=$id&column=image' >" size='60' readonly="true" /></form></td>
</tr>
</table></td>
</tr>
STUFF;
}