PHP:随机变化的.jpg图片
实现的原理很简单,就是apache的url_rewrite功能+php程序
url_rewrite代码
[test@test htdocs]$ cat .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^images/logo\.jpg$ /test/randomimg.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^images/logo\.jpg$ /test/randomimg.php [L]
</IfModule>
php代码
- <?php
- header("Content-Type: image/jpeg");
- $dir = './images/';
- $imgarr=array();
- if ($handle = opendir($dir)) {
- while (false !== ($file = readdir($handle))) {
- if (preg_match ("/(.*?)\.jpg$/i", $file) ) {
- array_push($imgarr,$dir.$file);
- }
- }
- closedir($handle);
- }
- //srand ((float) microtime() * 10000000);
- readfile($imgarr[array_rand ($imgarr)]);
- ?>
只要把把要显示的图片放到$dir目录里就可以了:)
没有评论▼