Using PHP to output an image, AND some text [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
$skills=NULL
Forum Newbie
Posts: 9
Joined: Fri Feb 27, 2009 1:18 pm

Using PHP to output an image, AND some text [SOLVED]

Post by $skills=NULL »

I'm new to PHP; I've messed with Perl a bit, but most of my background is in C. I've scoured the php.net documentation to death, and tried a million different combinations of things to try to get this to work, with no success.

I have a PHP form that will accept image uploads. For security purposes, it moves the image outside of the WWW root, saves it under a random filename, connects to a MySQL database, stores the filename with a unique record number, and returns that record number. To view the image, I have another PHP form that accepts a record number, looks up that record in the database, reads the image from the filename in that record, and displays it.

That all works fine. I use readfile() to output the contents of the image file to the screen. The problem is, there is a piece of text that also gets stored in the database record when the image is uploaded, and I want to display that along with the image. I've spent hours messing with header() and readfile() and content types and boundaries and output buffers and everything else, and if I try to display the image AND the text, I either get an internal server error or the image is broken. Or I get a huge encoded image string.

Ideally I'd like to have PHP spit out an HTML page with the image in it, but I'd settle for outputting the image and a line of plain text. What can I do?

Thanks :)
Last edited by $skills=NULL on Fri Feb 27, 2009 5:41 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Using PHP to output an image, AND some text

Post by pickle »

You can't display image data and text in the same PHP file - you need different headers.

So, have 1 file that acts like an image proxy. Basically you pass it the filename of the image you want to display, it uses readfile() & header() to pretend to be that image file.

Have a second PHP file that the user looks at. That PHP file displays the text you need, as well as calls the image proxy PHP file - just like it would refer to a regular image.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
$skills=NULL
Forum Newbie
Posts: 9
Joined: Fri Feb 27, 2009 1:18 pm

Re: Using PHP to output an image, AND some text

Post by $skills=NULL »

OK. So, something like this might work?

viewimage.php

Code: Select all

 
<?php
...some HTML markup...
 
<img SRC="getimage.php?row=1">
 
...the text I want to display...
 
...end of HTML markup...
?>
 
getimage.php

Code: Select all

 
<?php
...fetch filename from database with $row...
 
header('Content-type: image/jpeg');
readfile($filename);
?>
 

edit: Yep, that did. MUCH thanks, you saved me a lot more frustration. :)
Post Reply