PHP: Random quote image 2005-11-04 20:42:44
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';
THIS TUTORIAL IS CONTINUED ON THE NEXT POST