Page 1 of 1

Cant display Jpeg

Posted: Thu Feb 18, 2010 2:59 am
by nrmstudios
I'm trying to load a JPEG, write a caption on it and display it. The code below just displays the image as text:
"????JFIF?? o_M+?emh?E??(?????U????~9|??>???????U?>???o?.???h?????D???/??::E??-V?þ6???w??:%??x?? ??_??1?|?g???????????h?|K?_??_n????????y? C?_ h??H?m|C???*??_????..l??_???O??/?~???/?(????C??<???? ??x???'?/ ?? ~xB?P?w??5t;???w??Q?????? .... "

Where did I go wrong?

Code: Select all

 
<?php
$cap = "Hello World!";
caption("bg.jpg", $cap);
 
function caption($bg_file, $cap)
{
// create image
 $img = imagecreatefromjpeg($bg_file);
 $red = imagecolorallocate($img, 255,0,0);
 
// Write the string at the top left
 imagestring($img, 5, 10, 10, $cap, $red);
 
// Output the image
 header('Content-Type: image/jpeg');
 imagejpeg($img, NULL, 100);
 imagedestroy($img);
}
 
?>
 

Re: Cant display Jpeg

Posted: Thu Feb 18, 2010 3:28 am
by Eran
make sure you have no whitespace before the opening php tag, as it will prevent the headers from being sent. You should turn on error_reporting in your php configuration if you haven't do so yet

Re: Cant display Jpeg

Posted: Thu Feb 18, 2010 3:35 am
by nrmstudios
Ah the problem was the single quotes around the content type:

Code: Select all

header('Content-Type: image/jpeg');
was changed to:

Code: Select all

header("content-type: image/jpeg");
and it works!

NEXT QUESTION:

I want to display the image in the middle of a form. How can I do that if I cant have any output before the header call?????

Re: Cant display Jpeg

Posted: Thu Feb 18, 2010 3:37 am
by requinix
Changing the quotes did not fix it. Something else happened.

Put this image script in one file then reference it from the form with an <img> tag.

Re: Cant display Jpeg

Posted: Thu Feb 18, 2010 3:52 am
by timWebUK
nrmstudios wrote:I want to display the image in the middle of a form. How can I do that if I cant have any output before the header call?????
Reference the image file in HTML tags, for example, <img src="image.php" alt="image" />.

If you ever want to control output to the page, for example, HTML before headers, look into ob_start() and ob_flush()

Re: Cant display Jpeg

Posted: Thu Feb 18, 2010 5:41 am
by nrmstudios
timWebUK wrote:
nrmstudios wrote:I want to display the image in the middle of a form. How can I do that if I cant have any output before the header call?????
Reference the image file in HTML tags, for example, <img src="image.php" alt="image" />.

If you ever want to control output to the page, for example, HTML before headers, look into ob_start() and ob_flush()
How do you pass arguments to it then. I need to send the caption that I want to have printed on the image.

Re: Cant display Jpeg

Posted: Thu Feb 18, 2010 5:53 am
by nrmstudios
Hey Guys, I got it working thanks for all your help! God Bless!