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
nrmstudios
Forum Newbie
Posts: 5 Joined: Tue Jul 15, 2008 10:37 pm
Post
by nrmstudios » Thu Feb 18, 2010 2:59 am
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);
}
?>
Eran
DevNet Master
Posts: 3549 Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME
Post
by Eran » Thu Feb 18, 2010 3:28 am
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
Post
by nrmstudios » Thu Feb 18, 2010 3:35 am
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?????
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Feb 18, 2010 3:37 am
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.
timWebUK
Forum Contributor
Posts: 239 Joined: Thu Oct 29, 2009 6:48 am
Location: UK
Post
by timWebUK » Thu Feb 18, 2010 3:52 am
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
Post
by nrmstudios » Thu Feb 18, 2010 5:41 am
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
Post
by nrmstudios » Thu Feb 18, 2010 5:53 am
Hey Guys, I got it working thanks for all your help! God Bless!