Footer with Content Submissions

I’ve finally gotten around to adding the final (for now) feature to my footer. You can submit your own content :slight_smile:

Submit your own:
http://deadstats.nokrev.com/submission.php

View the footer (without visiting the kforums):
http://deadstats.nokrev.com/footer.php

Source code (view variables at the bottom to use it yourself):

<?php

class Footer {
	var $directory;
	var $myPhrases;
	var $yourPhrases;
	var $userSubmittedImage;
	var $font;
	var $origin;

	function getMyPhrases () {
		// Make sure $file exist
		if ( file_exists ( $this->directory."/".$this->myPhrases ) )  {
			return explode ( "
", file_get_contents($this->directory."/".$this->myPhrases) );
		} else return false; // Return false if file not found
	}
	
	function getYourPhrases () {
		// Make sure $file exist
		if ( file_exists ( $this->directory."/".$this->yourPhrases ) )  {
			return explode ( "
", file_get_contents($this->directory."/".$this->yourPhrases ) );
		} else return false; // Return false if file not found
	}

	function makeFooter () {
		$myPhrases = $this->getMyPhrases();
		$yourPhrases = $this->getYourPhrases();
		$font = "MyriadPro-Bold.otf";
		
		if ( rand(0,1) == 0 ) {
			$string = $myPhrases[rand ( 0, count ( $myPhrases ) - 1 )];
			$this->origin = 1;
		} else {
			$string = $yourPhrases[rand ( 0, count ( $yourPhrases ) -1 )];
			$this->origin = 2;
		}
		
		$backgrounds = array (
			array ( 77, 52, 55 ),
			array ( 63, 82, 95 ),
			array ( 211, 234, 187 )
		);
		
		$colors = array (
			array ( 242, 222, 224 ),
			array ( 199, 216, 227 ),
			array ( 79, 95, 63 )
		);
		
		$pallet = rand(1,count($backgrounds))-1;
		
		$bg = $backgrounds[$pallet];
		$cl = $colors[$pallet];
	
		// Create Image
		if ( $this->origin == 1 ) { // From scratch
			$im = @imagecreate(300, 60);
			$background_color = imagecolorallocate($im, $bg[0], $bg[1], $bg[2]);
			$text_color = imagecolorallocate($im, $cl[0], $cl[1], $cl[2]);
			imageantialias($im, 1);
		} else { // Based off "user submitted" image
			$im = imagecreatefrompng($this->userSubmittedImage);
			$cl = array ( 252, 239, 235 );
			$text_color = imagecolorallocate($im, $cl[0], $cl[1], $cl[2]);
			imageantialias($im, 1);
		}
		// Loop through font sizes and center
		if ( $this->origin == 1 ) {
			for ( $fontsize = 28; $fontsize > 5; $fontsize-- ) {
				$size = imagettfbbox ( $fontsize, 0, $font, $string );
				if ( $size[2] < 270 ) {
					imagettftext($im, $fontsize, 0, 15, 30-$size[5]/2, $text_color, $font, $string); break;
				}
			}
		} else {
			imagettftext($im, 18, 0, 5, 48, $text_color, $font, $string);
		}

		// Flush the output
		$img = imagepng($im);
		imagedestroy($im);
		
		return $img;
	}
	
	function grabFooter () {
		// Get Files
		$dh  = opendir($this->directory);
		while (false !== ($filename = readdir($dh))) {
			if ( strstr ( $filename, "txt" ) == false && $filename != $this->userSubmittedImage && $filename != $this->font ) {
				$files[] = $filename;
			}
		}
		
		sort($files);
		
		// Remove . and ..
		array_shift($files);
		array_shift($files);
		
		// Count and Ranhdomize
		$rand = rand(1, count($files))-1;
		
		return $files[$rand];
	}
	
	function pickFooter () {
		if ( rand ( 0,1 ) == 0 ) {
			header("Content-type: image/png");
			echo $this->makeFooter();
		} else {
			$file = $this->grabFooter();
			switch ( strstr($file,".") ) {
				case ".jpg":
				case ".jpeg":
					header("Content-type: image/jpeg");
					break;
				case ".gif":
					header("Content-type: image/gif");
					break;
				case ".png":
					header("Content-type: image/png");
					break;
			}
			include ( "../images/kirupa_footers/" . $file );
		}
		return true;
	}
}

$footer = new Footer();
$footer->directory = "[path to your images]";
$footer->myPhrases = "phrases.txt"; // in images directory
$footer->yourPhrases = "yourPhrases.txt"; // this too
$footer->userSubmittedImage = "usersubmitted.png"; // and this
$footer->font = "MyriadPro-Bold.otf"; // and this...

$footer->pickFooter();

?>

And the source for the submission page:

<?php
if ( isset ( $_POST['submit'] ) && isset ( $_POST['message'] ) ) {
	$f = fopen("[path to your file]/yourPhrases.txt",'a');
	$added = fwrite($f, "
".$_POST['message']);
	fclose($f);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>Submit KForum Footer Messages</title>
</head>
<body>
	<h1>Submit a KForum Footer Message</h1>
	<?php if ( $added ) { ?>
		<p><strong>Success</strong>. Your message has been added to the list.</p>
	<?php } ?>
	<p>These appear marked as "User Submitted Message", when randomly seen
	through the KForum footer.</p>
	
	<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="comment_form">
		<fieldset><legend>Forum Footer Messages</legend>
			<div>
				<label for="message">Message</label>
				<input tabindex="1" id="message" name="message" />
				
				<input type="submit" name="submit" tabindex="2" value="Submit" 
					/>
			</div>
		</fieldset>
	</form>
	<img src="footer.php" alt="KForum Footer Preview" />
</body>
</html>