Php implements a summary of random display image methods. Php implementation of random display image method summary this article shares a function of php implementation of random display of images, you can randomly display the images stored in the specified folder. A friend who is interested in php can achieve a summary of random display image methods
This article shares a php function for randomly displaying images. you can randomly display images stored in a specified folder. If you are interested, study it.
Php uses the rand () function to generate a random number. This function can generate a number within a specified range.
This code randomly selects an image through the random number generated
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
Srand (microtime () * 1000000 );
$ Num = rand (1, 4 );
Switch ($ num)
{
Case 1: $ image_file = "/home/images/alfa.jpg ";
Break;
Case 2: $ image_file = "/home/images/ferrari.jpg ";
Break;
Case 3: $ image_file = "/home/images/jaruar.jpg ";
Break;
Case 4: $ image_file = "/home/images/porsche.jpg ";
Break;
}
Echo "Random Image :";
?>
|
Method 2:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
$ Handle = opendir ('./'); // Current Directory
While (false! ==( $ File = readdir ($ handle) {// traverse the directory where the php Tutorial file is located
List ($ filesname, $ kzm) = explode (".", $ file); // get the extension
If ($ kzm = "gif" or $ kzm = "jpg") {// file filtering
If (! Is_dir ('./'. $ file) {// folder filtering
$ Array [] = $ file; // Save the qualified file name to an array
}
}
}
$ Suiji = array_rand ($ array); // use the array_rand function to randomly extract a unit from the array.
?>
">
|
Method 3:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
/*************************************** *******
* Filename: img. php
* Author: freemouse
* Usage:
*
*
**************************************** *******/
If ($ _ GET ['Folder']) {
$ Folder = $ _ GET ['Folder'];
} Else {
$ Folder = '/images /';
}
// The location where the image file is stored
$ Path = $ _ SERVER ['document _ root']. "/". $ folder;
$ Files = array ();
If ($ handle = opendir ("$ path ")){
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = "." & $ File! = ".."){
If (substr ($ file,-3) = 'GIF' | substr ($ file,-3) = 'jpg ') $ files [count ($ files)] = $ file;
}
}
}
Closedir ($ handle );
$ Random = rand (0, count ($ files)-1 );
If (substr ($ files [$ random],-3) = 'GIF') header ("Content-type: image/gif ");
Elseif (substr ($ files [$ random],-3) = 'jpg ') header ("Content-type: image/jpeg ");
Readfile ("$ path/$ files [$ random]");
?>
|
The above is all the content of this article. I hope you will like it.
In this article, we will share a function for randomly displaying images implemented by php. you can randomly display images stored in a specified folder. Interested friends...