Skip to content

Commit f03ccbe

Browse files
author
Crazy-White
committed
support vercel
0 parents  commit f03ccbe

11 files changed

Lines changed: 732 additions & 0 deletions

File tree

LICENSE

Lines changed: 326 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Random-Picture
2+
3+
随机图片api
4+
JUST FOR FUN
5+
6+
## 实验地址:
7+
8+
[http://miku.ueuo.com/](http://miku.ueuo.com/)*(无稳定性保证)*
9+
[https://random-picture.vercel.app/](https://random-picture.vercel.app/)*(部署到vercel)*
10+
11+
### 注意:
12+
13+
| 路径 | 说明 |
14+
| --- | --- |
15+
| ./url.txt | 务必一行一个url,不要输入不完整的url |
16+
| ./api/index.php | 修改ALLOW_OUTPUT以开启output |
17+
18+
## 部署到vercel
19+
20+
建议fork后,自行修改配置,然后在vercel平台上导入自己的项目
21+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?s=https%3A%2F%2Fgithub.com%2FCrazy-White%2FRandom-Picture)
22+
23+
<section>
24+
<h3><strong>直接调用</strong></h3>
25+
<p>访问./api即可,跳转至图片地址</p>
26+
<p>访问./api/?id=数字,跳转至指定图片</p>
27+
<p>注:api附加参数 api=数字 亦可跳转至指定图片</p>
28+
<p>如:<a href="./api/?type=json&id=3">./api/?type=json&id=3</a></p>
29+
</section>
30+
<section>
31+
<h3><strong>访问<a href="./api/?type=length">./api/?type=length</a></strong></h3>
32+
<p>返回图片总量,即id的最大值</p>
33+
</section>
34+
<section>
35+
<h3><strong>访问<a href="./api/?type=output">./api/?type=output</a></strong></h3>
36+
<p>服务器读取后输出,一般不建议使用,默认禁用</p>
37+
</section>
38+
<section>
39+
<h3><strong>访问<a href="./api/?type=json">./api/?type=json</a></strong></h3>
40+
<p>服务器输出json</p>
41+
<pre class="language-json">
42+
<code class="language-json">
43+
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;}
44+
</code>
45+
</pre>
46+
</section>
47+
<section>
48+
<h3><strong>访问<a href="./api/?type=JSON">./api/?type=JSON</a></strong></h3>
49+
<p>服务器读取图片信息后输出json,如非需要图片信息不建议使用</p>
50+
<pre class="language-json">
51+
<code class="language-json">
52+
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;,&quot;width&quot;:&quot;1920&quot;,&quot;height&quot;:&quot;1080&quot;,&quot;mime&quot;:&quot;image\/jpeg&quot;,&quot;size&quot;:&quot;821735&quot;}
53+
</code>
54+
</pre>
55+
</section>

