熱評
加載中...
當前位置:首頁技術館建站教學網站建設PHP 實現隨機圖像功能

PHP 實現隨機圖像功能

之前發過一篇PHP簡單實現一言 / 隨機一句功能,既然隨機的文字有了,隨機圖像當然不能落下。

PHP 實現隨機圖像功能-AngelaL博客
PHP 實現隨機圖像功能-AngelaL博客

PHP簡單實現隨機一言API

方法一:隨機讀取資料夾圖片API

PHP 隨機圖像實現的代碼超級簡單,短短四行就搞定了:

PHP
<?php
$img_array = glob('images/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);
if(count($img_array) == 0) die('沒找到圖片文件。請先上傳一些圖片到 '.dirname(__FILE__).'/images/ 文件夾');
header('Content-Type: image/png');
echo(file_get_contents($img_array[array_rand($img_array)]));
?>

或者

PHP
<?php 
$img_array = glob("imgs/*.{gif,jpg,png}",GLOB_BRACE); 
$img = array_rand($img_array); 
echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />'; 
?>

或者

PHP
<?php $img=file('img.txt');//txt文件
$url=array_rand($img);//imgtxt文檔裏面圖片API
header("Location:".$img[$url]); ?>

以上的代碼會查找 images 目錄下的所有圖片,並隨機挑選出一張顯示出來。

方法二:隨機讀取網址資料夾圖片API

PHP
<?php
//設置站點地址及圖片文件夾
$weburl= 'https://yourdomain.com/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/random/bg.php 測試一下。

方法三:隨機讀取圖片位址API

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);

?>

方法四:隨機讀取圖片位址API帶擴展返回

api.php?id=[圖片ID]&type=[返回類型]<br>## 返回指定id的圖片:api.php?id=6<br>## 返回指定id的圖片json資訊:api.php?id=6&type=json<br>{“id”:438,“width”:“1536”,“height”:“864”,“url”:“https:\/\/yourdomain.com\/i\/random\/bg\/random006.webp”}<br>## 返回隨機圖片總數:api.php?type=quantity

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');
}

//初始化
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);
}

?>

解決同一站點訪問隨機圖片相同的問題

PHP
## 在訪問連接後面設置參數,參數可以任意,不重複就可以。
bg.php?_r=792

如想實現分類的話可查看此文章:

PHP 實現隨機圖像功能-AngelaL博客
PHP 實現隨機圖像功能-AngelaL博客

PHP圖片介面API帶分類

如想從Bing API中自動取隨機圖則查看此文章:

PHP 實現隨機圖像功能-AngelaL博客
PHP 實現隨機圖像功能-AngelaL博客

PHP隨機Bing每日一圖APi代碼及調用方法

AngelaL博客

       本文是原創文章,採用CC BY-NC-SA 4.0協議,完整轉載請註明來自AngelaL博客

給TA打賞
共{{data.count}}人
人已打賞
網站建設

PHP簡單實現隨機一言API

2025-2-26 19:42:26

網站建設

PHP隨機Bing每日一圖APi代碼及調用方法

2025-2-26 21:26:42

0 條回复 A文章作者 M管理员
    
    暫無討論,說說你的看法吧
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索