Jump to content

Display Image rather than Binary


Recommended Posts

Ok so i now have at least my binary data displaying in my html table, how do i get this to display as an image instead of the binary??

 


<?php

require_once('conn.php');

$q = "SELECT image from images where imgid='2'";

$r = @mysqli_query ($dbc, $q);

if ($r) {


echo '
       <table align="center" cellspacing="3" cellpadding="3" width="90%">
<tr>
	<td align="left"><b>Picture</b></td>
</tr>';

while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) {
	echo '
		<tr>
			<td align="left">' .$row['image'] . '</td>
		</tr>';
}
}

?>

 

Thanks

Link to comment
Share on other sites

So my new php code for showimage.php is:

<?php

header("content-type:image/jpg");
require_once('conn.php');

$q = "SELECT image from images where imgid='2'";

$r = @mysqli_query ($dbc, $q);

$row = mysqli_fetch_assoc($r);
$news = imagecreatefromstring($row['image']);
$final = imagejpeg($news);
echo $final;

?>

 

This won't change the header so it still shows binary the code is below:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/e/s/jesspomfret/html/showimage2.php:10) in /home/content/j/e/s/jesspomfret/html/showimage2.php on line 12
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀ^"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡

 

Why does it say headers already sent?? Thanks

 

Link to comment
Share on other sites

Ok i have now edited my code again:

 

<?php
header("content-type:image/jpeg");
require_once('conn.php');
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php
$q = "SELECT image from images where imgid='2'";
$r = @mysqli_query ($dbc, $q);
$data = mysqli_fetch_assoc($r);
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
   header('Content-Type: image/jpg');
   imagejpg($im);
}
else {
   echo 'An error occured.';
}
?>

 

The error i am getting is below:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image in /home/content/j/e/s/jesspomfret/html/showimage2.php on line 18

 

Line 18 is $im = imagecreatefromstring($data);

 

I still don't see my image?!? Any ideas?

Link to comment
Share on other sites

Its not to do with your code. Its how you're doing it. You cannot display HTML and binary data at the same time. The following will display your image

<?php

header("content-type:image/jpg");
require_once('conn.php');

$q = "SELECT image from images where imgid='2'";

$r = @mysqli_query ($dbc, $q);

$row = mysqli_fetch_assoc($r);
$news = imagecreatefromstring($row['image']);
$final = imagejpeg($news);
echo $final;

?>

Once you start adding HTML it wont work. It would be a lot simpler to physically have the image saved on filesystem and only have the path to the image stored in your database.

Link to comment
Share on other sites

I see:

"http://www.wadeandgattonnurseries.com/showimage2.php"

 

On the page. >_>

 

Edit: I just viewed the source and saw the binary.  I think that you're physically incapable of following directions at this point.  Show us the exact code for that page please.  You have absolutely baffled me with your lack of concentration or effort.  Do you even understand how PHP outputs images?

Link to comment
Share on other sites

This is the exact code...what instructions am i not following???

 

<?php
header("content-type:image/jpeg");
require_once('conn.php');

$q = "SELECT image from images where imgid='2'";
$r = @mysqli_query ($dbc, $q);

$row = mysqli_fetch_assoc($r);
$news = imagecreatefromstring($row['image']);
$final = imagejpeg($news);
?>

Link to comment
Share on other sites

Could someone have a look at this and explain to me why i can only see the binary, I have tried having a html page with <img src> pointing to my php file. And then having no html in the php file at all. I still just see binary.  I had an error for a while with not being able to change the header but i have solved that problem by removing the html that was before it.

Any ideas please???

Link to comment
Share on other sites

View source of your page shows some HTML at the start...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>



</body>
</html>

 

 

Please try to check where this html is coming from, it should not be there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.