Welcome to WebmasterWorld Guest from 54.161.228.30

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to convert blob to image in php

   
5:30 am on Jun 6, 2006 (gmt 0)

5+ Year Member



I am using Smarty as templates in my site...I have images stored as blob in db i can get this blob to image using header(content-type: image/jpg) but i wann to assign this image to smarty variable How to do that? please help me
12:08 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcomme to WebmasterWorld, bhavnagadhiya.

if ($row = mysql_fetch_array(mysql_query('SELECT image FROM imageMySQL'))) { 
$image = $row['image'];
}

You just assign it as you would any other variable.
12:16 pm on Jun 6, 2006 (gmt 0)

5+ Year Member



Thank you...

its printing the binary data itself in page

12:18 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, and it will unless you send the proper mime type out prior to it's display.
header("Content-type: image/jpg"); 
print $row['image'];
12:25 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Also, there is a thread in our PHP Forum Library [webmasterworld.com] that discusses How to convert image blob files back to image [webmasterworld.com] which you may find useful.
12:28 pm on Jun 6, 2006 (gmt 0)

5+ Year Member



its same thing coopster

see i am displaying this templates variable only in to html page i cant use header in html page u know that...

i cant use headet and print function in my tpl(html/templates)..

3:27 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



You should consider changing this setup. Storing images as blobs in dbs is:

1. space inefficient;
2. resource inefficient (time to http display an image v. db query plus php process plus http)
3. and difficult to maintain.

A faster/cleaner setup is to setup a file server, save the image there and just save the image location, name, size and lastupdatedatetime to the db.

7:13 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



bhavnagadhiya, you are correct, but if you study the discussion given in my last post you will see how you can accomplish what you desire. You have a php script that accepts an image identifier and retrieves it from the database table. Then, at the end of that script you print out the appropriate header as well as the binary image data. You refer to this script in your <img> element in the HTML. Read the example again carefully and you will see how it is done.