englishcodemonkey Posted July 19, 2008 Share Posted July 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2008 Share Posted July 19, 2008 Each image on a web page requires a HTML img tag - http://www.w3schools.com/html/html_images.asp The URL in the src="..." parameter would be to a .php file that outputs a Content-type: header for the image type and then outputs the binary data for that image to the browser. Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 19, 2008 Author Share Posted July 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 19, 2008 Share Posted July 19, 2008 change $final = imagejpeg($news); echo $final; to imagejpeg($news); imagedestroy($news); place on page with <img src='showimage.php'> Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 21, 2008 Author Share Posted July 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 Why did you run it through base64_decode? Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 21, 2008 Author Share Posted July 21, 2008 I'm just trying different things because i don't understand why i can only see binary!! What do you suggest to get this to work?? Thanks Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 21, 2008 Share Posted July 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 @wildteen: Not really, because you have an echo $final; at the end. Remove that line. Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 21, 2008 Author Share Posted July 21, 2008 I still just see binary www.wadeandgattonnurseries.com/showimage2.php Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 21, 2008 Author Share Posted July 21, 2008 what do u mean? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 Show us the exact code for that page please. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 21, 2008 Share Posted July 21, 2008 You may want to check this out. That shows the correct way of retrieving images stored in the database and displaying it in HTML Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 21, 2008 Author Share Posted July 21, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 22, 2008 Author Share Posted July 22, 2008 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??? Quote Link to comment Share on other sites More sharing options...
samshel Posted July 22, 2008 Share Posted July 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
englishcodemonkey Posted July 22, 2008 Author Share Posted July 22, 2008 i noticed that to when i viewed the source. Where could this be coming from?? I'm so confused at this point! Also when i viewed the source it displays all the binary...ahhhh! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.