PHP Snippets
random_sig
Information
(PHP Version >= 4.3.0)Functions used in this snippet: array_rand, getimagesize, header, image_type_to_mime_type, readfile, is_numeric.
Description
mixed random_sig( bool image )
If the optional image arguement is left as true, random_sig will output a random image. If image is set to false it will output the html tags for an image, and link if a link is supplied.
(note you cannot have more than one image pointing to the same link)
Snippet
<?php
function random_sig($image=true){
$array = array( 'http://www.google.com' => 'google.gif',
'someimage.jpg',
'anotherimage.png');
$rand = array_rand($array);
$size = getimagesize($array[$rand]);
if($image){
header('Content-type: ' . image_type_to_mime_type($size[2]));
readfile($array[$rand]);
exit;
}else{
if(!is_numeric($rand)){
return '<a href="' . $rand . '"><img src="' . $array[$rand] . '" alt="" /></a>';
}else{
return '<img src="' . $array[$rand] . '" alt="" />';
}
}
return false;
}
?>
Example
random_sig Can be used in the following way:
<?php
echo random_sig(false); // outputs <img src="" /> etc
random_sig(); // outputs an actual image.
// the above can be used like <img src="your_file_name.php" />
?>
0 Responses to random_sig