使用PHP自建随机图API的三种方法

闲来无事突然想找一个自己喜欢的随机图API用作观影类APP的背景图,但翻了半天也没找到自己喜欢的,索性自己做一个。参考了一些文章,结合自己实际情况,总结出三总方式现分享给大家。

图片[1]-使用PHP自建随机图API的三种方法-OMii

准备:

一台已经搭建好的php环境服务器

第一种方法

简单实现随机显示一张图片。

在网站根目录新建一个文件夹,命名为:img(这个文件里放你需要的图片)

新建一个index.php文件,把以下代码填写进去 (这个文件就是api地址,随便命名,命名Index.php那么直接访问域名就可以,如果命名api.php就需要在你的域名后面加上/api.php)

<?php
/*
*代码来自omii.top
*/
$img_array = glob("img/*.{webp,gif,jpg,png}",GLOB_BRACE); 
$img = array_rand($img_array); 
$dz = $img_array[$img];
header("Location:".$dz);
?>

第二种方法

实现随机显示一张图片

在同一目录下创建img.txt和index.php。目录名字可以随便起,比如API。

打开img.txt文件粘贴图片地址,一行一张。列入:

https://static.likepoems.com/2020/02/19/607a4e34144a56fba8.jpg
https://static.likepoems.com/2020/02/19/30a36bd30357fa2710.jpg
https://static.likepoems.com/2020/02/06/385ea1fb5a0a337613.jpg
https://static.likepoems.com/2020/02/12/202101261046307867.png

在index.php输入下面代码并保存:

<?php
/*
*代码来自omii.top
*/
//存放api随机图链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
    die('文件不存在');
}
 
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
    $line=trim(fgets($fs));
    if($line!='' && substr($str , 0 , 1) != '#'){
        array_push($pics, $line);
    }
}
 
// 从数组随机获取链接
$pic = $pics[random_int(0, count($pics) - 1)]; 
 
//返回指定格式
$type=$_GET['type'];
switch($type){
 
//JSON返回
case 'json':
    header('Content-type:text/json');
    die(json_encode(['pic'=>$pic]));
 
default:
    die(header("Location: $pic"));
}
 
?>

访问路径:你的域名/api 即可显示一张随机图片。

第三种方法

通过判断UA实现手机和电脑设备访问给出不同随机图片的功能。

同一目录下比如:API下创建img.txt及img-m.txt,名字随便起,你能区分就好。img.txt存放电脑图片,img-m.txt存放手机图片。判断UA代码,下载Mobile_Detect.php保存。以上完成后创建index.php文件内容如下:

<?php
/*
 *代码来自omii.top
*/
function is_mobile(){
    require('./Mobile_Detect.php');
    $MobileDetect = new Mobile_Detect();
 
    if ($MobileDetect->isTablet()) {
        // 平板定义为PC类
        return false;
    } elseif ($MobileDetect->isMobile()) {
        return true;
    } else {
        return false;
    }
}
 
// 电脑与手机用不同的壁纸
if(is_mobile()){
   // 手机壁纸
   $filename = "img-m.txt";
}else{
   // 电脑壁纸
   $filename = "img.txt";
}
 
//存放api随机图链接的文件名img.txt
if(!file_exists($filename)){
    die('文件不存在');
}
 
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
    $line=trim(fgets($fs));
    if($line!='' && substr($str , 0 , 1) != '#'){
        array_push($pics, $line);
    }
}
 
// 从数组随机获取链接
$pic = $pics[random_int(0, count($pics) - 1)]; 
 
//返回指定格式
$type=$_GET['type'];
switch($type){
 
//JSON返回
case 'json':
    header('Content-type:text/json');
    die(json_encode(['pic'=>$pic]));
 
default:
    die(header("Location: $pic"));
}
 
?>   

访问路径:你的域名/api

注意:如果把index.php改名。访问路径为 你的域名/api/xx.php

本页内容已结束·点赞收藏不迷路
使用PHP自建随机图API的三种方法-OMii
使用PHP自建随机图API的三种方法
此内容为免费资源,请登录后查看专享高速链接
积分
免费资源
© 版权声明
THE END
谢各位老爷们打赏。
点赞9打赏 分享
评论 共1条

请登录后发表评论