Page 1 of 1

One Image Display Routine Works, The Other Does Not

Posted: Tue Apr 29, 2003 3:13 pm
by Wayne Herbert
In the first routine, I use two scripts. imageview.php calls getfile2.php, and this correctly displays the jpeg image.

imageview.php

<html>
<body>
<?php
/* An extremely simple script to show a jpeg */
echo "<img src=\"getfile2.php\">";
?>
</body>
</html>

getfile2.php

<?php
Header("Content-Type: image/jpeg");
$picfilename = "brian.jpg";
$picfile = fopen($picfilename, "rb");
$filecontents = fread($picfile, filesize($picfilename));
echo $filecontents;
?>

Interestingly enough, this works whether or not the Header line is commented out or not. Why would that be?

Now, I attempted to combine the two scripts into one by making getfile2.php a function. Now, it will not display an image. Why is that? It does not matter whether I 'return' or 'echo' the result.

testfunc.php

<html>
<body>
<?php
// a small function to display the contents of a file
function viewfile()
{
Header("Content-type: image/jpeg");
$picfilename = "larry.jpg";
$picfile = fopen($picfilename, "rb");
$filecontents = fread($picfile, filesize($picfilename));
return $filecontents;
// echo $filecontents;
}

/* An extremely simple script to show a jpeg */
echo "<img src=\"viewfile()\">";
?>
</body>
</html>

Posted: Tue Apr 29, 2003 4:27 pm
by pootergeist
1. quite a few browsers will render images fine without being sent a preceeding mime header telling them what is coming - note though that a fair number will not render the image.

2. the reason you cannot just output a binary image into html is a matter of headers and encoding - html is your bob standard text, whereas an image is binary (as has to be interpreted as binary). when the page is requested a header is sent - one that says Content-type: application/octet-stream or something (maybe a proper html-text header) - anyway, that'll be very different to an image header

3. You can get sneaky and output the image data as img src="/-/image-jpeg,base64_encode; data......" or similar, though that only works for images below 1KB and is even quirkier than sending headerless images.

4. readfile($image_pointer) would be a faster way of outputting the file btw - beats fopen-fread-fclose

Posted: Wed Apr 30, 2003 9:56 am
by Wayne Herbert
Thanks for the readfile tips. I have found that the image header is mandatory for Netscape, and sometimes for IE... depending if an image was shown last, and the only thing on the page.

Still have one open question, though... why do the routines work as two separte scripts but not work when I convert the called script to a function?

TIA

Posted: Wed Apr 30, 2003 3:39 pm
by Wayne Herbert
Thanks for the readfile tips. I have found that the image header is mandatory for Netscape, and sometimes for IE... depending if an image was shown last, and the only thing on the page.

Still have one open question, though... why do the routines work as two separte scripts but not work when I convert the called script to a function?

TIA

Posted: Wed Apr 30, 2003 4:02 pm
by toms100
because if you are displaying an image, you CANNOT give out text at the same time.. one is binary as said above and the other is normal text. it will screw things up. think of the image generation php file as an actual image.jpg. you cant go putting html tags in that now can you!

OK... CAll Me Dense, But...

Posted: Wed Apr 30, 2003 5:21 pm
by Wayne Herbert
But there must be something about functions I don't understand.

In my first example, the main script calls the getfile2.php script via the line

echo "<img src=\"getfile2.php\">";

Then in the getfile2.php script I issue the header statement for a jpeg, and echo the contents of the jpeg file. So this makes sense... issue the header, then send the data.

When I combine the two scripts, I call the function vai the line

echo "<img src=\"viewfile()\">";


Now the contents of the getfile2.php script and the viewfile() function are indentical... issue a header, read a file and return/echo the file data.

So, I had thought that the order of outputting would be identical in either format... but there is clearly something about the function output I do not understand. If I issue the header line and echo the data from the function, why is that not equivalent to what I have done in a separate script.

Your patience and teaching skills are appreciated.

Posted: Wed Apr 30, 2003 5:34 pm
by volka
your first approach results in two documents (and therefor two http-request/responses)
With the first request the browser receives an html-document that contains (among other things)

Code: Select all

<img src="getfile2.php">
When the browser processes the document it will notice the img-tag and request the document pointed to by the src-property. Second request->second response->second document. In this case hopefully an image.

if the browser receives something like <img src="viewfile()"> it cannot handle it for several reasons.
src is supposed to be an url like http://www.serv.er./the/image.gif not a function-call.
Even if it would be allowed to place a function-call there the browser would not be aware of the function because the code resides on the server-side script, the browser never sees it.
Even it would receive the function code there are very few (marginal) browsers having a client-side php parser installed. So the code would be useless for the browser.