侧边栏壁纸
博主头像
v林羽博主等级

行动起来,活在当下

  • 累计撰写 171 篇文章
  • 累计创建 34 个标签
  • 累计收到 14 条评论

目 录CONTENT

文章目录

【Docker项目】之--简单的PHP随机图片生成程序

v林羽
2022-12-11 / 0 评论 / 0 点赞 / 577 阅读 / 6020 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-11-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

【Docker项目】之–简单的PHP随机图片生成程序

#教程 #工具 #Docker #Docker-compose #Linux #yaml #Ubuntu

本程序需要PHP环境,如果站点已经安装PHP可以直接使用。

这里直接在docker版的Eayimage中搭建(PHP环境为7.4),而且不影响后续迁移,Eayimage搭建参考:【Docker项目】之–Easyimage搭建一个简单图床

1. 设置Eayimage

首先打开设置–>图床安全–>文件管理(为确保安全设置完后记得关闭)。

打开文件管理

新建random文件夹,用于存放随机图片和PHP文件。

新建bg文件夹放入需要展示的随机图片,bg.php文件放置代码。

调整文件执行权限。

2. 方法一:随机读取文件夹图片API

打开bg.php文件填入代码如下,按照自己的站点信息修改。

<?php
//设置站点地址及图片文件夹
$weburl= 'https://yourdomain.com/i/random/';
$path = 'bg';

function getImagesFromDir($path) {
    $images = array();
    if ( $img_dir = @opendir($path) ) {
        while ( false !== ($img_file = readdir($img_dir)) ) {
            if ( preg_match("/(\.gif|\.jpg|\.png|\.webp)$/", $img_file) ) {
                $images[] = $img_file;
            }
        }
        closedir($img_dir);
    }
    return $images;
}
function getRandomFromArray($ar) {
    mt_srand( (double)microtime() * 1000000 );
    $num = array_rand($ar);
    return $ar[$num];
}
$imgList = getImagesFromDir($path);
$img = getRandomFromArray($imgList);

header("Location:" . $weburl. $path . '/' . $img);

?>

可以访问https://yourdomain.com/i/random/bg.php测试一下。

3. 方法二:随机读取图片地址API

新建img.txt用于存放图片地址。

填入图片来源,也可以使用站外的图片地址。

bg.php代码。

<?php

const imageFiles = './img.txt';

$data = file(imageFiles, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (empty($data[0])){
    header('HTTP/1.1 503 Service Unavailable');
    die ('503 Service Unavailable');
}

$id = array_rand($data) + 1;
settype($id,'integer');
if ($id <= 0 || $id > $quantity){
    $id = array_rand($data) + 1;
}

$pic = $data[$id - 1];
header("Location:" . $pic);

?>

4. 方法三:随机读取图片地址API带扩展返回

使用方法。

api.php?id=[图片ID]&type=[返回类型]

## 返回指定id的图片:api.php?id=6

## 返回指定id的图片json信息:api.php?id=6&type=json
{"id":438,"width":"1536","height":"864","url":"https:\/\/yourdomain.com\/i\/random\/bg\/random006.webp"}

## 返回随机图片总数:api.php?type=quantity

代码。

<?php

const imageFiles = './img.txt';

//读取数据
$data = file(imageFiles, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (empty($data[0])){
    header('HTTP/1.1 503 Service Unavailable');
    die ('503 Service Unavailable');
}

//初始化
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Origin: *');
$id = $_REQUEST['id'];
$type = $_REQUEST['type'];
$quantity = count($data);

//处理id
if(!isset($id)){
    $id = array_rand($data) + 1;
}
settype($id,'integer');
if ($id <= 0 || $id > $quantity){
    $id = array_rand($data) + 1;
}

//输出
$pic = $data[$id - 1];
switch ($type) {
    case 'quantity':
        echo $quantity;
        break;
    case 'json':
        $imageInfo = getimagesize($pic);
        $result = [
            'id' => $id,
            'width' => "$imageInfo[0]",
            'height' => "$imageInfo[1]",
            'url' => $pic
        ];
        header('Content-Type: text/json');
        echo json_encode($result);
        break;
    default:
        header("Location:" . $pic);
}

?>

5. 解决同一站点访问随机图片相同的问题

## 在访问连接后面设置参数,参数可以任意,不重复就可以。
bg.php?_r=792

代码来源,稍有改动:

  1. OriLight152/Simple-Random-Picture: 简易的随机图片API

  2. Oblvnn/random-image-api: A simple random-image-api written in PHP.

0

评论区