<?php
// 使用中的文件，源
$imagesDir = '.';
$workingDir = getcwd();
$imgPath = $workingDir . DIRECTORY_SEPARATOR . $imagesDir;

// 直接获取当前目录的图片文件（无需检查目录是否存在）
chdir($imgPath);
$files = glob("*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}", GLOB_BRACE);
chdir($workingDir);

//生成图片路径数组（直接使用文件名）
$imageSources = array_map(function ($file) {
 return rawurlencode($file); 
 }, $files);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>图片浏览器</title>
 <style>
@font-face {
				font-family:'pmzd';
				src: url(http://media.mf.103648.xyz/dist/fonts/PangMenZhengDaoBiaoTiTi-1.ttf);
			}
@font-face {
				font-family:'jxzj2';
				src: url(http://media.mf.103648.xyz/dist/fonts/jiangxizhuojing2.0.ttf);
			}
@font-face {
				font-family:'OPPOSans-H';
				src: url(http://media.mf.103648.xyz/dist/fonts/OPPOSans-H.ttf);
			}
 /* 主布局优化 */
 body, html {
 margin:0;
 padding:0;
 overflow: hidden;
 height:100%;
 }

 .container {
 display: flex;
 height:100vh;
 transition: all0.3s;
 }

 /* 主图区域 */
 .main-container {
 position: relative;
 flex:1;
 overflow: hidden;
 }

 .main-image {
 width:100%;
 height:100%;
 background-size: contain;
 background-position: center;
 background-repeat: no-repeat;
 transition: background-image0.3s;
 }

 /* 导航按钮样式 */
 .nav-btn {
 position: absolute;
 width:40%;
 height:95%;
 background: rgba(255,255,255,0.01);
 border-radius:0;
 display: flex;
 align-items: center;
 justify-content: center;
 cursor: pointer;
 opacity:0.8;
 transition: opacity0.3s;
 color: #faa; /* 图标颜色 */
}

.left {
 left:20px;
 top:50%;
 transform: translateY(-75%);
}

.right {
 right:20px;
 top:50%;
 transform: translateY(-75%);
}

.center {
 bottom:20px;
 left:50%;
 transform: translateX(-50%);
 width:600px;
 height:60px;
 border-radius:5%;
 background: rgba(255,255,255,0.2);
}


 /* 缩略图容器 */
 .thumbnail-container {
 display: none;
 width:30%;
 max-height:100%;
 overflow-y: auto;
 padding:10px;
 background: rgba(0,0,0,0.8);
 }

 .thumbnail-item {
 width:100px;
 height:100px;
 margin:5px;
 background-size: cover; 
 /* 缩略图保持比例裁剪 */
 background-position: center;
 cursor: pointer;
 transition: transform0.2s;
 }

 .thumbnail-item:hover { transform: scale(1.1); }

 /* 响应式布局 */
 @media (orientation: portrait) {
 .container { flex-direction: column; }
 .thumbnail-container { width:100%; max-height:30vh; }
 }

 @media (orientation: landscape) {
 .container { flex-direction: row; }
 }
 </style>
</head>
<body>
 <div class="container">
 <!-- 主图区域 -->
 <div class="main-container">
 <div class="main-image" id="mainImage"></div>
 <!-- 导航按钮 -->
 <div class="nav-btn left" onclick="prevImage()">
 &lt; <!-- 左箭头符号 -->
 </div>
 <div class="nav-btn right" onclick="nextImage()">
 &gt; <!-- 右箭头符号 -->
 </div>
 <div class="nav-btn center" onclick="toggleThumbs()">
 ▼ <!-- 切换缩略图按钮图标 -->
 </div>
 </div>

 <!-- 缩略图区域 -->
 <div class="thumbnail-container" id="thumbnailContainer"></div>
 </div>

 <script>
 // 初始化变量 
 let currentImage =0;
 const images = <?php echo json_encode($imageSources); ?>;
 const thumbContainer = document.getElementById('thumbnailContainer');

 // 预加载所有图片（关键优化点）
 const preloadImages = images.map(src => {
 const img = new Image();
 img.src = src; 
 return img;
 });

 // 初始化图片和缩略图 
 document.addEventListener('DOMContentLoaded', function() {
 if (images.length ===0) {
 alert("未找到图片，请确保图片与PHP文件同级存放！");
 return;
 }

 // 设置初始主图 
 document.getElementById('mainImage').style.backgroundImage = `url(${images[0]})`;

 // 创建缩略图 
 images.forEach((src, index) => {
 const div = document.createElement('div');
 div.className = 'thumbnail-item';
 div.style.backgroundImage = `url(${src})`;
 div.addEventListener('click', () => selectImage(index));
 thumbContainer.appendChild(div);
 });
 });

 // 图片切换函数 
 function selectImage(index) {
 currentImage = index;
 updateMainImage();
 highlightThumbnail(index);
 }

 function prevImage() {
 currentImage = (currentImage >0) ? currentImage -1 : images.length -1;
 selectImage(currentImage);
 }

 function nextImage() {
 currentImage = (currentImage < images.length -1) ? currentImage +1 :0;
 selectImage(currentImage);
 }

 function updateMainImage() {
 document.getElementById('mainImage').style.backgroundImage = `url(${images[currentImage]})`;
 }

 // 缩略图高亮 
 function highlightThumbnail(index) {
 const thumbs = document.querySelectorAll('.thumbnail-item');
 thumbs.forEach((thumb, i) => {
 thumb.style.boxShadow = (i === index) ? '0010px #fff' : '';
 });
 }

 // 切换缩略图显示 
 function toggleThumbs() {
 const container = document.querySelector('.thumbnail-container');
 container.style.display = container.style.display === 'none' ? 'block' : 'none';
 }

 // 自动适配屏幕方向 
//    硬链接源文件
 window.addEventListener('resize', () => {
 const container = document.querySelector('.container');
 if (window.innerWidth > window.innerHeight) {
 container.style.flexDirection = 'row';
 } else {
 container.style.flexDirection = 'column';
 }
 });
 </script>
</body>
</html>
