Skip to content

Instantly share code, notes, and snippets.

@radarin
Created January 26, 2019 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radarin/999c5535110136dcb98ab23d59c4122e to your computer and use it in GitHub Desktop.
Save radarin/999c5535110136dcb98ab23d59c4122e to your computer and use it in GitHub Desktop.
Zufallsbild
<?php
// Allgemeine PHP-Variante
add_shortcode( 'randompic', 'rad_randompic' );
$vn = "./static-img/randompic";
$html_vn = "static-img/randompic/";
$barray = array(1,2,3,4,5);
$verzeichnis = opendir($vn);
while($file = readdir($verzeichnis)) {
$s = @getimagesize($vn."/".$file);
if(in_array($s[2], $barray))
$auswahl[] = $file;
}
mt_srand((double)microtime()*1000000);
$number = mt_rand(0,count($auswahl)-1);
$ausgabe = "<img src='".$html_vn.$auswahl[$number]."'>";
return $ausgabe;
?>
<?php
// Variante mit Funktion und Shortcode für Wordpress
add_shortcode( 'randompic', 'rad_randompic' );
function rad_randompic() {
$vn = "./static-img/randompic";
$html_vn = "static-img/randompic/";
$barray = array(1,2,3,4,5);
$verzeichnis = opendir($vn);
while($file = readdir($verzeichnis)) {
$s = @getimagesize($vn."/".$file);
if(in_array($s[2], $barray))
$auswahl[] = $file;
}
mt_srand((double)microtime()*1000000);
$number = mt_rand(0,count($auswahl)-1);
$ausgabe = "<img src='".$html_vn.$auswahl[$number]."'>";
return $ausgabe;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment