Scripts

Walk Of Fame

Hoi iedereen. Ik ben nieuw op PHPhulp, ik dacht laat ik bij mijn rentree eens een scriptje plaatsen. Ik had namelijk een leuk idee in gedachten, maar wist niet precies hoe ik dit moest aanpakken. Ik kwam viavia in connectie met Kalle, die mij hielp met de plaatjes en css. Via hem kwam ik dan ook bij PHPerik, mijn reddende engel. Als beginnend php-er kan ik natuurlijk niet direct zo goed PHP-en, laat staan OOP PHP-en, dus vroeg ik aan PHPerik of hij mij hiermee kon helpen. Hij zette mijn PP(Procedure Programming) script in een mum van tijd om in een OOP script. Dus hierbij mijn script. Helaas ondersteunt mijn host geen PHP5 (en GD, zover als ik weet) vandaar dat ik het niet online kan zetten, maar PHPerik was bereid om het online te zetten. Ik heb een .rar gemaakt. Daarin staat alles wat je nodig moet hebben. http://www.badongo.com/file/1515791 Als laatste nogmaals mijn dank aan het adres van Kalle en PHPerik. Ook dat phperik het online wilde zetten.

walk-of-fame
[code]
<?php
error_reporting(E_ALL);
ini_set('display-errors', 1);

class KEK 
{
	public static	$size				= 40;							// grootte van font
	public static	$font				= 'fonts/verdana.ttf';			// font
	public static	$saveFile			= 'kek.jpg';					// bestand om in op te slaan
	public static	$originalFile		= 'images/phphulp_gd.jpg';		// originele afbeelding
	public static	$y					= 365;							// verticale positie van tekst
	
	public			$walk_of_fame		= null;							// image resource
	public			$name				= null;							// naam die op afbeelding wordt gezet
	public			$color				= null;							// kleur van font
	public 			$location			= null;							// locatie van nieuw bestand
	
	public function __construct() {
		if (!$this->walk_of_fame = $this->makeResource()) {
			throw new Exception('Fout tijdens het maken van een resource');
		}
		
		if (!$this->color = $this->getColor()) {
			throw new Exception('Fout tijdens het bepalen van de kleur');
		}	
	}	
	
	private function makeResource() {
		$resource = imagecreatefromjpeg(self::$originalFile);
		
		return $resource;
	}
	
	public function setColor($color) {
		$this->color = $color;
	}
	
	private function processName($name) {
		if (strlen($name) > 15) {
			$name = substr($name, 0, 15);
		}
		
		return strtoupper($name);
	}
	
	private function getColor() {
		return imagecolorallocate($this->walk_of_fame, 251, 238, 210);
	}

	private function getX() {
		$text_info = imageTTFBbox(self::$size, 0, self::$font, $this->name);
		$mid_text = abs(($text_info[2] - $text_info[0]) / 2);
		$mid_image = imagesx($this->walk_of_fame) / 2;
		
		$x = $mid_image - $mid_text;
		
		return $x;
	}
	
	private function getY() {
		return self::$y;
	}
	
	public function kekIt($name) {
		$this->name = $this->processName($name);
				
		if (!imagettftext($this->walk_of_fame, self::$size, 0, $this->getX(), $this->getY(), $this->color, self::$font, $this->name)) {
			throw new Exception('Fout tijdens het plaatsen van tekst op de afbeelding');
		}
		
		if (!imagejpeg($this->walk_of_fame, self::$saveFile)) {
			throw new Exception('Fout tijdens het aanmaken van de nieuwe afbeelding');
		}
		
		@imagedestroy($this->walk_of_fame);
		
		return self::$saveFile;
	}
}

if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['naam'])) {
	$naam = $_POST['naam'];
}

else {
	$naam = "kek";
}

try {
	$kek 			= new KEK();
	$walk_of_fame 	= $kek->kekIt($naam);
}

catch (Exception $e) {
	die('<h1>' . $e->getMessage() . '</h1>');	
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Walk Of Fame</title>
    <link rel="stylesheet" type="text/css" media="screen" href="css.css" />

</head>

<body>
<div id="container">
	<div id="content">
	    <div id="top">
			<p>Kom ook op de <em>Hollywood Walk Of Fame</em><p> 
			<p>Vul je naam, zodat je ook een gepast plaastje krijgt tussen de <em>PHPhulp</em>ers</p>
		</div>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" class="formulier" method="post">

			<fieldset>
				<legend> <em>Walk Of Fame</em> </legend>
				<ol>
					<li><label for="naam">Naam:</label> <input type="text" id="naam" name="naam" value=""/></li>
				</ol>
				<div id="line">&nbsp;</div>
				<ol>
					<input type="submit" id="submit" name="submit" value="Versturen" />
				</ol>
			</fieldset>
		</form>		
		<img src="<?php echo $walk_of_fame; ?>" alt="<?php echo $naam; ?>" /> 
	</div>
</div>
</body>
</html>
[/code]

De CSS die op de website staat.

[code]
/* CSS Document */
html, body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
    padding: 0;
	margin: 0;
	height: 100%;
	text-align:center;
	}

#container {
	background: #e2f7ba url(images/bg_wof.gif) center top repeat-y;
	width: 850px;
	text-align:left;
	margin: 0 auto;
	padding: 0;
	}

#content {
	margin: 0px 6px;
	}

#center {
    margin: 0px auto;
	width:400px;
	}	
	
#top {
	margin: 0;
	padding: 5px;
	font-size: 14px;
	text-align:center;
	}	
	
img {
	margin: 10px 0 0 1px;
    //margin-top: -10px; 
	height: 715px;
	}	

/*formulier*/	
form.formulier {
	width: 100%;
	font-size: 13px;
	color: #333;
	padding: 0; 
	}	
		
form.formulier legend {
	padding: 0 2px 0 0;
	color: #333;
	font-weight: bold;
	}

form.formulier fieldset label {
	width: 200px;
	display: inline-block;
	line-height: 1.8;
	vertical-align: top;
	color: #333;
	}

form.formulier fieldset {
	border: none;
	border-top: 1px solid #C9DCA6;
	border-bottom: 1px solid #C9DCA6;
	margin-top: 10px; 
	margin: 3px 0 0;
	background: none;
	}
		
form.formulier fieldset ol {
	margin: 0;
	padding: 0;
	display: inline-block;
	}

form.formulier div#line {	
	margin: 0;
	padding: 0;
	width: 50%;
	background: url(images/line_div.gif) left top repeat-x;	
	}
	
form.formulier fieldset li {
	padding: 5px 10px 7px;
	list-style: none;
	padding: 5px;
	margin: 0;
	}
		
form.formulier fieldset legend {
	padding: 0 0 5px;
	font-weight: normal;
	}
	
form.formulier fieldset label {
	width: auto;
	}

form.formulier input#naam {
	background: #C9DCA6;
	}
	
form.formulier input#submit {
	margin-top: -10px;
	margin-bottom: -5px;
	//margin-bottom: 5px;
	//margin-left: 6px;
	}	
	
div#container em {
	font-weight: bold;
	font-style: normal;
	}
[/code]

Reacties

0
Nog geen reacties.