之前發過一篇PHP簡單實現一言 / 隨機一句功能,既然隨機的文字有了,隨機圖像當然不能落下。
方法一:隨機讀取資料夾圖片API
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
$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 $img=file('img.txt');//txt文件
$url=array_rand($img);//imgtxt文檔裏面圖片API
header("Location:".$img[$url]); ?>
以上的代碼會查找 images 目錄下的所有圖片,並隨機挑選出一張顯示出來。
方法二:隨機讀取網址資料夾圖片API
<?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
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
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);
}
?>
解決同一站點訪問隨機圖片相同的問題
## 在訪問連接後面設置參數,參數可以任意,不重複就可以。
bg.php?_r=792
如想實現分類的話可查看此文章:
如想從Bing API中自動取隨機圖則查看此文章:
文章標題:PHP 實現隨機圖像功能
本文鏈接:https://angelal.cc/1227.html
文章版權:除非特別註明,否則均為AngelaL的原創文章,轉載必須以鏈接形式標明本文鏈接
本文最後更新發佈於:2025年02月26日 19:52, 某些文章具有時效性,若有錯誤或已失效,請在下方留言。