Forum Moderators: coopster
Iam working with image databases
I created a table named Image , which contains the blob field containing all the binary information(bytes) along with some other fields.THis binary infomation corresponds to some images.Each record contains binary information w.r.to one image.There are may blob records(images). how could i read back the blob content and present it as an image on the web
please help me
Thanks
Shamboo
Arguments for/against storing images in a database aside, you could read it directly by using a script ...
<?php
// after connecting to and reading the row from the table
$image = $row['myimage'];
header("Content-type: image/gif"); // or whatever
print $image;
exit;
>?
"Arguments for/against storing images in a database aside..."
coopster could you please explain this part. honestly ive never used "blob" before. cant say i know a lot about it either. when i need to display images by querying the db i do it by storing the path of the image. what ive gathered about blob is that its possible to store the whole image(!) is it so?
would i be doing it wrong if i stored the path in the db rather than storing the image itself?(not in terms of functionalities or ease of editing/manipulating but in a programming sense)
coopster could you please explain this part.
Sure, but how about in a new thread:
Storing Images in the filesystem versus a database [webmasterworld.com]
what ive gathered about blob is that its possible to store the whole image(!) is it so?
It is so. Here is a very basic and simple test script to demonstrate. (Copy a small jpg into the same directory in which you save this script). This script uses a table named 'imageMySQL' so if you already have that table setup in your 'test' database, it is going to get blown away -- rename it if necessary! Change the appropriate variable settings ...
<?php
error_reporting(E_ALL);
$my_table = 'imageMySQL';
$my_image = 'example.jpg';
$my_server = 'localhost';
$my_user = 'username';
$my_pwd = 'password';
$my_name = 'test';
$my_link = mysql_connect($my_server, $my_user, $my_pwd)
or exit('Could not connect (' . mysql_errno() . '): ' . mysql_error());
$my = mysql_select_db($my_name, $my_link)
or exit('Could not select database (' . mysql_errno() . '): ' . mysql_error());
$image = file_get_contents("$my_image");
mysql_query("DROP TABLE IF EXISTS $my_table");
mysql_query("CREATE TABLE $my_table (image MEDIUMBLOB)");
mysql_query("INSERT INTO $my_table (image) VALUES ('".addslashes($image)."')");
$row = mysql_fetch_array(mysql_query("SELECT image FROM $my_table"));
mysql_query("DROP TABLE IF EXISTS $my_table");
header("Content-type: image/jpg");
print $row['image'];
?>
would i be doing it wrong if i stored the path in the db rather than storing the image itself?(not in terms of functionalities or ease of editing/manipulating but in a programming sense)
As with any application, it is more than likely going to be decided by the application analysis and design. The other discussion should allow you to make your own decisions on this. If the question is not asked or addressed in there, feel free to ask and see what type of responses are received.
Storing the path to the image is also a good technique .But storing the complete image in a blob is a different thing.
Converting the blob content back to some is done by a function which you all know i.e imagecreatefromstring(); . But iam getting some problems with the PHP4 configuration.To use these funcitons PHP need have GD library . BUT how to compile PHP4 with GD library?Iam using DEBIAN LINUX . which package i should load for GD lib . By using apt-cache search in linux iam getting many packages.
please if some one could help me
Shamboo
<p><img src="getImage.php?imageID=<?php print $imageID;?>" /></p>
I am getting all the binary junk printed on the screen if i am printing the $row as shown in the above code.
The thing is,
I have a table Image , in that i have a field named image which contains binary content for each image.I loaded many images into the table with imageID as the key. I need to get the original back from that binary ,so that i could display on the webpage.
please help me. i saw a function for doing this in php that is imagecreatefromstring()
If you need code i would post it
Thanks
Shamboo