随机图片API部署

Github 准备 1 2 #安装所需程序 sudo apt install nginx php7.4-fpm php7.4-mysql php7.4-fpm php7.4-mysql php7.4-cli php7.4-opcache certbot python3-certbot-nginx python3-certbot-dns-cloudflare 创建Index.php文件 这里只需要修改路径 主路径 1 2 cd /var/www/html nano index.php 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 68 69 70 71 72 <?php $pcPath = '/landscape';#这里的路径自行修改 $mobilePath = '/portrait';#这里的路径自行修改 // 函数:从目录中获取图片列表 function getImagesFromDir($path) { $images = array(); if ($img_dir = @opendir($path)) { while (false !== ($img_file = readdir($img_dir))) { // 匹配 webp、jpg、jpeg、png、gif 格式的图片 if (preg_match("/\.(webp|jpg|jpeg|png|gif)$/i", $img_file)) { $images[] = $img_file; } } closedir($img_dir); } return $images; } // 函数:生成完整的图片路径 function generateImagePath($path, $img) { return $path . '/' . $img; } // 检测用户代理以区分手机和电脑访问 $userAgent = $_SERVER['HTTP_USER_AGENT']; $isMobile = preg_match('/(android|iphone|ipad|ipod|blackberry|windows phone)/i', $userAgent); // 根据访问设备设置图片路径 if ($isMobile) { $path = $mobilePath; } else { $path = $pcPath; } // 缓存图片列表 $imgList = getImagesFromDir($path); // 从列表中随机选择一张图片 shuffle($imgList); $img = reset($imgList); // 获取图片的格式 $img_extension = pathinfo($img, PATHINFO_EXTENSION); // 根据图片的格式设置 Content-Type switch ($img_extension) { case 'webp': header('Content-Type: image/webp'); break; case 'jpg': case 'jpeg': header('Content-Type: image/jpeg'); break; case 'png': header('Content-Type: image/png'); break; case 'gif': header('Content-Type: image/gif'); break; // 添加其他格式的处理方式 // case 'bmp': // header('Content-Type: image/bmp'); // break; } // 生成完整的图片路径 $img_path = generateImagePath($path, $img); // 直接输出所选的随机图片 readfile($img_path); ?> PC路径 1 2 mkdir /var/www/html/pc && cd /var/www/html/pc nano index.php 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 <?php $pcPath = '../landscape';#这里的路径自行修改 // 函数:从目录中获取图片列表 function getImagesFromDir($path) { $images = array(); if ($img_dir = @opendir($path)) { while (false !== ($img_file = readdir($img_dir))) { // 匹配 webp、jpg、jpeg、png、gif 格式的图片 if (preg_match("/\.(webp|jpg|jpeg|png|gif)$/i", $img_file)) { $images[] = $img_file; } } closedir($img_dir); } return $images; } // 函数:生成完整的图片路径 function generateImagePath($path, $img) { return $path . '/' . $img; } // 设置图片路径为 landscape $path = $pcPath; // 缓存图片列表 $imgList = getImagesFromDir($path); // 从列表中随机选择一张图片 shuffle($imgList); $img = reset($imgList); // 获取图片的格式 $img_extension = pathinfo($img, PATHINFO_EXTENSION); // 根据图片的格式设置 Content-Type switch ($img_extension) { case 'webp': header('Content-Type: image/webp'); break; case 'jpg': case 'jpeg': header('Content-Type: image/jpeg'); break; case 'png': header('Content-Type: image/png'); break; case 'gif': header('Content-Type: image/gif'); break; // 添加其他格式的处理方式 // case 'bmp': // header('Content-Type: image/bmp'); // break; } // 生成完整的图片路径 $img_path = generateImagePath($path, $img); // 直接输出所选的随机图片 readfile($img_path); ?> Mobile路径 1 2 mkdir /var/www/html/mobile && cd /var/www/html/mobile nano index.php 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 <?php $pcPath = '../portrait';#这里的路径自行修改 // 函数:从目录中获取图片列表 function getImagesFromDir($path) { $images = array(); if ($img_dir = @opendir($path)) { while (false !== ($img_file = readdir($img_dir))) { // 匹配 webp、jpg、jpeg、png、gif 格式的图片 if (preg_match("/\.(webp|jpg|jpeg|png|gif)$/i", $img_file)) { $images[] = $img_file; } } closedir($img_dir); } return $images; } // 函数:生成完整的图片路径 function generateImagePath($path, $img) { return $path . '/' . $img; } // 设置图片路径为 portrait $path = $pcPath; // 缓存图片列表 $imgList = getImagesFromDir($path); // 从列表中随机选择一张图片 shuffle($imgList); $img = reset($imgList); // 获取图片的格式 $img_extension = pathinfo($img, PATHINFO_EXTENSION); // 根据图片的格式设置 Content-Type switch ($img_extension) { case 'webp': header('Content-Type: image/webp'); break; case 'jpg': case 'jpeg': header('Content-Type: image/jpeg'); break; case 'png': header('Content-Type: image/png'); break; case 'gif': header('Content-Type: image/gif'); break; // 添加其他格式的处理方式 // case 'bmp': // header('Content-Type: image/bmp'); // break; } // 生成完整的图片路径 $img_path = generateImagePath($path, $img); // 直接输出所选的随机图片 readfile($img_path); ?> 反代开启HTTPS 1 2 3 4 #清空默认配置 > /etc/nginx/sites-available/default #手动编辑默认配置 sudo nano /etc/nginx/sites-available/default 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 server { listen [::]:8080; listen 8080; server_name example.com; # 替换为你的域名以便下面申请域名 root /var/www/html; # 设置网站根目录 # 默认首页是 index.php 或其他文件(根据你的需求) index index.php index.html index.htm; # 处理根目录请求,直接返回根目录下的文件(如果存在) location / { try_files $uri $uri/ /index.php?$query_string; } # 处理 PHP 文件 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # 防止访问 .ht 文件(如果有) location ~ /\.ht { deny all; } } 1 2 3 4 #测试Nginx配置 sudo nginx -t #重新加载Nginx配置 sudo systemctl reload nginx 现在可以访问ip:8080查看效果 ...

五月 15, 2025 · 5 分钟 · 2278 字 · Beiyuan