Posts Tagged ‘image’

Random Image on Refresh (Using Numbers)

No Comments »

The image sizes are predetermined, but the order is not (hence random). Make sure your images are numbered (1 to infinity), not by name (a-z).

First Form:

[code]function randSidebar() {
$file = rand(x, y);//x is the min #, y is the max
$file .= '.ext';// change .ext to any extension, ie: .jpg, .gif
echo '<img src="' . $file . '">';
}
randSidebar(); //Call the function
[/code]

Using the original function, you can pass multiple sizes all over the page with this slight edit:

[code]function randSidebar($x,$y) {
$file = rand($x,$y);//x is the min #, y is the max
$file .= '.ext';// change .ext to any extension, ie: .jpg, .gif
echo '<img src="' . $file . '">';}
randSidebar(x-value,y-value); //Call the function, change x-value and y-value to whatever you want, ie: randSidebar(100,200)
[/code]


List Images in Directory

No Comments »

This can be modified to scan specific, non-image files (such as .html, .php, whatever, which is the beauty of using regular expressions.) In which case, you will have to change how the results echo.

[code]function listDirImages($dir) { //$dir is the name of the directory you want to list the images.
$files = scandir($dir); //scans the directory's files
$preg = "/.(jpg|gif)/i"; //match the following files, can be changed to limit or extend range, ie: png,jpeg,etc.
foreach($files as $img) { //loop through directory files
if(substr($img, 0, 1) != '.') { //ignore anything starting with a period
if(preg_match($preg, $img)) {
echo "<img src='" . preg_replace($preg, "", $img) . "'><br />";
}
}
}
}
listDirImages(nameofdirectory); //call function
[/code]


deleo_felisha@mailxu.com stickel.byron@mailxu.com leib_kathi@mailxu.com angrisano-judith@mailxu.com