<?php
/*******************************************************************************
*       PHP AVATAR ROTATOR
*******************************************************************************/

$path = "/gallery/zakitano1/comics/Avatars/";     // Path to your avatar folder relative to the root directory of your site

/******************************************************************************
*       DO NOT EDIT BELOW THIS LINE!
******************************************************************************/

$dir = $_SERVER['DOCUMENT_ROOT'].$path;
$avatars = array();

// Open avatar directory and read its contents into an array
if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                        if (filetype($dir.$file) == "file" && getimagesize($dir.$file)) {
                                array_push($avatars, $file);
                        }
                }
                closedir($dh);
        }
}

// Create random avatar
$img = $dir.$avatars[rand(0, count($avatars)-1)];
$info = getimagesize($img);

if ($info[2] == 2) {
        header('Content-Type: image/jpeg');

        $img = imagecreatefromjpeg($img);
        imagejpeg($img);
}

elseif ($info[2] == 3) {
        header('Content-Type: image/png');

        $img = imagecreatefrompng($img);
        imagepng($img);
}

else {
        header('Content-Type: image/gif');

        $img = imagecreatefromgif($img);
        imagegif($img);
}

imagedestroy($img);
?>