因为一直用的图片API突然更新了,返回json格式的图片api我不会用,于是东拼西凑有了这么个东西。
2024.12.12更新
添加前缀数组支持多个cdn随机生成链接
新新代码
index.php 代码
<?php
function get_data($file) {
$lines = file($file, FILE_IGNORE_NEW_LINES);
return $lines[array_rand($lines)];
}
function generate_url($data, $prefixes, $suffix) {
$prefix = $prefixes[array_rand($prefixes)];
return $prefix . $data . $suffix;
}
// 检测设备类型
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'Mobile')) {
$device = 'mobile';
$file = 'pe.txt';
$prefixes = [
'CDN1前缀',
'CDN2前缀',
'CDN3前缀',
];
$suffix = ').webp';
} else {
$device = 'pc';
$file = 'pc.txt';
$prefixes = [
'CDN1前缀',
'CDN2前缀',
'CDN3前缀',
];
$suffix = ').webp';
}
$data = get_data($file);
$url = generate_url($data, $prefixes, $suffix);
header("Location: $url");
?>
pc.php/pe.php代码
<?php
function get_data($file) {
$lines = file($file, FILE_IGNORE_NEW_LINES);
return $lines[array_rand($lines)];
}
function generate_url($data, $prefixes, $suffix) {
$prefix = $prefixes[array_rand($prefixes)];
return $prefix . $data . $suffix;
}
$file = 'pc.txt'; // pe.txt或pc.txt
$data = get_data($file);
$prefixes = [
'CDN1前缀',
'CDN2前缀',
'CDN3前缀',
];
$suffix = ').webp'; // 你的后缀
$url = generate_url($data, $prefixes, $suffix);
header("Location: $url");
?>
2024.8.10更新
随着图片链接增多txt文件也越来越大,参照二次寒树的思路拆分url以减小txt体积。
新代码
index.php 代码
<?php
function get_data($file) {
$lines = file($file, FILE_IGNORE_NEW_LINES);
return $lines[array_rand($lines)];
}
function generate_url($data, $prefix, $suffix) {
return $prefix . $data . $suffix;
}
// 检测设备类型
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'Mobile')) {
$device = 'mobile';
$file = 'pe.txt'; txt文件名
$prefix = 'https://example.com/example/S ('; // 前缀
$suffix = ').webp'; // 后缀
} else {
$device = 'pc';
$file = 'pc.txt'; txt文件名
$prefix = 'https://example.com/example/H ('; // 前缀
$suffix = ').webp'; // 后缀
}
$data = get_data($file);
$url = generate_url($data, $prefix, $suffix);
header("Location: $url");
?>
pc.php/pe.php代码
<?php
function get_data($file) {
$lines = file($file, FILE_IGNORE_NEW_LINES);
return $lines[array_rand($lines)];
}
$file = 'pc.txt'; // txt文件名 pe.php为pe.txt
$data = get_data($file);
$prefix = 'https://example.com/example/H ('; // 前缀
$suffix = ').webp'; // 后缀
$url = $prefix . $data . $suffix;
header("Location: $url");
?>
旧代码
index.php 代码
<?php
function isMobile(){
$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ”;
$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:”;
function CheckSubstrs($substrs,$text){
foreach($substrs as $substr)
if(false!==strpos($text,$substr)){
return true;
}
return false;
}
$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
$found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||
CheckSubstrs($mobile_token_list,$useragent);
if ($found_mobile){
return true;
}else{
return false;
}
}
$pc = 'pc.php';
$pe = 'pe.php';
if (isMobile()){
header("Location:".$pe);
}else{
header("Location:".$pc);
}
?>
pc.php 代码
<?php $a=file('pc.txt');$b=$a[array_rand($a)];header("Location:$b");
pe.php 代码
<?php $a=file('pe.txt');$b=$a[array_rand($a)];header("Location:$b");
index.php :自适应设备分别输出pc.php&pe.php
pc.php&pe.php :分别从pc.txt&pe.txt中获取图片链接
pc.txt&pe.txt :pc.txt放横图链接 pe.txt放竖图链接 每行一个链接