Random Pic php script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ephekt
Forum Newbie
Posts: 2
Joined: Fri Nov 19, 2004 2:41 pm

Random Pic php script

Post by ephekt »

I've been usign the following php script for random sigs and avatars in vBulletin. It works fine for the defined image types, but I'd like to include small flash movies for avatars etc. The script seems to work fine outside of the forums, but doesn't parse the .swf in posts. Anyone have any idea's? Below is the script, notice that I've added the following line.

Code: Select all

$extList['swf'] = 'application/x-shockwave-flash';
Here's the full script.

Code: Select all

<?

// set image directory if '.' = image dir


        $folder = '.';




   $extList = array();
        $extList['gif'] = 'image/gif';
        $extList['jpg'] = 'image/jpeg';
        $extList['jpeg'] = 'image/jpeg';
        $extList['png'] = 'image/png';
        $extList['swf'] = 'application/x-shockwave-flash';   




// --------------------- END CONFIGURATION -----------------------

$img = null;

if (substr($folder,-1) != '/') {        $folder = $folder.'/'; }

if (isset($_GET['img'])) {
        $imageInfo = pathinfo($_GET['img']);
        if (isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )) {
                $img = $folder.$imageInfo['basename'];
        }
} else {
        $fileList = array();
        $handle = opendir($folder);
        while ( false !== ( $file = readdir($handle) ) ) {
                $file_info = pathinfo($file);
                if (
                    isset( $extList[ strtolower( $file_info['extension'] ) ] )
                ) {
                        $fileList[] = $file;
                }
        }
        closedir($handle);

        // If we have at least 1 image
        if (count($fileList) > 0) {
                $imageNumber = time() % count($fileList);
                $img = $folder.$fileList[$imageNumber];
        }
}

if ($img!=null) {
        $imageInfo = pathinfo($img);
        $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
        header ($contentType);
        readfile($img);
} else {
        if ( function_exists('imagecreate') ) {
                header ("Content-type: image/png");
                $im = @imagecreate (100, 100)
                    or die ("Cannot initialize new GD image stream");
                $background_color = imagecolorallocate ($im, 255, 255, 255);
                $text_color = imagecolorallocate ($im, 0,0,0);
                imagestring ($im, 2, 5, 5,  "You broke it!", $text_color);
                imagepng ($im);
                imagedestroy($im);
        }
}

?>
Any help would be greatly appreciated. Thanks in advance. :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

if the images are in a pubwww directory, instead of random.php outputtting something...... redirect to a random $image from the pubwww directory. this way you don't have to worry about content types ;)

<img src="random.php" />


random.php

Code: Select all

$avatars = array(...);
header("Location: http://host/{$avatars[rand(0, sizeof($avatars))]}");
ephekt
Forum Newbie
Posts: 2
Joined: Fri Nov 19, 2004 2:41 pm

Post by ephekt »

Thanks, that would work too, but it's not what I'm really looking for. All of the images to be used by the script are in /titles/username. I want to be able to use this one script for both image and possibly flash content. Having the script call randomly between both content types.
Post Reply