接着前面爬取的资源继续讨论:
创建一个图片API随机调用接口
来个成品
1.在serv00中创建PHP类型网站并绑定域名
2.修改index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Linuxdo Random Images</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-size: cover;
background-position: center;
transition: background-image 1s ease-in-out;
}
.header {
background-color: rgba(0, 0, 0, 0.7);
padding: 20px;
text-align: center;
font-size: 2.5em;
font-weight: bold;
background: linear-gradient(90deg, #ff0080, #00bfff);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.content {
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
}
.content h2 {
font-weight: bold;
font-size: 2em;
background: linear-gradient(90deg, #ff0080, #00bfff);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.api-link {
font-size: 1.5em;
font-weight: bold;
background: linear-gradient(90deg, #ff0080, #00bfff);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.section {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="header">
<h1><a href="https://linux.do" style="text-decoration:none; color:inherit;">Linuxdo Random Images</a></h1>
</div>
<div class="content section">
<h2>随机图片说明</h2>
<p>目前系统共收录10000+张图片。</p>
<p>图片均为HTTPS协议,采用本地储存图,感谢https://tuapi.eees.cc站收集的图片。</p>
<p>感谢serv00主机商提供的虚拟主机。</p>
</div>
<div class="content section">
<h3 class="api-link">接口地址:</h3>
<p><a href="https://你的域名/random.php" target="_blank">https://你的域名/random.php</a></p>
</div>
<script>
function updateBackground() {
document.body.style.backgroundImage = "url('https://你的域名/random.php?" + new Date().getTime() + "')";
}
setInterval(updateBackground, 15000);
updateBackground();
</script>
</body>
</html>
域名改为你的,其余部分自行调整
3.在serv00中创建一个random.php
文本
<?php
// 设置图片目录
$imageDir = 'images';
// 使用 glob 函数获取所有JPG图片
$images = glob($imageDir . '/*.jpg');
// 检查是否有图片
if (count($images) > 0) {
// 随机选择一张图片
$randomImage = $images[array_rand($images)];
} else {
// 如果没有找到图片,设置一个默认图像
$randomImage = 'images/default.jpg'; // 确保这个默认图像存在
}
// 获取文件名和扩展名
$imageInfo = pathinfo($randomImage);
$imageName = $imageInfo['basename'];
// 构建完整的图片URL
$imageUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $randomImage;
// 设置响应头为重定向到图片URL
header("Location: $imageUrl");
exit();
4.创建images
文件
在images文件下放入你的图片,并创建.htaccess
文本
Options -Indexes