Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading image in PHP from MySQL db

Status
Not open for further replies.

miguelgm

Programmer
Aug 28, 2001
17
ES
Hi!!

I'm working PHP and MySQL. JPG's images are being stored in this db (blob field). I need to know how to show them in a PHP page.
I access well to db.
My problem begins when I try to access data in &quot;Image&quot; field: I have built an index.php which calls image.php (echo(&quot;<img src='image.php'>&quot;);), but image.php doesn't work.
I've got ASP code, and works well, but I need to translate to PHP (ASP aaaarg!)
This is a portion of the ASP code:
Set lFieldImage = rs.Fields(&quot;Fotografia&quot;)
Response.ContentType = rs.Fields(&quot;image/jpg&quot;)
lSize = lFieldImagen.ActualSize
lread = 0
Do While lSize > lRead
Response.BinaryWrite lFieldImage.GetChunk(2000)
lRead = lRead + 2000
Loop
Response.End

Thanks!!
 
Here is a simple way of doing it:
Code:
<?php
$mime = array(&quot;JFIF&quot; => &quot;jpeg&quot;,
              &quot;GIF89&quot; => &quot;gif&quot;,
              &quot;PNG&quot; => &quot;png&quot;);

$con = @mysql_pconnect(&quot;host&quot;,&quot;user&quot;,&quot;pass&quot;);
@mysql_select_db(&quot;inlandpac_com&quot;,$con);

if(!$id) {
     $id = &quot;blank&quot;;
}

$q = &quot;SELECT content FROM images WHERE name='$id'&quot;;
$r = mysql_query($q) or die(&quot;could not query $id&quot;);

$c = mysql_fetch_row($r);
$content = base64_decode($c[0]);

while(list($key,$val) = each($mime)) {
     if(ereg($key,$content)) {
          $mime_type = $val;
     }
}

header(&quot;Content-type: image/$mime_type&quot;);
echo $content;
?>

the above takes in an id, loads the corresponding id from mysql, then spits out the image. The above will also try to automatically detect whether the image is a GIF, JPEG, or PNG.

Now, to use the above script, we have to get all of the ids from our mysql database and use them by making a call to the above script like this: <img src=&quot;image.php?id=$image_id&quot;>
Code:
<?php
$con = @mysql_pconnect(&quot;host&quot;,&quot;user&quot;,&quot;pass&quot;);
@mysql_select_db(&quot;inlandpac_com&quot;,$con);

$q = &quot;SELECT name FROM images&quot;;
$r = mysql_query($q) or die(&quot;could not query!&quot;);

?>
<html>
<head>
<title>image</title>
</head>
<body>
<table border=&quot;0&quot; cellspacing=&quot;4&quot; cellpadding=&quot;0&quot; align=&quot;center&quot;>
  <tr>
<?php
while($c = mysql_fetch_row($r)) {
     echo &quot;<td align=\&quot;center\&quot; valign=\&quot;middle\&quot;><img src=\&quot;image.php?id=$c[0]\&quot;></td>\n&quot;;
}
?>
  </tr>
</table>
</body>
</html>

You can see this in action here:

Hope this helps.
Chad. ICQ: 54380631
 
Thanks. It works!!!!!!!.
Easy, easy, easy.......... :)
 
Why is everybody putting images in a dB? Its hopelessly inefficient... Can someone tell me for what reason this is done? I just dont get it, i think. mcvdmvs
-- &quot;It never hurts to help&quot; -- Eek the Cat
 
There are more reasons NOT to do it than to do it.

Some advantages are: less strain on filesystem, completely indexable and searchable, more accurate stats.....

Some disadvantages: slow, takes up a huge load on the server, slows MySQL down, the more queries, the slower the returns, etc.

But, to each his own. I have found it more opportunistic and efficient to utilize both a database (to store information about an image including searchable keywords, name, etc.) and the filesystem (to store the image itself).

chad. ICQ: 54380631
 
Chad, exactly my point. Use the db for info and put the url to the image in the db, and upload ure images on the filesystem.
Bout the advantages: a little less strain on filesystem but not comparable to the strain for mysql.
how u want to index a image, by beginning of gif89a? Its just raw code, so indexing it would help u with nothing. And in our way by putting the images on the filesystem and write a function for getting images u can raise a counter in the db for the image. Also ure stats would than be correct.

I guess we find the same advantages for NOT putting images in a database

greetz Mick
ICQ 48133714 mcvdmvs
-- &quot;It never hurts to help&quot; -- Eek the Cat
 
I use both ways ...
On my main server I store the image in the filesystem because I get about 500K-800K hits a day and need all the speed I can get.

But ... I have an image server that is used by our PR department. This server gets less then 100 hits a day and is used only for storing and indexing images for the PR department. To simplify how the files were stored ... I just dump them into the database along with their index keys, access stats, etc. It is cleaner.
 
perfect examples for each case. Thank you. My point exactly, and I do the same thing myself!

Chad. ICQ: 54380631
 
Oke, oke, thats a solution, but i think not the right one. Its simple to build it that way and easy to use, however i dont think its quite correct. Maybe its cleaner, but not that clean, if you are misusing a db. (harsh words, but i really dont think a db should be used for images).

I think its just an easy way to solve a problem, not cause its right but because it can be done that way. mcvdmvs
-- &quot;It never hurts to help&quot; -- Eek the Cat
 
Well, I think the point that we are getting across here is that you should NOT use a database for images, but rather store pertinent, searchable information about them in the database. Images themselves should be stored as usual within the filesystem.

One example is:
+------------+---------------+----------------------------+
| image_name | description | location |
+------------+---------------+----------------------------+
| star.gif | a yellow star | /images/user/ |
| square.gif | a red square | /images/user2/ |
| circle.gif | a blue circle | /images/user7/ |
| dot.gif | a black dot | /images/user3/ |
+------------+---------------|----------------------------+


Now we have a database that holds the image name, a description, and the location for the image. Users can now search for specific images using very basic query methods (such as 'SELECT image_name,description,location FROM my_images WHERE description LIKE '%$keyword%').

Chad.

ICQ: 54380631
online.dll
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top