Cant display Jpeg

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
nrmstudios
Forum Newbie
Posts: 5
Joined: Tue Jul 15, 2008 10:37 pm

Cant display Jpeg

Post 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);
}
 
?>
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Cant display Jpeg

Post 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
nrmstudios
Forum Newbie
Posts: 5
Joined: Tue Jul 15, 2008 10:37 pm

Re: Cant display Jpeg

Post 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?????
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cant display Jpeg

Post 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.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Cant display Jpeg

Post 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()
nrmstudios
Forum Newbie
Posts: 5
Joined: Tue Jul 15, 2008 10:37 pm

Re: Cant display Jpeg

Post 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.
nrmstudios
Forum Newbie
Posts: 5
Joined: Tue Jul 15, 2008 10:37 pm

Re: Cant display Jpeg

Post by nrmstudios »

Hey Guys, I got it working thanks for all your help! God Bless!
Post Reply