api/index.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
const ALLOW_OUTPUT = false; // 修改以开启
3+
const NOT_FOUND_IMG = "https://i.loli.net/2020/08/17/J7ZU2VAHPQTbcy8.png";
4+
5+
if (file_exists('../url.txt')) {
6+
$url = file('../url.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
7+
} else {
8+
$url = file('http://' .$_SERVER['HTTP_HOST']. '/url.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
9+
}
10+
11+
$id = $_REQUEST['id'];
12+
$type = $_REQUEST['type'];
13+
14+
$is_empty = ($id == "") ? true : false; // 只判断一次
15+
$length = count($url); // 数组长度
16+
$rand_id = $is_empty ? array_rand($url) : $id; // 实际id
17+
18+
if (!$is_empty && $id > $length) {
19+
// exceed maximum length
20+
$code = 404;
21+
$target_url = NOT_FOUND_IMG;
22+
} else {
23+
$code = 200;
24+
$target_url = $url[$rand_id];
25+
}
26+
27+
/**
28+
* 只使用以下变量
29+
* $code $target_url $length
30+
*/
31+
32+
header('Access-Control-Allow-Origin:*');
33+
switch ($type) {
34+
case 'length':
35+
echo $length;
36+
break;
37+
case 'json':
38+
$result = array("code" => "{$code}", "url" => "{$target_url}");
39+
header('Content-type:text/json');
40+
echo json_encode($result);
41+
break;
42+
case 'JSON':
43+
$result = array("code" => "{$code}", "url" => "{$target_url}");
44+
$imageInfo = getimagesize($target_url);
45+
$imageSize = get_headers($target_url, 1)['Content-Length'];
46+
$result['width'] = "{$imageInfo[0]}";
47+
$result['height'] = "{$imageInfo[1]}";
48+
$result['mime'] = "{$imageInfo['mime']}";
49+
$result['size'] = "{$imageSize}";
50+
header('Content-type:text/json');
51+
echo json_encode($result);
52+
break;
53+
case 'output':
54+
header($header);
55+
header("Cache-Control: no-cache");
56+
header("Pragma: no-cache");
57+
if (ALLOW_OUTPUT) {
58+
header('Content-type:image/png');
59+
echo file_get_contents($target_url);
60+
} else {
61+
die("disabled");
62+
}
63+
break;
64+
default:
65+
header("Location:$target_url");
66+
}

index.html

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>随机图片api</title>
6+
<meta charset="utf-8" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1" />
8+
<link rel="dns-prefetch" href="https://cdn.jsdelivr.net/">
9+
<link rel="preload" href="https://cdn.jsdelivr.net/gh/kylelogue/mustard-ui/dist/css/mustard-ui.min.css" as="style">
10+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kylelogue/mustard-ui/dist/css/mustard-ui.min.css" />
11+
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
12+
<script>
13+
$(document).ready(function () {
14+
15+
$('.scroll-down, .get-started').on('click', function (e) {
16+
e.preventDefault()
17+
$('html, body').animate({
18+
scrollTop: $('section:first-of-type').offset().top
19+
}, 300)
20+
})
21+
22+
})
23+
</script>
24+
<style>
25+
header {
26+
background: #a0e5e4
27+
}
28+
29+
.get-started {
30+
background: #43ccc8;
31+
border: 3px solid #50d0cc;
32+
}
33+
</style>
34+
</head>
35+
36+
<body>
37+
<header>
38+
<h1 class="title">随机图片api</h1>
39+
<h2 class="subtitle">说明文档</h2>
40+
<p class="disclaimer"><a href="show.html">图片一览</a></p>
41+
<button class="get-started button button-primary button-large">详情</button>
42+
<a class="scroll-down" href="#"></a>
43+
</header>
44+
<section>
45+
<h3><strong>直接调用</strong></h3>
46+
<p>访问./api即可,跳转至图片地址</p>
47+
<p>访问./api/?id=数字,跳转至指定图片</p>
48+
<p>注:api附加参数 api=数字 亦可跳转至指定图片</p>
49+
<p>如:<a href="./api/?type=json&id=3">./api/?type=json&id=3</a></p>
50+
</section>
51+
<section>
52+
<h3><strong>访问<a href="./api/?type=length">./api/?type=length</a></strong></h3>
53+
<p>返回图片总量,即id的最大值</p>
54+
</section>
55+
<section>
56+
<h3><strong>访问<a href="./api/?type=output">./api/?type=output</a></strong></h3>
57+
<p>服务器读取后输出,一般不建议使用,默认禁用</p>
58+
</section>
59+
<section>
60+
<h3><strong>访问<a href="./api/?type=json">./api/?type=json</a></strong></h3>
61+
<p>服务器输出json</p>
62+
<pre class="language-json">
63+
<code class="language-json">
64+
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;}
65+
</code>
66+
</pre>
67+
</section>
68+
<section>
69+
<h3><strong>访问<a href="./api/?type=JSON">./api/?type=JSON</a></strong></h3>
70+
<p>服务器读取图片信息后输出json,如非需要图片信息不建议使用</p>
71+
<pre class="language-json">
72+
<code class="language-json">
73+
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;,&quot;width&quot;:&quot;1920&quot;,&quot;height&quot;:&quot;1080&quot;,&quot;mime&quot;:&quot;image\/jpeg&quot;,&quot;size&quot;:&quot;821735&quot;}
74+
</code>
75+
</pre>
76+
</section>
77+
<footer>
78+
<p class="copyright align-center">Made by <a href="https://blog.poo.li/">Crazy白茫茫</a>. <a
79+
href="https://github.com/Crazy-White/Random-Picture">Github(Source Code)</a></p>
80+
</footer>
81+
<!--</body>-->
82+
</body>
83+
84+
</html>

now.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"functions": {
3+
"api/index.php": {
4+
"runtime": "vercel-php@0.3.1",
5+
"excludeFiles": "{test/**}",
6+
"memory": 512,
7+
"maxDuration": 5
8+
}
9+
},
10+
"headers": [
11+
{
12+
"source": "/api/index.php",
13+
"headers" : [
14+
{
15+
"key" : "Cache-Control",
16+
"value" : "public, max-age=0, must-revalidate"
17+
}
18+
]
19+
}
20+
]
21+
}

