Случайна снимка

<?php


$folder = '.';


$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';


$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 (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}

if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
$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, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}

?>
 
сложих го ама дава някви ерори Warning: Cannot modify header information - headers already sent by (output started at C:\****\*****\index.php:13) in C:\*****\****\index.php on line 124
 
Намерих нещо по-кратко


<?php



$total = "11";



$file_type = ".jpg";


$image_folder = "images/random";


$start = "1";



$random = mt_rand($start, $total);



$image_name = $random . $file_type;



echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";



?>
 
<?
if (!$_GET["start"]) $_GET["start"] = 1;
$nf = ""; // Файлът
$show = 3;
$picsWidth = 120;
$showpl = 3;
$nf = "";
$dir = "galeriq";
$dir = array_diff(scandir("galeriq"),array("1", "2","3"));
$rand = array_rand($dir,1);
$thedir = opendir($dir);
while (false !== ($file = readdir($thedir))){
if (in_array($file, array(".", "..", "Thumbs.db"))) continue;
$files[] = $file;
}
$pages = ceil(count($files)/$show); $count = 1;
if($fileCount == $picsOnRow) $closeTr = "</tr>\n<tr>\n";
foreach ($files as $file){
$nr++;
if ($nr >= $_GET["start"] && $nr < $_GET["start"]+$show){
if ($count == $showpl) { $br = "<br />";}

echo $dir[$rand];
if ($count == $showpl) { $count = 0; $br = ""; }
$count++;
}
}


?>
Поправете ме
Warning: opendir() expects parameter 1 to be string, array given in
Warning: readdir(): supplied argument is not a valid Directory
Warning: Invalid argument supplied for foreach() in
 
Сметна ,че като смесиш кодовете ще работи както трябва?
Втори опит и от мене , ще се опитам да ти бъда максималко полезен ,и ако не работи - сподели какво не работи точно ,или ако не те удовлетворява резултата - просто кажи ,че не е това ,която търсиш :idea:
$directory = "ТВОЯТА-ПАПКА";
$dir = array_diff(scandir($directory),array(".", ".."));
$rand = array_rand($dir,1);
echo "<img src=\"".$directory."/".$dir[$rand]."\" />";
 
Replace каза:
Сметна ,че като смесиш кодовете ще работи както трябва?
Втори опит и от мене , ще се опитам да ти бъда максималко полезен ,и ако не работи - сподели какво не работи точно ,или ако не те удовлетворява резултата - просто кажи ,че не е това ,която търсиш :idea:
$directory = "ТВОЯТА-ПАПКА";
$dir = array_diff(scandir($directory),array(".", ".."));
$rand = array_rand($dir,1);
echo "<img src="".$directory."/".$dir[$rand]."" />";
хмм интересното е че тва го сложих в кода до който сега се мъча да направя и стана мерси +1 :?:
 

Back
Горе