Liquid Nitrogen Blaster

Group: Ice Age Members
Posts: 185
Member No.: 475
Joined: 16-August 04
Ice Cubes: 5

|
It's been a while since I posted here but I managed to get my dynamic sig working a while back and now I'll share the code.
Firstly, the files I have in my dynamic sig folder.
| CODE | dynamic_sig | | +--- .htaccess | | +--- index.html | | +--- official_sig.png | | +--- text.txt
|
index.html is just a redirect to my website homepage. Nothing terribly complex or relevant there.
.htaccess is there so that the png file is treated as php.
| CODE | <Files *.png> ForceType application/x-httpd-php </Files>
|
official_sig.png is the dynamic signature image. It goes as follows.
| CODE | <?php // tell the user's browser that it is an image header("Content-type: image/png");
header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false);
//Get quotes $lines = file('text.txt');
//Count quotes $num_quotes = count($lines); $num_quotes_minus1 = $num_quotes - 1;
//Choose random quote mt_srand((double)microtime()*1000000); $quotes_choice = mt_rand(0, $num_quotes_minus1); $text = $lines[$quotes_choice]; $text = trim($text);
//get the length of the string $text_length = strlen($text);
//max line length is 65
//Create image $lines_needed = $text_length / 65; $lines_needed = ceil($lines_needed); $height = $lines_needed * 17;
$image = @ImageCreate(500, $height) or die("Cannot Initialize new GD image stream");
//select random colors mt_srand((double)microtime()*1000000); $result = mt_rand(0, 5);
switch($result) { case 0: $bg_color = imagecolorallocate($image, 0, 0, 0);//black $text_color = imagecolorallocate($image, 255, 255, 255);//white break; case 1: $bg_color = imagecolorallocate($image, 0, 0, 0);//black $text_color = imagecolorallocate($image, 255, 0, 0);//red break; case 2: $bg_color = imagecolorallocate($image, 0, 0, 0);//black $text_color = imagecolorallocate($image, 0, 255, 0);//green break; case 3: $bg_color = imagecolorallocate($image, 0, 0, 0);//black $text_color = imagecolorallocate($image, 0, 255, 255);//blue break; case 4: $bg_color = imagecolorallocate($image, 192, 192, 192);//grey $text_color = imagecolorallocate($image, 0, 0, 0);//black break; case 5: $bg_color = imagecolorallocate($image, 192, 192, 192);//grey $text_color = imagecolorallocate($image, 0, 0, 255);//blue break; }
//apply background color ImageFill($image, 0, 0, $bg_color);
//Break string up into words $explode_text = explode(" ", $text);
//Re-construct the exploded string into lines $line_length = 0; $line = 0; foreach ($explode_text as $token) { $token_length = strlen($token); $output_text[$line] .= $token; $output_text[$line] .= " "; $line_length = $line_length + $token_length + 1; if ($line_length > 62){ $line++; $line_length = 0; } } // define the font, x_position // how much the y_position changes (increments) $font = 3; $x_pos = 5; $y_inc = 5;
$line_number = 1;
//Output each line to the image $line_number = 1; foreach ($output_text as $print) { if ($line_number == 1) {$y_inc = 1;} else {$y_inc = 17;}; imagestring($image, $font, $x_pos, "$y_inc * $line_number", $print, $text_color); $line_number++; };
// Display the image imagepng($image); imagedestroy($image); ?>
|
Hopefully the notes will help but I'll go into a little bit of detail at the end.
text.txt is where I keep the text I want to display
| CODE | . . . File not found. Should I fake it? (Y/N) For any problem there is a solution that is simple, quick, and ultimately worse than the problem. Hardware: The parts of a computer system that can be kicked. If a train station is where the train stops, what is a work station? I haven't lost my mind; it's backed up on tape somewhere. Is reading in the bathroom considered Multi-Tasking? It works! Now if only I could remember what I did... To err is human, but to really foul things up requires a computer. Will the information superhighway have any rest stops? Never interrupt your opponent while he is making a mistake. Life is like a roll of toilet paper . . . the closer you get to the end the faster it goes. It is much easier to be critical than to be correct. I have no problem keeping secrets. It's the people I tell... Some people grin and bear it . . . others smile and change it. The man who can smile when things go wrong has thought of someone else he can blame it on. Acquaintance: a person whom we know well enough to borrow from, but not well enough to lend to. Advice is what we ask for when we already know the answer but wish we didn't. I couldn't repair your brakes, so I made your horn louder. It's only funny until someone gets hurt. Then it's hilarious. Lead me not into temptation. I can find it myself. Never underestimate the power of stupid people in large groups. The downside of being better than everyone else is that people tend to assume you're pretentious. It is morally wrong to allow a sucker to keep his money. Money can't buy you happiness, but it does bring you a more pleasant form of misery. Many men would rather die than think. Many do. |
Each quote is on a seperate line so that when I open the file it is broken up into an array. I wanted to update the list of quotes without updating my code so I first count how many quotes I have. Knowing that I'm able to pick one at random. The image is of a set width and I don't want my text running off the edge of the image so I break it up into lines. I originally did this by just chopping it up into strings of a arbitrary length but this didn't look good as words were broken up. Instead I broke the quote up into a series of words so I am able to reconstruct it up to a set length and then start on the next line. This means that no words are broken up and it just looks better. Obviously as the length of quotes changes so does the number of lines and hence the height of the image. This is again dedided on-the-fly. One problem with this code is that the height is not decided based on the number of lines but on the length of the quote divided by the maximum number of characters I want. This means that occasionally you get an extra line with no text on it. This was a throw back to the earlier version of this code that I never changed. Bigger and better projects and all that. Okay, so we've picked the quote, decided how many lines we want and hence how high the image wil be and then broken the quote down into lines. Between all this though is a switch statement. This just randomly chooses a set of colours for the text and background. I did consider randomly selecting colours but on consideration of the possible combinations decided against it. You could do it so you always get complimentary colours but I decided not to go that far. After that it is a simple case of constructing the image. All this gives the following final product.  Obviously you'll need to refresh the page a few times to see it in its full glory. Any question and I'll be happy to answer them.
--------------------

|