Page 1 of 1

Image From PHP Function

Posted: Mon Apr 26, 2010 5:07 am
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);
?>

Re: Image From PHP Function

Posted: Mon Apr 26, 2010 5:28 am
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.

Re: Image From PHP Function

Posted: Mon Apr 26, 2010 12:05 pm
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.

Re: Image From PHP Function

Posted: Mon Apr 26, 2010 4:20 pm
by SammyD
Thanks a lot guys :)