使用阿里云函数和微博图床搭建高速随机图片api

之前用的也是阿里云函数,图片放到阿里oss+阿里cdn,然而速度总是那么不尽人意。
然后看到别人( < api.vinua.cn/tuapi > )放在美国1h1g但是速度秒杀我时,我自闭了。

仔细分析了原因后我明白了,美国服务器产生随机图片重定向不需要很久的时间,大约100~200ms(算上延迟)

那我放在阿里云函数那速度不是比他美国还快?所以我把图片从阿里oss+cdn搬到了微博图片上,微博那cdn叫一个快啊!

适用于阿里云函数的随机图片源码我找了很久都找不到,希望我自己写的可以帮到你。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

<?php

use RingCentral\Psr7\Response;



function handler($request, $context): Response{

$filename = "imgurl.txt";

//从文本获取链接

$pics = [];

$fs = fopen($filename, "r");

while(!feof($fs)){

$line=trim(fgets($fs));

if($line!=''){

array_push($pics, $line);

}

}

//从数组随机获取链接

$pic = $pics[array_rand($pics)];

$num=rand(1,4);//随机变量

$pic = str_replace("https://tva4","https://tva".$num,$pic);

return new Response(

302,

array(

'Location' => $pic

),

""

);

}