🔳 第一种代码 |
- 安装环境准备
𓀋 1.宝塔面板 𓀋 2.PHP8.0 𓀋 3.域名
- 第一个步骤
➡️宝塔后台➡️网站➡️添加站点
- 第二个步骤
💨设置自己的域名
💨网站根目录创建"pcing"和"peing"文件夹,(其中pc存放电脑端图片,pe存放手机端图片)
💨创建"random"php,并添加以下代码
<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
//var_dump($agent);
if(stripos($agent,'android')!==false || stripos($agent, 'iphone')!==false){
$img_array = glob('peimg/*.{gif,jpg,png,jpeg,webp,bmp,JPG}', GLOB_BRACE);
if (count($img_array) == 0) {
die('没找到pe图片文件。请先上传一些图片到 ' . dirname(__FILE__) . '/peimg/ 文件夹');
}
header('Content-Type: image/png');
echo file_get_contents($img_array[array_rand($img_array)]);
}
else {
$img_array = glob('pcimg/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);
if (count($img_array) == 0) {
die('没找到pc图片文件。请先上传一些图片到 ' . dirname(__FILE__) . '/pcimg/ 文件夹');
}
header('Content-Type: image/png');
echo file_get_contents($img_array[array_rand($img_array)]);
}
🔳 第二种代码 |
第一步:
用宝塔新建一个站点,并再新建文件:index.php。存储以下代码:<?php
if($_GET['folder']){
$folder=$_GET['folder'];
}else{
$folder='/img/';
}
//存放图片文件的位置
$path = $_SERVER['DOCUMENT_ROOT']."/".$folder;
$files=array();
if ($handle=opendir("$path")) {
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file,-3)=='gif' || substr($file,-3)=='jpg') $files[count($files)] = $file;
}
}
}
closedir($handle);
$random=rand(0,count($files)-1);
if(substr($files[$random],-3)=='gif') header("Content-type: image/gif");
elseif(substr($files[$random],-3)=='jpg') header("Content-type: image/jpeg");
readfile("$path/$files[$random]");
?>
第二步:在站点中新建"img"文件夹,将图片存入,访问网址就可以。