show.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>随机图片api</title>
6+
<meta charset="utf-8" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1" />
8+
<link rel="dns-prefetch" href="https://cdn.jsdelivr.net/">
9+
<link rel="preload" href="https://cdn.jsdelivr.net/gh/kylelogue/mustard-ui/dist/css/mustard-ui.min.css" as="style">
10+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kylelogue/mustard-ui/dist/css/mustard-ui.min.css" />
11+
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
12+
<script src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
13+
<script>
14+
$(document).ready(function () {
15+
$('.scroll-down, .get-started').on('click', function (e) {
16+
e.preventDefault()
17+
$('html, body').animate({
18+
scrollTop: $('section:first-of-type').offset().top
19+
}, 300)
20+
})
21+
});
22+
23+
</script>
24+
<style>
25+
header {
26+
background: #a0e5e4
27+
}
28+
29+
.get-started {
30+
background: #43ccc8;
31+
border: 3px solid #50d0cc
32+
}
33+
34+
* {
35+
word-break: break-all
36+
}
37+
38+
.panel img {
39+
width: 100%
40+
}
41+
42+
.right {
43+
float: right
44+
}
45+
</style>
46+
</head>
47+
48+
<body>
49+
<header>
50+
<h1 class="title">随机图片api</h1>
51+
<h2 class="subtitle">图片一览</h2>
52+
<p class="disclaimer"><a href="index.html">查看说明文档</a></p>
53+
<button class="get-started button button-primary button-large">详情</button>
54+
<a class="scroll-down" href="#"></a>
55+
</header>
56+
<section>
57+
<div class="row" id="output">
58+
59+
</div>
60+
</section>
61+
<footer>
62+
<p class="copyright align-center">Made by <a href="https://blog.poo.li/">Crazy白茫茫</a>. <a
63+
href="https://github.com/Crazy-White/Random-Picture">Github(Source Code)</a></p>
64+
</footer>
65+
66+
<script>
67+
$.get("./url.txt", function (data) {
68+
data.split(/\r\n|\r|\n/).filter(e=>e.length>2).forEach((item, index) => {
69+
document.getElementById('output').innerHTML += `
70+
<div class="col col-xs-12 col-sm-6 col-md-4 col-lg-3">
71+
<div class="panel">
72+
<div class="panel-head">
73+
<p class="panel-title">图片ID:${index}</p>
74+
</div>
75+
<img class="lozad" data-src="${item}" alt="${index}" />
76+
<div class="panel-footer">
77+
<button onclick="window.open('${item}')" class="button-primary right">查看图片</button>
78+
</div>
79+
</div>
80+
</div>`
81+
});
82+
const observer = lozad();
83+
observer.observe();
84+
});
85+
</script>
86+
<!--</body>-->
87+
</body>
88+
89+
</html>

test/360.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
// 从360壁纸获取图片,建议修改GET的url
3+
$api = "http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByCategory&cid=26&start=1&count=200&from=360chrome";
4+
// cid=分类id||start=页数||count=图片数
5+
$arr = json_decode(file_get_contents($api),true);
6+
if ($arr['errmsg'] = "正常") {
7+
$arr_data = $arr['data'];
8+
$img = $arr_data[rand(0,count($arr_data))]['url'];
9+
header("Location:$img");
10+
} else {
11+
$img = "https://i.loli.net/2019/08/21/LZcjgnGYXCp2KFe.jpg"; //出错时的图片
12+
header("Location:$img");
13+
}

test/RandomPicture.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const RandomPicture = Object.create(null);
2+
// https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLImageElement
3+
Object.assign(RandomPicture, {
4+
getElement: function(arr, timeout = 5000) {
5+
if (typeof arr === 'string') arr = [arr]
6+
if (!Array.isArray(arr)) throw new Error('param must be array or string')
7+
if (isNaN(Number(timeout))) throw new Error('param must be number or string')
8+
return new Promise((resolve, reject) => {
9+
let i = 0,
10+
max = arr.length - 1
11+
let process = () => {
12+
let img = new Image()
13+
img.src = arr[i]
14+
let clock = setTimeout(() => {
15+
img.remove()
16+
i++
17+
process()
18+
}, timeout)
19+
20+
img.onload = () => {
21+
clearTimeout(clock)
22+
resolve(img)
23+
}
24+
25+
img.onerror = () = > {
26+
clearTimeout(clock)
27+
img.remove()
28+
i++
29+
process()
30+
}
31+
if (i > max) reject(null)
32+
}
33+
}
34+
}
35+
})
36+
37+
//RandomPicture.getElement(url).then(console.log)

test/bing.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
//Bing Wallpaper 1920x1080
3+
$json = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
4+
$arr = json_decode($json,true);
5+
$img = 'https://cn.bing.com'.$arr['images'][0]['url'];
6+
header("Location:$img");

test/mini.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php $a=file('url.txt')[array_rand(file('url.txt'))];header("Location:$a");

0 commit comments

Comments
 (0)