Newgrounds Background Image Theme

Our goal is for Newgrounds to be ad free for everyone! Become a Supporter today and help make this dream a reality!

PHP: Random quote image

3,842 Views | 6 Replies
New Topic Respond to this Topic

PHP: Random quote image 2005-11-04 20:42:44

PHP: Main

PHP: Random quote image

Before you read this tutorial, you should probably know how to use PHP arrays.

This tutorial is great if you would like to learn how to use the PHP GD library to create images dynamicly.

You can see the image you create with this tutorial at work here: http://www.orangefox..tes/random_quote.png

Goal of the project: The goal of this tutorial is to create an image dynamicly with PHP, and display a random quote from a group of quotes we specify. We also want to trick web browsers into thinking that our image is an acual .png image, instead of .php. That was we can display the image with: <img src="random_quote.png"> instead of <img src="random_quote.php">, which a lot of forums wont accept for signatures.

Steps to do this:
1. Create PHP script that selects a random quote from our array of quotes.
2. After we select our quote, we need to break it up on the "|" because we need to tell the image to display the quote on different lines.
3. Then, we need to output each line of our quote to the image.
4. Last, but very important, we need to create a .htaccess file that will say "If random_quote.png" is requested, request the file random_quote.php instead.

In order to make it easier to read, I'll write comments within the code that explain everything.


// Define our quotes array, which holds all of our quotes. Within each quote, "|" seperates the quote onto the next line.

$quotes[] = '"I always tried to turn every disaster into an opportunity."|- John D. Rockefeller';
$quotes[] = '"It\'s been reported that Britney Spears is having financial|trouble and recently took out a $5 million loan. Kevin Federline|offered to co-sign the loan and then everyone had a big laugh."|- Conan O\'Brien';
$quotes[] = '"This week, rapper Snoop Dogg started doing a series of|commercials for Chrysler automobiles. This marks the first time|Snoop has recommended a dealer who sells cars."|- Conan O\'Brien';
$quotes[] = '"David Copperfield has announced plans to impregnate a|woman onstage without touching her. Copperfield calls his new|trick \'The Tom Cruise.\'"|- Conan O\'Brien';
$quotes[] = '"Paris Hilton is denying reports that she slept with|actor Tom Sizemore. Paris said, \'I\'m not even up to the S\'s yet.\'"|- Conan O\'Brien';
$quotes[] = '"It\'s been reported that Kevin Federline is teaming|up with Michael Jackson\'s father to build a dance studio. The|slogan for the studio will be \'Where Lazy Meets Crazy.\'"|- Conan O\'Brien';
$quotes[] = '"Dr. Phil is being sued by three people who claim|that his diet plan is a waste of money and doesn\'t work. Not|surprisingly, they figured this out by looking at Dr. Phil."|- Conan O\'Brien';
$quotes[] = '"It\'s been reported that Leo Sternbach, the inventor of Valium,|died this week. Not surprisingly, Sternbach\'s family is taking|it very well."|- Conan O\'Brien';
$quotes[] = '"This week, President Bush hinted that his next choice for|Supreme Court nominee could be a minority. The President said, \'It|could be a Latino or it could be a Chinese-o.\'"|- Conan O\'Brien';
$quotes[] = '"CNN reported that this week Bill Gates - the world\'s richest|man - received a raise from Microsoft. Gates was excited, saying,|\'With this raise, I can finally buy that Canada I\'ve had my eye on.\'"|- Conan O\'Brien';
$quotes[] = '"Fox News reports that Michael Jackson asked Stevie Wonder to|appear on his new album, but Stevie turned him down. Stevie\'s|exact quote was, \'I\'m blind, not crazy.\'"|- Conan O\'Brien';
$quotes[] = '"In a recent interview, Martha Stewart says she didn\'t get|into any fist fights in prison. Martha says she preferred to kick|people to death."|- Conan O\'Brien';


Response to PHP: Random quote image 2005-11-04 20:44:30

// 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[0] would be the first line of the quote, $line_holder[1] 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.

$x ++;

// Set the content type to be: image/png, which is what we need, because we're about to output an image.

header("Content-type: image/png");

// 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:

RewriteEngine on
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!

Response to PHP: Random quote image 2005-12-06 16:26:06

Nice. I've always wondered how to do that =P
I better start learning PHP

BBS Signature

Response to PHP: Random quote image 2005-12-06 17:22:57

Pretty well written tutorial, but it's most common to use mt_rand() versus rand(). I've found it's "better" at making numbers random.

Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to PHP: Random quote image 2005-12-07 11:57:46

Instead of manually inserting where newlines should be, you could calculate it using a script, seeing as in that case this font is fixed width. Besides even if it weren't, there wouldn't be much difference.

Great tutorial though. I need to start learning this damn GD Lib. But I never found a good use for it.

BBS Signature

Response to PHP: Random quote image 2005-12-07 12:47:24

Cache the result if your are going to use autolinewrap.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

Response to PHP: Random quote image 2005-12-07 14:58:18

At 12/7/05 11:57 AM, Sir-Davey wrote: Instead of manually inserting where newlines should be, you could calculate it using a script, seeing as in that case this font is fixed width. Besides even if it weren't, there wouldn't be much difference.

That would probably be a cool thing to add. One of the reasons I didn't do auto originaly was because people might want to break lines after one word, because the script can be used for other things, not only quotes. Or you might want to put a title above all the other text. Maybe a combination of auto line parsing and line break on the "|" would be good.

Glad you like it!