曲径通幽论坛

标题: 随机获取图像 [打印本页]

作者: beyes    时间: 2010-9-20 00:09
标题: 随机获取图像
随机获取图像,代码:
<?php
     $url='images';   //目录

     $image='';

     $files=array();   //存储图像名数组

     if($handle=opendir("$url")){   //打开目录
         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);   //随机获取数组中的图片文件名
     $image="$url/$files[$random]";   //得到相对路径
     if(substr($image,-3) == 'gif') {
         $pic = imagecreatefromgif($image);  //读取外部GIF图片函数
         header("Content-type: image/gif");
         imagegif($pic);   //将图片传送到浏览器
         imagedestroy($pic);   //释放内存
     } elseif (substr($image, -3) == 'jpg') {
         $pic = imagecreatefromjpeg($image);  //读取外部JPEG图片函数
                 header("Content-type: image/jpeg");
                 imagejpeg($pic);
                 imagedestroy($pic);
     }
     
?>





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2