花褪残红青杏小。燕子飞时,绿水人家绕。

PHP随机图片/头像

Web 十五楼的鸟儿 25834浏览 1评论

专为给喜欢逛论坛的你设计...

<?php


$path = "./img/". preg_replace('/[^a-zA-Z0-9]/i','',$_GET['id'] );
if (is_dir($path)){
    $url = $path  ;
}
else {
        $url =  "./img/nobird";
}
$files = array();
if(false !== ($handle = @opendir($url)))
{
  while($file = readdir($handle))
{
if(($file !== ".")&&($file !== ".."))
{
  if((substr($file,-3) == "gif") || (substr($file,-3) == "jpg") || (substr($file,-3) == "png"))
   $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");

elseif(substr($files[$random],-3) == "png")
   header("Content-type:image/png");
   readfile("$url/$files[$random]"); //读文件,显示图片;
}else
   {
       $im = imagecreatetruecolor(500, 100);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 500, 100, $bgc);
        imagettftext($im,15,0,5,20,$tc,$font,iconv("gb2312","UTF-8","图片载入出错,请向我报告!"));
        imagettftext($im,15,0,6,36,$tc,$font,iconv("gb2312","UTF-8",$imgname));
            }
?>

和网上流传甚多的版本比较起来,这个支持id,也就是说,一个文件,可以为多个账户服务(我就是一篇凑数的文章,你特么来咬我呀).

转载请注明:鸟儿博客 » PHP随机图片/头像

游客
发表我的评论 换个身份
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)