Page 1 of 1

Incredibly Nooby php image display question

Posted: Wed Jun 24, 2009 8:37 am
by Lobsta
ok, I am designing a website, and i felt like playing around with php to see if it would make it easier... and i seem to be having the stupidest of problems. I am currently practicing simple stuff to make sure i understand it before applying it to my (mastly designed) website. and i have hit the wall of not being able to display a picture... i have tried fopen --> fread --> echo/print, and imagecreatejpeg --> imagejpeg and in both cases, im pretty sure it is finding the image and all that, but it returns just a page of gibberish, like the stuff that is displayed if you open an image in notepad.

Code for those that are interested: (this is just a muck around/sandbox thingo that i have hosted to play about with stuff)

Code: Select all

 
<html>
<body>
<?php
$file=fopen("test.txt","r") or exit("Unable to open file!");
$output=fread($file, filesize("test.txt"));
fclose($file);
echo $output;
$image=imagecreatefromjpeg("image1.jpg");
imagejpeg($image);
?>
</body>
</html>
 
for the fopen attempt, it was a straight copy paste of the code for the test.txt one above (which works fine), with changed variable names. the source files are all in the same directory on my hosting.

if any more info is needed, leave us a reply.

Lobby

Re: Incredibly Nooby php image display question

Posted: Wed Jun 24, 2009 12:42 pm
by iBroughtCookies
Right now your page wrote the image into text, and it's displaying it as text.
You need to put this in there:

Code: Select all

header('Content-Type: image/jpeg');
HOWEVER, what this will do, is this will open the entire PAGE as an image. You cannot have this AND text. This is like opening a picture like this:
http://www.google.com/intl/en_ALL/images/logo.gif
It's opening the image as an image in firefox.
If you want the image AND text, I.E.

Code: Select all

<?php
echo 'Hello word<br />';
IMAGEHERE
echo '<br /> text after image';
?>
You would need to use imagejpeg() to SAVE the image, and then use <img> to open that image up. In this case, you would not use the header() function.