 |
|
January 26 '10, 03:51 AM (#1)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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.
|
|
January 26 '10, 10:00 AM (#2)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
If you're storing your images in MySQL,
what is the fwrite portion for? I'm confused.
|
|
January 26 '10, 01:03 PM (#3)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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.
|
|
January 26 '10, 01:09 PM (#4)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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.
|
|
January 26 '10, 01:27 PM (#5)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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];
?>
|
|
January 26 '10, 01:39 PM (#6)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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?
|
|
January 26 '10, 01:52 PM (#7)
|
|
|
WDF Moderator
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
|
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");
|
|
January 26 '10, 01:53 PM (#8)
|
|
|
WDF Moderator
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
|
If the above doesn't work, try adding a Content-Length header (just do a strlen() on the image data to get that value).
|
|
January 26 '10, 02:44 PM (#9)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
Steven ...
I missed this one: image/jpeg (that might be it).
|
|
January 26 '10, 07:43 PM (#10)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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.
|
|
January 26 '10, 08:35 PM (#11)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
Show us the actual page ... where the image is supposed to appear.
And also the PHP scripting that creates that page?
|
|
January 26 '10, 11:25 PM (#12)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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.
|
|
January 27 '10, 12:20 AM (#13)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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.
|
|
January 27 '10, 04:12 AM (#14)
|
|
|
Freelance
Join Date: June 2009
Location: Destin Florida
Posts: 905
|
ok dude. your not actually echoing the image like this
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.
|
|
January 27 '10, 08:45 AM (#15)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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').
|
|
January 27 '10, 09:42 AM (#16)
|
|
|
WDF Moderator
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
|
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.
|
|
January 27 '10, 09:48 AM (#17)
|
|
|
WDF Moderator
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
|
There's some funky stuff going on in that image. Can you attach the original so I can compare?
|
|
January 27 '10, 09:52 AM (#18)
|
|
|
WDF Moderator
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
|
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.
|
|
January 27 '10, 10:22 AM (#19)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
And also give us the listing for this script: get_pic.php
scratch-out any MySQL login stuff.
|
|
January 27 '10, 11:06 AM (#20)
|
|
|
Freelance
Join Date: June 2009
Location: Destin Florida
Posts: 905
|
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.
|
|
January 27 '10, 11:36 AM (#21)
|
|
|
Freelance
Join Date: June 2009
Location: Destin Florida
Posts: 905
|
are the other images on the page or is this being tested as standalone?
|
|
January 27 '10, 06:35 PM (#22)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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
|
|
January 27 '10, 07:48 PM (#23)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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.
|
|
January 27 '10, 10:34 PM (#24)
|
|
|
WDF Member
Join Date: June 2009
Posts: 21
|
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.
|
|
January 27 '10, 11:05 PM (#25)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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;
?>
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
| Advertisement |
|
|
|