首先先新建一个php文件,然后在以下2种代码任选一种复制进去,地址可以自行更改。
第1种:使用自己的主机(服务器)上的图片
<?php readfile( "./img/" . rand(1,5) . ".jpg" ); ?>
新建一个目录,文件名叫img.把图片以1,2,3,4,5的命名规则放入目录中,就行了~
第2种:php通过rand()函数产生随机数,这个函数可以产生一个指定范围的数字
这段代码通过产生的随机数,随机选择图片
<html> <body> <?php 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 : <img src=$image_file />"; ?> </body> </html>
第3种:
<? $handle = opendir('./'); //当前目录 while (false !== ($file = readdir($handle))) { //遍历该php教程文件所在目录 list($filesname,$kzm)=explode(".",$file);//获取扩展名 if ($kzm=="gif" or $kzm=="jpg") { //文件过滤 if (!is_dir('./'.$file)) { //文件夹过滤 $array[]=$file;//把符合条件的文件名存入数组 } } } $suiji=array_rand($array); //使用array_rand函数从数组中随机抽出一个单元 ?> <img src="<?=$array[$suiji]?>">
第4种:
<?php /********************************************** * Filename : img.php * Author : freemouse * Usage: * <img src=img.php> * <img src=img.php?folder=images2/> ***********************************************/ if($_GET['folder']){ $folder=$_GET['folder']; }else{ $folder='/images/'; } //存放图片文件的位置 $path = $_SERVER['DOCUMENT_ROOT']."/".$folder; $files=array(); if ($handle=opendir("$path")) { 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); if(substr($files[$random],-3)=='gif') header("Content-type: image/gif"); elseif(substr($files[$random],-3)=='jpg') header("Content-type: image/jpeg"); readfile("$path/$files[$random]"); ?>
第5种:调用其他网站(网络相册)上的图片
<?php$id=rand(1,5);$image[1]='http://farm.static.flickr.com/1.png';$image[2]='http://farm.static.flickr.com/2.png';$image[3]='http://farm.static.flickr.com/3.png';$image[4]='http://farm.static.flickr.com/4.png';$image[5]='http://farm.static.flickr.com/5.png';header("location:$image[$id]");?>
最后就可以调用地址应用到网站上了: http://你的网址/randomimage.php
你也可以加上后缀对付论坛的格式过滤,例如: http://你的网址/randomimage.php?.jpg
第6种:直接用其他网站url图片+API
http://lorempixel.com/1600/900
https://unsplash.it/1600/900?random
(国内加载略慢)
https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture
【返回必应图片】
https://uploadbeta.com/api/pictures/random/?key=%E6%8E%A8%E5%A5%B3%E9%83%8E
【随机美女图片】
http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1
必应返回JSON数据,具体百度
7自己做
分享三个在线壁纸调用接口:
tags http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome
new http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByOrder&order=create_time&start=【0开始】&count=【加载数】&from=360chrome
专区 http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByCategory&cid=【分类ID】&start=【0开始】&count=【加载数】&from=360chrome
演示:https://www.68xi.cn/search/
<?php/* 功能:自动调取壁纸作为网站背景 开发时间:2019年7月4日 应用案例:https://www.68xi.cn/search/ 分享三个在线壁纸调用接口 tags http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome new http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByOrder&order=create_time&start=【0开始】&count=【加载数】&from=360chrome 专区 http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByCategory&cid=【分类ID】&start=【0开始】&count=【加载数】&from=360chrome */ @header('Content-Type: application/json; charset=UTF-8'); $number=-1; $url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=6&n=1';//必应壁纸调取接口 $curl = curl_init(); $httpheader[] = "Accept:*/*"; $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8"; $httpheader[] = "Connection:close"; curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $bing_data = curl_exec($curl); curl_close($curl); $bing_arr=json_decode($bing_data,true); if (!empty($bing_arr['images'][0]['url'])) { $data['code']=1; $data['url']='https://cn.bing.com'.$bing_arr['images'][0]['url']; if(checkmobile()==true)$data['url']=str_replace('1920x1080','768x1366',$data['url']); }else{ $data['code']=-1; } exit(json_encode($data)); function strexists($string, $find) { return !(strpos($string, $find) === FALSE); } function dstrpos($string, $arr) { if(empty($string)) return false; foreach((array)$arr as $v) { if(strpos($string, $v) !== false) { return true; } } return false; } function checkmobile() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); $ualist = array('android', 'midp', 'nokia', 'mobile', 'iphone', 'ipod', 'blackberry', 'windows phone'); if((dstrpos($useragent, $ualist) || strexists($_SERVER['HTTP_ACCEPT'], "VND.WAP") || strexists($_SERVER['HTTP_VIA'],"wap"))){ return true; }else{ return false; } }
速度: ★★★★★
功能: 返回Bing的随机图片
地址: https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture
速度: ★★★★★
功能: 返回随机图片, 需指定分辨率, 如果指定的分辨率没有找到图片则返回空页面
地址: http://lorempixel.com/1600/900
速度: ★★★★☆
功能: 来自github的项目https://github.com/xCss/bing
地址: https://bing.ioliu.cn/v1/rand (返回随机图片)
https://bing.ioliu.cn/v1/rand?w=1920&h=1200 (指定大小)
/**
* 已知分辨率
*/
'1920x1200',
'1920x1080',
'1366x768',
'1280x768',
'1024x768',
'800x600',
'800x480',
'768x1280',
'720x1280',
'640x480',
'480x800',
'400x240',
'320x240',
'240x320'
速度: ★★☆☆☆
功能: 返回指定分辨率的随机图片, (加载最慢, 可能是因为服务器在国外)
地址: https://unsplash.it/1600/900?random
速度: ★★★★★
功能: 返回随机的二次元图片, 国人之作, 发现地址: https://www.dujin.org/12142.html/amp
地址: https://api.xjdog.cn/Get-Image
速度: ★★★★★★
功能: 返回随机Bing图片, 其中数字为分辨率, 例如 1366x768 -> 1366, 1920x1080 -> 1920
地址: https://api.dujin.org/bing/1366.php
/** * 调用百度壁纸接口随机生成图片 * @author Reaper <369994633@qq.com> * @param string $Classify 分类 * @param string $Subclass 子类 * @param string $Rand 最大随机页 * @return string */function BaiduRandImg($Classify,$Subclass,$Rand){ $rand = rand(0,$Rand); $Classify = urlencode($Classify); $Subclass = urlencode($Subclass); $BaiduApiUrl = "http://image.baidu.com/data/imgs?pn=0&rn=36&col=%E5%A3%81%E7%BA%B8&tag=".$Classify."&tag3=".$Subclass."&width=1920&height=1080&ic=".$Rand."&ie=utf8&oe=utf-8&image_id=&fr=channel&p=channel&from=1&app=img.browse.channel.wallpaper&t=0.1993955611514664"; $ImgB = file_get_contents($BaiduApiUrl); $JsonGo = json_decode($ImgB,1); $Num = count($JsonGo['imgs']); $Arrnum = rand(0,$Num-2); return $JsonGo['imgs'][$Arrnum]['imageUrl']; }//使用跳板来破解防盗链,然后拼接跳转$GangWay = "https://www.myapi.wang/api/imgapi.php?imgurl="; $GangWay .= BaiduRandImg("风景","自然风光","10"); header("Location:".$GangWay);
<?php /** * 通过搜狗搜索壁纸接口随机图片 * @author Reaper <369994633@qq.com> * @param string $ImgClass 图片分类 | $ImgOrder 图片 */$GetImgUrl = file_get_contents("https://www.sogou.com/home/data/skinlist?code=".md5('Reaper'.rand(998,19998))); $GetImgRen = json_decode($GetImgUrl,1); $ImgClass = $GetImgRen['skinlib'][rand(0,count($GetImgRen['skinlib'])-1)]['skins']; $ImgOrder = rand(0,count($ImgClass)-1); header("location:".$ImgClass[$ImgOrder]['bigurl']);
My partner and I absolutely love your blog and find many of your post's to be exactly I'm looking for. Would you offer guest writers to write content available for you? I wouldn't mind writing a post or elaborating on a lot of the subjects you write about here. Again, awesome site!
Excellent beat ! I would like to apprentice while you amend your website, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept
Simply want to say your article is as astounding. The clarity in your post is just cool and i can assume you're an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please keep up the enjoyable work.
Hi there, You've done a fantastic job. I will certainly digg it and personally suggest to my friends. I am confident they'll be benefited from this web site.
You've made some good points there. I looked on the web to find out more about the issue and found most people will go along with your views on this site.
Hi, I do think this is an excellent web site. I stumbledupon it ;) I am going to revisit once again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to help others.
เดิมพันบาคาร่ามันที่สล็อต666เล่นคาสิโน 777เดิมพันออนไลน์ทดลอง เล่น สล็อต pgสล็อต เครดิตฟรจัดคาสิโนออนไลน์777แทงpgสล็อตวัดใจสล็อต ออนไลน์จัดสล็อต เว็บตรง แตก ง่ายวัดใจสล็อต เว็บ ตรง แตก ง่ายวัดใจสล็อต pg เว็บ ตรงเดิมพันу9สล็อตเทรดซุปเปอร์ สล็อต 666เดิมพันบาคาร่า ออนไลน์ ufa99เดิมพันสล็อตxo168จัดสล็อต ออนไลน์ sabai55แทงสล็อต 68วัดดวงสล็อต ฝาก20รับ100 เว็บตรงวัดใจสล็อต เว็บแท้วัดโคตรเทพnoname auto สล็อตเดิมพันออนไลน์โหลด สล็อต666วัดmgw สล็อต
Simply wish to say your article is as surprising. The clarity in your post is just cool and i can assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the gratifying work.
There's certainly a lot to know about this topic. I love all of the points you made.
Great blog you've got here.. It's difficult to find quality writing like yours these days. I really appreciate individuals like you! Take care!!