homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to convert image blob files back to image
shamboo




msg:1265850
 4:07 pm on Jul 8, 2005 (gmt 0)

HI ,

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

 

coopster




msg:1265851
 10:14 pm on Jul 8, 2005 (gmt 0)

Welcome to WebmasterWorld, 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;
>?

maxi million




msg:1265852
 3:55 am on Jul 9, 2005 (gmt 0)

this is not really a reply to shamboo, rather a question actaully.

"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




msg:1265853
 2:12 pm on Jul 9, 2005 (gmt 0)


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.

shamboo




msg:1265854
 11:10 pm on Jul 9, 2005 (gmt 0)

Hi every one,

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

jatar_k




msg:1265855
 4:25 am on Jul 10, 2005 (gmt 0)

look at the notes on this page regarding installation
[php.net...]

maxi million




msg:1265856
 7:31 am on Jul 10, 2005 (gmt 0)

thanks again coopster
tested the script. ya the image is getting stored in the db! im sure that BLOB has its own use.
and thans for starting another thread for the arguments with a lively and interesting post. it is an article by its own right!
:)

coopster




msg:1265857
 5:17 pm on Jul 10, 2005 (gmt 0)

shamboo, you do not need to use imagecreatefromstring() to convert the blob content back to an image. Take a closer look at the example as that is not what is happening here. Do you need to embed the image in a page? If so, continue using your script to do so. Create a script that accepts the image id or whatever key is necessary to get it out of the database and then in your HTML code, use PHP to retrieve the image, using the code provided above as an example. The only difference being, you would use something like the following in your HTML
<p><img src="getImage.php?imageID=<?php print $imageID;?>" /></p>

shamboo




msg:1265858
 3:08 pm on Jul 11, 2005 (gmt 0)

Hi coopster

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

daljitS




msg:1265859
 7:53 am on Jul 12, 2005 (gmt 0)

Hi Shamboo,

"I am getting all the binary junk printed on the screen if i am printing the $row as shown in the above code."

i guess you might have not added this line in your code before printing the $row[image] :

header("Content-type: image/jpg");

try it or post your code.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved