NamePros.Com (http://www.namepros.com/)
-   CODE (http://www.namepros.com/code/)
-   -   Show random image (http://www.namepros.com/code/435390-show-random-image.html)

Daniel 02-21-2008 11:00 AM

Show random image
 
Someone asked for a script that would show a random image, only took a few minutes to write, so I thought I might as well share it here.

PHP Code:
<?php

/**
* Danltn | http://danltn.com/
* No warranty is given to code used.
* I'm scared of lawsuits.
*/

$images = array();

$images[] = 'http://www.escapistmagazine.com/global/media/images/articles/folder/120/zeropunctuation_logo.png';
$images[] = 'http://www.google.com/images/experimental.gif';
$images[] = 'http://www.w3.org/2001/tag/2007/05/TagThursDiagram1.JPG';

// Add as many as you like here.

$image = $images[rand(0, count($images) - 1)];
// Get random image.

switch (strtolower(substr($image, -4, 4)))
{
    case
"jpeg":
    case
".jpg":
        
$ctype = "image/jpg";
        break;
    case
".gif":
        
$ctype = "image/gif";
        break;
    case
".png":
        
$ctype = "image/png";
        break;
}
// Find the content type, based on the last 4 characters, I could add GDLib here, but I haven't, this was a quick script.

$file = @file_get_contents($image);
// Get the file

if ($file)
{
    
// Only if the file has successfully been 'grabbed'.
    
$len = strlen($file);
    
// Get the length to be kind to the browser.
    
if (!headers_sent())
    {
        
header("Pragma: public");
        
header("Expires: 0");
        
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
header("Cache-Control: public");
        
// Set headers.
        
header("Content-Type: $ctype");
        
// Set content type as correct image.
        
header("Content-Length: " . $len);
        
// Be kind to the browser by saying the length.

        
echo $file;
        
// If it all checks out, send the image.
    
}
}

?>


All times are GMT -7. The time now is 12:08 AM.
Site Sponsors
Advertise your business at NamePros

Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0