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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Viewing an Image from MySQL

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I read everything here about it (some of this code might look familiar) but nothing seems suitable to my needs so I hope there is a way. I need to loop through the images table, pulling out one or two for presentation on various existing pages using the ID field, which is also being taken care of already in the Select statement for the loop, but all I can get is the source of the image, not the image itself. Here is what I have - there is already a database connection and this is already inside a loop, and the variables for $image are already declared:

[tt]$mime = array("JFIF" => "jpeg",
"GIF89" => "gif");

if ($image != '') {
$content = base64_decode($image);

while(list($key,$val) = each($mime)) {
if(ereg($key,$content)) {
$mime_type = $val;
}
}
$image = &quot;<img src=\&quot;&quot; . $content . &quot;.&quot; . $mime_type . &quot;\&quot;>&quot;;
}[/tt]

I think what I need to do is in the loop, to actually create a physical temp copy of the image somewhere in a temp folder and then after it's presented, to delete it again but I am not sure how to do that. Any ideas? Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Just out of curiosity....

If you're going to have to write the files to the filesystem anyway, why not store the images on the filesystem and put their filenames in the database? ______________________________________________________________________
TANSTAAFL!
 
Thanks, that's what I am doing now. The space used for images counts agains the space I am allowed on the account but the space of MySQL does not. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Some beancounter somewhere missed a trick.

Why not have the &quot;src&quot; attribute of your &quot;img&quot; tag be a separate PHP script that takes an input parameter of some kind and streams out the appropriate image from the database? That way you'd never have to write anything to a temp directory.

You should be able to do something like:

<IMG src=&quot;myDB2ImageScript.php?imageid=2&quot;>

And have myDB2ImageScript.php take the given imageid, fetch the image from the database, and stream out the image. It'll take a couple of tricks getting the headers just right, but it should work.

______________________________________________________________________
TANSTAAFL!
 
That's what I tried originally but as I want to present more than one image at a time to an existing page (with some HTML wrapped around them), I don't think I can do it by &quot;img src&quot;ing another page. My sample code wasn't very clear because it was heavily modified from the version needed for the IDE environment in which the page is being created, so this line:

[tt]$image = &quot;<img src=\&quot;&quot; . $content . &quot;.&quot; . $mime_type . &quot;\&quot;>&quot;;[/tt]

is probably more closely translated as:

[tt]echo = &quot;<img src=\&quot;&quot; . $content . &quot;.&quot; . $mime_type . &quot;\&quot;>&quot;;[/tt]

The IDE generates its own headers so the best I could do was to get a &quot;header already sent&quot; error and besides, the page has lots more on it besides images so a header just for images does not work and that's why I am pretty sure there is no way around creating the temp files. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Sure you can.

Remember, each image on an HTML page is fetched separately from the page itself as a separate communication.

If you use &quot;src&quot; attributes with the appropriate URL-encoded data, then the browser can get the images as separate fetches to the same script. ______________________________________________________________________
TANSTAAFL!
 
I would like to try it both ways so if someone can help with the temp file creation in my loop (and the deletion of same), it will be a place to start. I saw these same debates in another posting so I already know that I should try it both ways before making up my mind. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Two questions on my mind are: Will your temporary images eat up your storage quota? What method will you use to remove the files -- specifically, how are you going to know that the files are no longer needed? ______________________________________________________________________
TANSTAAFL!
 
First, I have plenty of space right now and the images won't be accessed all that much anyway for the time being. But as far as knowing which ones to delete, I was hoping that the loop could delete whatever it created as soon as it loaded them for display. Since the loop itself would also be creating the file names themselves, it should &quot;know&quot; what it created in order to delete them again. That's the thought anyway. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
I think you missed some of the implications of my point about browsers' fetching images separately from the HTML page.

Since the images are fetched separately, you have no way of guarantying that all the images will have been fetched before the HTML-producing script ends. In fact, if you have an IMG tag near the end of your HTML output, you can nearly guaranty that the script will end before the last image is fetched.

Now you need a garbage collection mechanism to clean up the filesystem. How is it going to know that an image is not being used and can be deleted? ______________________________________________________________________
TANSTAAFL!
 
No, I didn't miss this but until I try it, I can't say where in the process it will happen. In this IDE environment, I can pass values to other parts of the code so that it can, for example, delete the extra files before the script ends, which will be well after they are created and used. It will take a separate loop but it can be done. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Will your users be using the IDE to access the images, or a web browser?

How do you know for a fact that the images will have been fetched by the end of the script's run? The web browser isn't going to talk to your script to get the image data -- via your method, the browser is going to fetch the data from the filesystem without any intervention from a PHP script. ______________________________________________________________________
TANSTAAFL!
 
What I really need is some help with the code - please refer to my original posting which has the question that I need answered. When I saw the other posting with the same arguments, I knew this would turn into a debate and while I appreciate the ideas and suggestions, it's not the area in which I am focusing right now, especially since I already tried what you are suggesting and was unable to get it to work within the IDE environment. Maybe it's a limitation of the IDE but since that's what I've used to create all the pages, I am stuck with it. (Yes, I can view the pages within the IDE since it has a browser built-in). Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Okay, let's take a look at your code snippet.

What does $content contain? ______________________________________________________________________
TANSTAAFL!
 
If I understand my own code, $content is the field that contains the image that is in the MySQL table. I based this on the other posting though so I wasn't sure exactly what the base64_decode($content) means. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
I don't understand.

The HTML spec requires that the &quot;src&quot; attribute of an &quot;IMG&quot; tag be the URI to where the file's data can be found. It cannot be the binary data of the file itself. Is that what it contains? ______________________________________________________________________
TANSTAAFL!
 
That is precisely what I am trying to fix. Right now, $content contains the text contents of the image as it is in the BLOB field of the table. What I want to try to do is to get this converted back into an image that can be used in place of $content for presentation to the screen in the src, either by creating a temp image file or by whatever means will do it. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Pick a filename based either on random letters or data from your database.

Use fopen() ( to write the image data from the database to the filename above.

Include the filename as the &quot;src&quot; attribute of your &quot;IMG&quot; tag.


Test it outside the IDE, though. You'll find that if you delete the files at the end of your script, some of them won't get read by the browser.
______________________________________________________________________
TANSTAAFL!
 
How do I go about writing the file (and write it to a particular folder) while being sure that it has the correct file name extension? Some of the images are GIFs while others are JPGs. I know I can use something like time() (maybe in combination with the image ID) to come up with a file name but it is not clear to me how $mime_type is a file name extension as I have it in my code above.

I'll worry about deleting the temp file after I get this first part working. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Everything you need to know about writing to a file is in the manual page for fopen() that I cited.

You give fopen() a filename to open. That filename can have any extension you want -- just build a string that contains the filename.

As far as basing the extension on the mime type of the file, I can only assume that when you inserted your image data into MySQL, you designed your table to include columns for the original filename or mime type. If not, I would add them. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top