Wenn man nicht immer das selbe Bild oder Banner haben will, kann man mit dieser Funktion ein zufälliges Bild aus einem Verzeichnis auslesen und anzeigen.
/** * Smarty plugin * @package Smarty * @subpackage plugins * * * Smarty random_pic function plugin * * Type: function * Name: random_pic * Purpose: loads a random pic from dir * * use: {random_pic dir='path/to/dir' alt='some description' width='300' height='200'} * the width and height can be omitted, it will be added automatically * but you can use this to overide those values * * @author Johannes 'Banana' Keßler <mail at bananas-playground dot net> * @param array * @param Smarty */ function smarty_function_random_pic($params, &$smarty) { { $type = false; } // check if we have an / at the end // if not add some if($dir_tmp != "/") { $params['dir'] .= "/"; } // read files from dir // and supress error messages { if($file[0] ==".") continue; { if($params['type']) { { $files[] = $file; } } else { $files[] = $file; } } } // if no files are found raise an error { $smarty->trigger_error("random_pic: either wrong 'dir', 'type' value or no files available"); return false; } // get the random pic $pic = $files[$rand]; // check if we have set the width or height { $params['width'] = $picInfo[0]; $params['height'] = $picInfo[1]; } // the return code $out = '<img src="'.$params['dir'].$pic.'" alt="'.$params['alt'].'" width="'.$params['width'].'" height="'.$params['height'].'" />'; return $out; }
