Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

SOLVED: Display image stored in MySQL



Site of the Month Nominations
ENTER YOUR SITE NOW!

Reply
 
LinkBack Thread Tools
Old January 26 '10, 03:51 AM (#1)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
SOLVED: Display image stored in MySQL

Hi everyone,

I'm having a slight problem with displaying an image stored in my mysql database. The data is being stored ok, I tested this by outputting the image data to a file. When I went to open the file, there was my image. However, when I go to display the image in the browser I get problems. The following is the code in my image loader file...

PHP Code:
<?php
//mysql connect & retrieve stuff would go here

header("Content-Type: image/jpg"); // The image I am loading is in fact a .jpg

//$fp=fopen("test_image.jpg","w+");
//fwrite($fp,$image_data); // this resulting image appears fine
//fclose($fp);

echo $image_data// this one does not
?>
Any ideas what I am doing wrong? Any help is appreciated.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 10:00 AM (#2)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
If you're storing your images in MySQL,
what is the fwrite portion for? I'm confused.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 01:03 PM (#3)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
It was the test to make sure the data was being stored properly, which it is. Which is also why it is commented out. I guess I shouldn't have included it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 01:09 PM (#4)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
Show us the script then, where you're displaying the JPEG from your MySQL database.

EDIT:
And I assume your image variable is a BLOB?



.

Last edited by mlseim; January 26 '10 at 01:13 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 01:27 PM (#5)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
The code I posted above is most of it, but here's the entire thing. Yes, it is stored as blob, again, the data outputs to a file perfectly. It has problems being output in the browser.

PHP Code:

<?php

$mysql_con 
mysql_connect("127.0.0.1","root","***")
mysql_select_db("pic_db");

$result mysql_query("SELECT * FROM pics where id = ".$_GET['id']." LIMIT 1");
$data mysql_fetch_row($result);


// This tells the browser that the content should be a jpg image...
header("Content-Type: image/jpg");

// This is where the data is being given to the browser
echo $data[2];

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 01:39 PM (#6)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
Let's say that script you show is called "image.php" ...

On another page, you do this:

<html>
<body>
<img src="image.php?id=235" />
</body>
</html>

You're saying that doesn't work?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 01:52 PM (#7)
smoseley is offline
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
It should work anyway.

Make sure you have no white space (spaces or return carriages) outside your <? PHP ?> block. That could corrupt the output.

Also, isn't the correct mime type JPEG (with an "E")? so it shoudl be header("Content-Type: image/jpeg");
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 01:53 PM (#8)
smoseley is offline
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
If the above doesn't work, try adding a Content-Length header (just do a strlen() on the image data to get that value).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 02:44 PM (#9)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
Steven ...

I missed this one: image/jpeg (that might be it).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 07:43 PM (#10)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
Tried those with no luck. I think it may have to do with the way the text is formatted when it shows up on the page. When I just display the raw data, all the new lines disappear. Is there any way to keep that from happening?

On a side note, the reason I had "content-type: image/jpg" is because when I checked the header information for other images (on my own site) it was image/[file ext] depending on the file type.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 08:35 PM (#11)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
Show us the actual page ... where the image is supposed to appear.
And also the PHP scripting that creates that page?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 11:25 PM (#12)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
Here's a link to the page,

http://clan.bluefirecode.com/system/..._pic.php?id=28

The script that creates the page is almost exactly what's in my third post. All the page is doing is loading an image to be called later. No html or anything, it's simply an image.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 12:20 AM (#13)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
Ok, I've given up on this storage strategy. Instead I'm going to store references to the image files (as means of security). I've sniffed around the internet for hours now and apparently I'm not the only one with this problem (and still no solution).

Thanks for your help anyway.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 04:12 AM (#14)
Dorky is offline
Freelance
 
Dorky's Avatar
 
Join Date: June 2009
Location: Destin Florida
Posts: 905
Dorky will become famous soon enough
ok dude. your not actually echoing the image like this
Quote:
$image_data
your just trying to echo the data. this can be done but not with echo. instead use this
http://www.php.net/manual/en/function.image2wbmp.php
if you want to use echo then you will need to echo the image in an image tag and src= to the image file you wrote to.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 08:45 AM (#15)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
Yea, I suppose GD would work, however that's not installed on my server (right now anyway). The problem is that the way I was doing it should work, you don't need the img tag or anything (and believe me I tried that at one point). The idea is that you tell the browser that it's about to display an image, using the headers, and then just output the image data and it should display the image (the equivalent of right-clicking on an image and hitting 'View Image').
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 09:42 AM (#16)
smoseley is offline
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
Echo works just fine... you're storing the image data as a BLOB in the DB, right? You should be able to just echo it to the page.

GD is used to actually CREATE an image, or to alter an existing image.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 09:48 AM (#17)
smoseley is offline
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
There's some funky stuff going on in that image. Can you attach the original so I can compare?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 09:52 AM (#18)
smoseley is offline
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
Can you do me a favor and post the script you're using to insert the image in the DB? I think that may be where the problem is.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 10:22 AM (#19)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
And also give us the listing for this script: get_pic.php

scratch-out any MySQL login stuff.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 11:06 AM (#20)
Dorky is offline
Freelance
 
Dorky's Avatar
 
Join Date: June 2009
Location: Destin Florida
Posts: 905
Dorky will become famous soon enough
or imagejpg(); i have had the same issue, like you said, as have others. i just think there are too many other ways to do it then butting you head on a buggy approach.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 11:36 AM (#21)
Dorky is offline
Freelance
 
Dorky's Avatar
 
Join Date: June 2009
Location: Destin Florida
Posts: 905
Dorky will become famous soon enough
are the other images on the page or is this being tested as standalone?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 06:35 PM (#22)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
Link to image we're trying to display:
http://clan.bluefirecode.com/system/...aded_image.jpg
Link to the image loader page (get_pic.php?id=28):
http://clan.bluefirecode.com/system/..._pic.php?id=28
...and it's contents:
PHP Code:
<?php

$mysql_con 
mysql_connect("127.0.0.1","root","***")
mysql_select_db("pic_db");

$result mysql_query("SELECT * FROM mypic_pics WHERE id = ".$_GET['id']." LIMIT 1");
$data mysql_fetch_row($result);

$image $data[2]; // data[2] is the image contents

header("Content-Length: ".strlen($image));
header("Content-Type: image/".$data[3]); //data[3] is the file type (jpg/jpeg/png/gif)

echo $image;
?>
here is the upload script...

PHP Code:
// these two lines are specifically for finding the file extension if the name has more than one '.' in it.
$name_array explode(".",$_FILES['picture_upload']['name']);
$last_index sizeof($name_array) - 1

$image_data file_get_contents($_FILES['picture_upload']['tmp_name']);    
$image_data mysql_real_escape_string($image_data);

$query "INSERT INTO mypic_pics (user,data,ext,caption,password,album,public) VALUES ('admin','";
$query .= $image_data."','".$name_array[$last_index]."','A test caption.',' ','0','0')";

$result mysql_query($query); 
My image data is being stored as a long blog, which I can't imagine is the problem.

Again, I don't believe that the storage is the problem because I can pull the data out of mysql and write it to a normal jpg file just fine; the image appears without error. The error occurs when I try to give the data directly to the browser.

If you want to try your own upload, I'm having the script tell you your id (it wil appear right above 'home'... here is the link to the page:

http://clan.bluefirecode.com/page/?page=home
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 07:48 PM (#23)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
How do you know that $data[2] is the image contents?
$image = $data[2]; // data[2] is the image contents

I would expect it be like this ....

Say for instance, that the name of your MySQL image field is 'data' ...

$image=$data['data'];
$extension=$data['ext'];

You didn't give us the names of your columns, but I think you should reference the name of the column.

EDIT ... yes, you showed us the names .. use 'data' instead of 2.
also the other ones.

Last edited by mlseim; January 27 '10 at 07:52 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 10:34 PM (#24)
bluefireco is offline
WDF Member
 
bluefireco's Avatar
 
Join Date: June 2009
Posts: 21
bluefireco is an unknown quantity at this point
Send a message via AIM to bluefireco
Quote:
Originally Posted by mlseim
How do you know that $data[2] is the image contents?
$image = $data[2]; // data[2] is the image contents

I would expect it be like this ....

Say for instance, that the name of your MySQL image field is 'data' ...

$image=$data['data'];
$extension=$data['ext'];
Yea, sorry, id is set to auto-increment and was not included in sql script. Here are all the columns...

id,user,data,ext,caption,password,album,public

Rest assured that $data[2] is in fact the data (I've checked it's output to a file). But thanks for the advice for naming, probably much easier than using the numbers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 27 '10, 11:05 PM (#25)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
What happens if you do this (manually force a jpeg content-type) ... since I have no way to test it myself ...

save your original in a safe place and try this in it's place ...
PHP Code:
<?php

$mysql_con 
mysql_connect("127.0.0.1","root","***")
mysql_select_db("pic_db");

$result mysql_query("SELECT * FROM mypic_pics WHERE id = ".$_GET['id']." LIMIT 1");
$data mysql_fetch_row($result);

$image $data['data'];

header("Content-Type: image/jpeg");

echo 
$image;
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert image to mysql ketanco PHP 4 June 18 '09 02:56 AM
Why won't Explorer display this image? johnjm22 Adobe Photoshop Help 9 September 9 '06 02:08 AM
"Image loading" graphic won't display, and one other image gallery problem TheGardener Javascript, AJAX, and JSON 1 July 10 '06 09:06 AM
Random Image Display network traffic with IE6 yardbird Javascript, AJAX, and JSON 0 May 30 '05 12:58 AM

 
User Infomation
Your Avatar

Site Of The Month

Ticket Cake
Ticket Cake

Ticket Cake is a drupal based event ticketing platform. It features that ability to browse events and share them.

Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:33 AM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163