Uhm, I'd like to think of myself as a pretty experienced php coder but I actually can not for the life of me figure out what is wrong with my code. So I wrote this little script to create a nice, colourful background, maybe throw in an easter egg every now and then. Nothing too serious, just having a little fun. Either way it works fine, it just throws pretty random errors.
Code looks like this: http://pastebin.com/5ndL1E7J
<?
header("Content-Type: image/png");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$bgtime = file_get_contents('bgtime');
if( $bgtime <= time() ) {
file_put_contents( 'bgtime', time() + 5 );
$im = imagecreate( 1920, 955 );
imagecolorallocate( $im, 255, 255, 255 );
$red = imagecolorallocatealpha( $im, 136, 34, 34, 235);
$green = imagecolorallocatealpha( $im, 34, 136, 34, 235);
$blue = imagecolorallocatealpha( $im, 34, 34, 136, 235);
for( $x=0; $x<88; $x++ ) {
for( $y=0; $y<44; $y++ ) {
if( mt_rand( 0, 1 ) ) {
$col = mt_rand( 0, 100 );
if( $col < 90 ) {
imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $green );
} else if( $col < 98 ) {
imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $blue );
} else {
imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $red );
}
}
}
}
imagepng( $im, 'background.png' );
imagepng( $im );
imagedestroy( $im );
} else if( mt_rand( 0, 10 ) == 1 ) {
$texts = [ "#<<_ Over#######>>> 9000", "#<<_ Game#######>>>_ Over" ];
include '../fonts/pokebet.charray';
$im = imagecreatefrompng( 'background.png' );
$text = str_split( $texts[array_rand($texts)] );
$red = imagecolorallocatealpha( $im, 136, 34, 34, 235);
$offx = 0;
$offy = 0;
foreach( $text as $char ) {
if( $char == '#' ) {
$offx += 9;
$offy = 0;
} else if( $char == ">" ){
$offx += 1;
} else if( $char == "<" ) {
$offx -= 1;
} else if( $char == "_") {
$offy += 5;
} else if( $char == " ") {
$offy += 1;
} else {
$offy -= $charray[$char][2];
foreach( $charray[$char][0] as $pixel ) {
$x = $pixel[0] + $offx;
$y = $pixel[1] + $offy;
imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $red );
}
$offy += $charray[$char][1];
}
}
imagepng( $im );
imagepng( $im );
} else {
include 'background.png';
}
?>
Error looks like this, claimed lines and t_strings differ greatly with every error:
PHP Parse error: syntax error, unexpected '\xdeu\xdc\x81\xcb\xd7\xb59jb\xe5\xae\xfa' (T_STRING) in /var/www/inf1/images/background.png on line 31, referer: http://inf1.****.nl/