// Pick a random number between 0 and the number of quotes, minus 1. We have to minus 1 because the array starts at 0. This number will be the number of the quote we use in the array.
$use_quote = rand(0, count($quotes) - 1);
// Create an image, with the width of 400, height of 75.
$im = @imagecreate(400, 75)
or die("Cannot Initialize new GD image stream");
// Set the background color of our image. We specify the color with its RBG numbers.
$background_color = imagecolorallocate($im, 0, 60, 255);
// Set the text color, the same way we set the background color.
$text_color = imagecolorallocate($im, 255, 255, 255);
// Grab the quote based on the random number we picked earlier, and assign it to $current_quote.
$current_quote = $quotes[$use_quote];
// Now, we use explode, a PHP function, to seperate our $current_quote based on the "|" character. Each part of the quote is now put into the array $line_holder.
$line_holder = explode('|', $current_quote);
// Count the number of lines we have by counting how many elements the array $line_holder contains.
$num_lines = count($line_holder);
// Set a variable, $x, which will be used to count what line we're up to in our while loop bellow.
$x = 0;
// Set a variable that will hold the y value we are up to printing on the image. This allows us to easily do multiple lines.
$cur_y = 5;
// Here, we use the imagestring function to output a string to our image.
// The parameters are as follows: imagestring ( resource image, int font, int x, int y, string s, int col )
// So, we use $im because we're writing this string to our $im image. We use 3 to specify the font size. We use 5 to specify the x point we are starting the string at. We use $cur_y to tell the string to start at that y point. We use 'A cool random quote:' to tell it the text we want to output, and $text_color to specify the text color.
imagestring($im, 3, 5, $cur_y, 'A cool random quote:', $text_color);
// Now, we have to set our current y value to further down our image, to where we want to start our quote.
$cur_y += 14;
// Now, we want to loop through all of the lines of our quote. We use $x < $num_lines because we are starting the loop with x as 0, because the array starts at 0.
while ($x < $num_lines)
// Once again we use the imagestring function. Once again, I'll explain why I used each parameter that I did:
// $im is to specify our image
// 2 is the font size we are using
// 5 is our x point to start the line at
// $cur_y is our current y, which we need in order to make each line of the quote under the previous line, and continue down the image
// $line_holder[$x] takes the line of the quote that we are up to printing based on the current line number we give it with $x. So $line_holder would be the first line of the quote, $line_holder would be the next line of the quote, and so on.
// As before, $text_color is the color we want our text to print out in, defined above
imagestring($im, 2, 5, $cur_y, $line_holder[$x], $text_color);
// Increment our current y value because we need to procede down the image with each new line.
$cur_y += 14;
// Increment our x value because we need to grab the next line of the quote when we go though the loop again.
// Set the content type to be: image/png, which is what we need, because we're about to output an image.
// Finally, output our image
// Free the memory we used for our image
// This line is very important. It writes to the output buffer. We need this in order to trick the browser into thinking we are giving it a real .png image. As in real, one that was not made dynamicly.
// MAKE SURE to change the path to the full path on your server.
Now that our script is done, I'll quickly go over the .htaccess that calls random_quote.php when random_quote.png is requested. This file should be saved as .htaccess and placed in the same directoy as random_quote.php.
Here's the file:
RewriteRule ^(.*).png$ random_quote.php
It's just those two lines. They are very simple. It says if any .png file is called, call random_quote.php instead. That's it. If you have png files you are calling within that directoy, and don't want to call random_quote.php instead, make sure you put this file and random_quote.php in a seperate directoy.
I hope you enjoyed this tutorial!