随机图api教程

API随机获取图片代码(Redis缓存+文件变更检测)

因为使用了Redis缓存,所以宝塔面板得安装好Redis缓存器和Redis缓存插件(打开php管理即可进行安装扩展插件)

参考了梦佬的教程文章

API随机获取图片代码以下是一个简单的PHP代码示例,用于从文本文件中随机选择一张图片,并返回该图片的URL。该代码使用了Redis缓存和文件变更检测,以提高性能和减少IO操作。

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
// Redis配置(根据实际情况修改)
$redisConfig = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '', // 如果有密码请填写
'database' => 0
];

// 初始化Redis连接
$redis = new Redis();
try {
$redis->connect($redisConfig['host'], $redisConfig['port']);
if (!empty($redisConfig['password'])) {
$redis->auth($redisConfig['password']);
}
$redis->select($redisConfig['database']);
} catch (Exception $e) {
die("Redis连接失败: " . $e->getMessage());
}

// 定义文件路径
$imgFile = __DIR__ . '/img.txt';
$counterFile = __DIR__ . '/counter.dat';

// 高性能读取图片列表(Redis缓存+文件变更检测)
$cacheKey = 'cached_pics';
$mtimeKey = 'cached_mtime';

$currentMTime = file_exists($imgFile) ? filemtime($imgFile) : 0;
$cachedMTime = $redis->get($mtimeKey);

if (!$redis->exists($cacheKey) || $currentMTime != $cachedMTime) {
if (!file_exists($imgFile)) die('文件不存在');

// 高效读取文件并过滤空行
$pics = file($imgFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$pics = array_map('trim', $pics);
$pics = array_filter($pics);

if (empty($pics)) die('图片列表为空');

// 存储到Redis(有效期1小时,按需调整)
$redis->setex($cacheKey, 3600, serialize($pics));
$redis->set($mtimeKey, $currentMTime);
} else {
$pics = unserialize($redis->get($cacheKey));
}

// 原子计数器(Redis实现)
$counter = $redis->incr('visit_counter');
// 每100次写入磁盘(降低IO压力)
if ($counter % 100 === 0) {
file_put_contents($counterFile, $counter, LOCK_EX);
}

// 高性能随机选择(避免array_rand内存问题)
$pic = $pics[random_int(0, count($pics) - 1)];

// 输出处理
if (isset($_GET['type']) && $_GET['type'] === 'json') {
header('Content-Type: application/json');
die(json_encode(['pic' => $pic]));
} else {
header("Location: $pic");
exit;
}

上传你的服务器或者主机空间,命名随意后缀为.php

并在同级目录上传一个为img.txt后缀的文件

PS:一个图片链接链接占一行

访问形式为 http(s)://你的域名/你的命名.php

例如 https://api.coul.top/coul.php

输出为图片