0

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/

2 Answers 2

1

include 'background.png'; pastes the PNG image in the PHP code and tries to parse it as PHP. That is not possible, of course. You probably need something like fpassthru:

$fp = fopen('background.php', 'rb');
fpassthru($fp);
fclose($fp);
Sign up to request clarification or add additional context in comments.

3 Comments

Yea facepalm I didn't really pay attention to that. I guess I assumed it would just be crammed in there as if it had no php tags. I mean in a way it does that, but I couldn't possibly tell what kind of potential code the image could contain xD
Yeup that seems to have fixed it. Thanks a bunch man, I'm such a derp, it would've taken me forever to realise that!
Congratulations, You've just gained a few experience points :-)
1

Try changing the include 'background.png' to:

base64_encode(file_get_contents("background.png"));

and echo it out to a <img src=...

Like:

$imageData = base64_encode(file_get_contents("background.png"));

$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

echo '<img src="',$src,'">';

Or maybe something along the lines of:

$file = 'background.png';

if (file_exists($file)) {
    header('Content-Type: image/png');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

Or simply:

$file = 'background.png';
$type = 'image/png';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);

The errors are happening because of special characters from the image data being read as PHP code.

2 Comments

Oh yea that's totally a valid thing these days... Let me see if I can work that out .-.
Wow, thanks for the barrage of options, sincerely, but I guess just using the header 'image/png' and echoing the images contents works fine. Oh yea but thanks for the base64 encode thing, I almost forgot about that, it's fun :3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.