php动漫随机图源码,随机图片API源码
下列代碼保存任意文件名的 php 文件
header('Cache-Control:no-cache,must-revalidate');
header('Pragma:no-cache');
header("Expires:0");
header("Access-Control-Allow-Origin:*");
//處理請求輸出數(shù)據(jù)
//這將得到一個文件夾中的所有 gif,jpg 和 png 圖片的數(shù)組
$rand=rand(0,1);
if($rand){
$localurl="images/*/*.{gif,jpg,png}";
}else{
$localurl="images/*/*/*.{gif,jpg,png}";
}
$img_array=glob($localurl,GLOB_BRACE);
//從數(shù)組中選擇一個隨機(jī)圖片
$img=array_rand($img_array);
$imgurl=$img_array[$img];
$https=isset($_GET["https"])?$_GET["https"]:1;
if($https == "true"){
$imgurl='https://'.$_SERVER['SERVER_NAME'].'/'.$imgurl;
}else{
$imgurl='http://'.$_SERVER['SERVER_NAME'].'/'.$imgurl;
}
if(isset($_GET["type"])?$_GET["type"]:1=="json"){
$rTotal='0';
$gTotal='0';
$bTotal='0';
$total='0';
$imageInfo = getimagesize($img_array[$img]);
//圖片類型
$imgType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
//對應(yīng)函數(shù)
$imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
$i = $imageFun($img_array[$img]);
//測試圖片,自己定義一個,注意路徑
for($x=0;
$x
$x++){
for($y=0;
$y
$y++){
$rgb=imagecolorat($i,$x,$y);
$r=($rgb>>16)&0xFF;
$g=($rgb>>8)&0xFF;
$b=$rgb&0xFF;
$rTotal+=$r;
$gTotal+=$g;
$bTotal+=$b;
$total++;
}
}
$rAverage=round($rTotal/$total);
$gAverage=round($gTotal/$total);
$bAverage=round($bTotal/$total);
$arr=array('ImgUrl'=>$imgurl,'Color'=>"$rAverage,$gAverage,$bAverage");
echo json_encode($arr);
exit();
}
//在頁面顯示圖片地址
//echo $imgurl;
header("location:$imgurl");
在文件所在目錄下創(chuàng)建 images 文件夾,支持二三級文件夾
這是說明
參數(shù):
type=json
JSON調(diào)用格式
http://api.zgymw.com/randacgimg/?return=json
JSON數(shù)據(jù)
{
"code":"200" #圖片狀態(tài)碼
"acgurl":"https:\/\/ws1.sinaimg.cn\/large\/0072Vf1pgy1foxkfy08umj31kw0w0nng.jpg" #圖片地址
"width":"2048" #圖片寬
"height":"1152" #圖片高
}
總結(jié)
以上是生活随笔為你收集整理的php动漫随机图源码,随机图片API源码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nng源码阅读
- 下一篇: 【PHP】创蓝253云通讯平台国际短信A