PHP随机显示图片的代码(刷新变换图片)教程Php

印迹发布于:2019-2-24 2151

首先先新建一个php文件,然后在以下2种代码任选一种复制进去,地址可以自行更改。

方法一:

使用自己的主机(服务器)上的图片:

<?php readfile( "./img/" . rand(1,5) . ".jpg" ); ?>

新建一个目录,文件名叫img.把图片以1,2,3,4,5的命名规则放入目录中,就行了~

方法二:

调用其他网站(网络相册)上的图片:

<?php
$id=rand(1,5);
$image[1]='http://virplus.com/random/pic/1.png';
$image[2]='http://virplus.com/random/pic/2.png';
$image[3]='http://virplus.com/random/pic/3.png';
$image[4]='http://virplus.com/random/pic/4.png';
$image[5]='http://virplus.com/random/pic/5.png';
header("location:$image[$id]");
?>

最后就可以调用地址应用到网站上了: http://virplus.com/randomimage.php

你也可以加上后缀对付论坛的格式过滤,例如: http://virplus.com/randomimage.php?.jpg

方法三:

php通过rand()函数产生随机数,这个函数可以产生一个指定范围的数字 

这段代码通过产生的随机数,随机选择图片

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/jaguar.jpg";
break;
case 4: $image_file = "/home/images/porsche.jpg";
break;
}
echo "Random Image : ".$image_file;
?>
方法四:
<?php
    $img_array = glob('images/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);
    if(count($img_array) == 0) die('没找到图片文件。请先上传一些图片到 '.dirname(__FILE__).'/images/ 文件夹');
    header('Content-Type: image/png');
    echo(file_get_contents($img_array[array_rand($img_array)]));
?>

以上的代码会查找 images 目录下的所有图片,并随机挑选出一张显示出来。

相关导航:

1. PHP随机显示图片的代码(刷新变换图片) 

2. php自动读取文件夹下所有图片    

3. php遍历、读取文件夹中图片并分页显示图片的方法

4. 利用PHP随机显示指定文件夹下某一图片的方法

5. 随机显示文件夹下图片文件的方法


http://www.virplus.com/thread-132.htm
转载请注明:2019-2-24 于 VirPlus 发表

推荐阅读
最新回复 (0)

    ( 登录 ) 后,可以发表评论!

    返回