Image From PHP Function

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
SammyD
Forum Newbie
Posts: 2
Joined: Mon Apr 26, 2010 5:01 am

Image From PHP Function

Post by SammyD »

Hey all. I have a problem I can't seem to solve! I have a function that adds text to an image. It works fine when I run it by itself but when I run it from a PHP page which has all the <html> tags and such it just prints out text. I believe this is because of the <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> that it is run inside of.

So my question is, how do I call my function?

Here is the function that works by itself.

Code: Select all

<?
function createLottoTicket($ticketNumber) {	
	header ("Content-type: image/pjpeg");
	$stringImage = (string)$ticketNumber;                                             
	$font  = 5;
	$im = ImageCreateFromjpeg("./images/lottoTicket.jpg");
	$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
	$text_color = imagecolorallocate ($im, 0, 0,0);//black text
	imagestring ($im, $font, 24, 40,  $stringImage, $text_color);
	imagejpeg ($im);
}
createLottoTicket(10101);
?>
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: Image From PHP Function

Post by davex »

Hi,

An image is loaded from its own request to the web server - not from within a page with <html> tags. Even if you set the header correctly it would then corrupt.

What you want to do is have two files: page.php and image.php

The image file creates the actual image and outputs it - that is all that it does.

The page file has the actual page and also a <img src="image.php"> image link.

The browser will then load the image file from image.php and display it.

HTH.

Cheers,

Dave.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image From PHP Function

Post by Jonah Bron »

Right. Seeing that you pass a number to the function, the <img> tag would probably look more like this:

Code: Select all

<img src="image.php?n=10101">
Then, you would obviously use $_GET['n'] instead.
SammyD
Forum Newbie
Posts: 2
Joined: Mon Apr 26, 2010 5:01 am

Re: Image From PHP Function

Post by SammyD »

Thanks a lot guys :)
Post Reply