Hello all. I found this code on the net regarding how to make a random quote generator. Anyway, this code creates an image that pulls its text from a quotes.txt file.
Anyway, it pulls the information just fine. However, I want to be able to INCLUDE this file into another php file. I don't know exactly how, since this file sends HEADER code to the browser, which will probably make things a bit sticky...
More importantly, I am interested to hear how I can generate this image and force it to have a transparent background. Now, due to the version of my GD, I cannot create a GIF. I can however create a PNG file. Does anyone know a way to force the PNG to have a transparent background? That way I can include it in my other PHP file without problem. If not, I can probably make do.
Thanks for your time!!!
Here is the code:
<?php
/*
Quote generator 0.01
This scripts generates a gif image produced on the fly with the gd-library.
I used it to get quotes from a file and display them in an image. This was made to
make it possible for "out-site-users" to show the quotes i have collected.
It expects to get the following variables sent to it:
$filename : the name of the file containing quotes or other things separated with #.
$path_to_font_file : the path to the true type font file
$steal : (optional) This makes it possible to output a specific line in the file. Good for debugging.
The script is limited to output 3 lines(and 1 static line) in the image. It can easily be changed by doing it
bigger and add more lines at appropriate places.
The script could be included on a page by calling it with a regular <img src="/path to script">.
Copyleft(L) 1999 Eric Persson, [email]eric@persson.tm[/email], [url]http://www.persson.tm/scripts/[/url]
*/
//this code below sends headers to the b
// rowser to make it think its always updat
// ed and should not be taken from cache.
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); // always modified
header("Cache-Control: no-cache, must-revalidate");// HTTP/1.1
header("Pragma: no-cache");// HTTP/1.0
//code below puts each line in to the array $file_content
$filename="quotes.txt";
$path_to_font_file="font.ttf";
$fd = fopen("$filename", "r"); //opens the specified file
while ($file_content[] = fgets($fd, 4096)) { //this limits the file(test.rem) to 4096 bytes. 4096 bytes is allocated in memory. Can be increased to load bigger files.
}
fclose($fd);
if(isset($steal)){ //makes it possible to skip the randomizer and pick up a specific row.
$test=$steal;
}else{
$arraysize=sizeof($file_content); //gets the size of the array
sRand(date("s")*(date("H")+1)); //includes the seconds and hours in the randomizer to "garantuee" it wont display same things in a row
$random=(1 + Rand(0,$arraysize-3));
$random=floor($random); //lowers the value of $random since its hard to take a half line. :-)
};
$selected_line=$file_content[$random];
$splitted_line = split( "#", $selected_line, 5 ); //splits the line at the occurrencies of #
Header("Content-type: image/gif"); //sends a imagetype header to the browser.
$im = ImageCreateTrueColor(250,45); //decides the size of the image created
$black = ImageColorAllocate($im, 255,255,255);
$white = ImageColorAllocate($im, 0,0,0);
$grey = ImageColorAllocate($im, 153,153,153);
$grey2 = ImageColorAllocate($im, 200,200,200);
ImageTTFText($im, 10, 0, 2, 10, $grey2, "$path_to_font_file", "$splitted_line[0]");
ImageTTFText($im, 10, 0, 2, 20, $grey2, "$path_to_font_file", "$splitted_line[1]");
ImageTTFText($im, 10, 0, 2, 30, $grey2, "$path_to_font_file", "$splitted_line[2]");
ImageTTFText($im, 10, 0, 2, 40, $grey2, "$path_to_font_file", "$splitted_line[3]");
Imagepng($im);
ImageDestroy($im);
?>