Rumah >pembangunan bahagian belakang >tutorial php >PHP随机显示目录下的图片_PHP
用JavaScript写的话,程序流程应该是:建立一个图片数组->随机选择数组里其中一个值->生成样式并写入body标签。
可是用JS做的话,有以下缺点:
1.万一浏览器禁用了JS的话就失效了,而且写代码是需要考虑兼容性。
2.维护比较麻烦,图片的位置都存放在数组里。
于是我提议用PHP处理,可是我和她对PHP都是半桶水的,一时之间也想不出怎么做。今天时运高,看到一个PHP随机显示目录下图片的源码,学习一下,并分享之。
先看看原理:从一个目录里获取某类型文件的清单(用在WEB的话一般是jpg/gif/png)->通过随机函数选一个图片->输出代码。
PHP代码如下:
1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br> |
<span style="COLOR: rgb(0,0,136)">$imglist</span><span style="COLOR: rgb(51,153,51)">=</span><span style="COLOR: rgb(0,0,255)">''</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <span style="COLOR: rgb(102,102,102); FONT-STYLE: italic">//用$img_folder变量保存图片所在目录,必须用“/”结尾</span><br /> <span style="COLOR: rgb(0,0,136)">$img_folder</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(0,0,255)">"images/tutorials/"</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /> <span style="COLOR: rgb(153,0,0)">mt_srand</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,153,0)">(</span>double<span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(153,0,0)">microtime</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">*</span><span style="COLOR: rgb(204,102,204)">1000</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /> <span style="COLOR: rgb(102,102,102); FONT-STYLE: italic">//使用目录类</span><br /> <span style="COLOR: rgb(0,0,136)">$imgs</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(153,0,0)">dir</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$img_folder</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /> <span style="COLOR: rgb(102,102,102); FONT-STYLE: italic">//检查目录下是否有图片,并生成一个清单</span><br /> <span style="COLOR: rgb(177,177,0)">while</span> <span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$file</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(0,0,136)">$imgs</span><span style="COLOR: rgb(51,153,51)">-></span><span style="COLOR: rgb(0,64,0)">read</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(0,153,0)">)</span> <span style="COLOR: rgb(0,153,0)">{</span><br /> <span style="COLOR: rgb(177,177,0)">if</span> <span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(153,0,0)">eregi</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,255)">"gif"</span><span style="COLOR: rgb(51,153,51)">,</span> <span style="COLOR: rgb(0,0,136)">$file</span><span style="COLOR: rgb(0,153,0)">)</span> <span style="COLOR: rgb(51,153,51)">||</span> <span style="COLOR: rgb(153,0,0)">eregi</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,255)">"jpg"</span><span style="COLOR: rgb(51,153,51)">,</span> <span style="COLOR: rgb(0,0,136)">$file</span><span style="COLOR: rgb(0,153,0)">)</span> <span style="COLOR: rgb(51,153,51)">||</span> <span style="COLOR: rgb(153,0,0)">eregi</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,255)">"png"</span><span style="COLOR: rgb(51,153,51)">,</span> <span style="COLOR: rgb(0,0,136)">$file</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(0,153,0)">)</span><br /> <span style="COLOR: rgb(0,0,136)">$imglist</span> <span style="COLOR: rgb(51,153,51)">.=</span> <span style="COLOR: rgb(0,0,255)">"<span style="FONT-WEIGHT: bold; COLOR: rgb(0,102,153)">$file</span> "</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /> <span style="COLOR: rgb(0,153,0)">}</span> <span style="COLOR: rgb(153,0,0)">closedir</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$imgs</span><span style="COLOR: rgb(51,153,51)">-></span><span style="COLOR: rgb(0,64,0)">handle</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /> <span style="COLOR: rgb(102,102,102); FONT-STYLE: italic">//把清单里的项都放到一个数组里</span><br /> <span style="COLOR: rgb(0,0,136)">$imglist</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(153,0,0)">explode</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,255)">" "</span><span style="COLOR: rgb(51,153,51)">,</span> <span style="COLOR: rgb(0,0,136)">$imglist</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <span style="COLOR: rgb(0,0,136)">$no</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(153,0,0)">sizeof</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(0,0,136)">$imglist</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">-</span><span style="COLOR: rgb(204,102,204)">2</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /> <span style="COLOR: rgb(102,102,102); FONT-STYLE: italic">//生成一个介于0和图片数量之间的随机数</span><br /> <span style="COLOR: rgb(0,0,136)">$random</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(153,0,0)">mt_rand</span><span style="COLOR: rgb(0,153,0)">(</span><span style="COLOR: rgb(204,102,204)">0</span><span style="COLOR: rgb(51,153,51)">,</span> <span style="COLOR: rgb(0,0,136)">$no</span><span style="COLOR: rgb(0,153,0)">)</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <span style="COLOR: rgb(0,0,136)">$image</span> <span style="COLOR: rgb(51,153,51)">=</span> <span style="COLOR: rgb(0,0,136)">$imglist</span><span style="COLOR: rgb(0,153,0)">[</span><span style="COLOR: rgb(0,0,136)">$random</span><span style="COLOR: rgb(0,153,0)">]</span><span style="COLOR: rgb(51,153,51)">;</span><br /> <br /><span style="COLOR: rgb(102,102,102); FONT-STYLE: italic">//输出结果</span><br /> <span style="COLOR: rgb(177,177,0)">echo</span> <span style="COLOR: rgb(0,0,255)">'<img src="'</span alt="PHP随机显示目录下的图片_PHP" ><span style="max-width:90%">.</span><span style="COLOR: rgb(0,0,136)">$img_folder</span><span style="COLOR: rgb(51,153,51)">.</span><span style="COLOR: rgb(0,0,136)">$image</span><span style="COLOR: rgb(51,153,51)">.</span><span style="COLOR: rgb(0,0,255)">'" border=0/>'</span><span style="COLOR: rgb(51,153,51)">;</span> |
如果要通过这个函数变换页面背景的话,可以把最后一句改为:
1<br>2<br> |
<span style="COLOR: rgb(177,177,0)">echo</span> <span style="COLOR: rgb(0,0,255)">'<body style="backgroud-image:url("'</span><span style="COLOR: rgb(51,153,51)">.</span><span style="COLOR: rgb(0,0,136)">$img_folder</span><span style="COLOR: rgb(51,153,51)">.</span><span style="COLOR: rgb(0,0,136)">$image</span><span style="COLOR: rgb(51,153,51)">.</span><span style="COLOR: rgb(0,0,255)">'">'</span><span style="COLOR: rgb(51,153,51)">;</span><br /><span style="COLOR: rgb(51,153,51)"></</span>body<span style="COLOR: rgb(51,153,51)">></span> |
并用整段程序替换标签。
如果需要多次调用此程序的话,可以写成一个函数,各位按需要改写。
用PHP的方法来输出随机图片的好处是:
1.维护简单,只需要控制目录里图片的数量。
2.可以自定义文件类型,只要你有需要,改成随机输出一个Flash也行的
3.可以自定义输出结果,换句话说,用在什么地方都行了
4.改写成函数后功能更强大