Summary of random display image methods in php

Source: Internet
Author: User

Summary of random display image methods in php

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

<Html>

<Body>

<? Php

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: ";

?>

</Body>

</Html>

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

<? Php

/*************************************** *******

* 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.