随机图片API源码 | 祭夜博客
  • 欢迎光临,这个博客颜色有点多

随机图片API源码

Wordpress msojocs 6年前 (2018-06-06) 4363次浏览 已收录 1个评论 扫描二维码

下列代码保存任意文件名的php文件

 

<?php
header('Cache-Control:no-cache,must-revalidate');
header('Pragma:no-cache');
header("Expires:0");
header("Access-Control-Allow-Origin:*");
//处理请求输出数据
//这将得到一个文件夹中的所有gif,jpg和png图片的数组
$rand=rand(0,1);
if($rand){
    $localurl="images/*/*.{gif,jpg,png}";
}else{
    $localurl="images/*/*/*.{gif,jpg,png}";
}
$img_array=glob($localurl,GLOB_BRACE);
//从数组中选择一个随机图片 
$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));
    //对应函数
    $imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
    $i = $imageFun($img_array[$img]);
	//测试图片,自己定义一个,注意路径
	for($x=0;
	$x<imagesx($i);
	$x++){
		for($y=0;
		$y<imagesy($i);
		$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");

在文件所在目录下创建images文件夹,支持二三级文件夹

这是说明
传送门


祭夜の咖啡馆 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:随机图片API源码
喜欢 (4)
[1690127128@qq.com]
分享 (0)
发表我的评论
取消评论
OwO表情
贴图 加粗 删除线 居中 斜体 签到

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 学习了,涨姿势啊!!
    付费阅读2018-06-19 22:49 回复 Windows 10 | Firefox浏览器 50.0