首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 生成 Ubuntu 動態壁紙 XML 配置文件(GNOME 背景輪換)

     編輯:PHP 綜合知識

背景格式說明

GNOME 桌面支援 .xml 格式的動態壁紙配置,定義:

  • <starttime>:生效起始時間
  • <static>:靜態顯示時長 + 文件路徑
  • <transition>:過渡動畫時長 + 來源/目標文件

完整生成腳本

<?php
/**
 * 生成 Ubuntu/GNOME 動態壁紙 XML
 * 用法:php gen_wallpaper.php
 * 輸出:圖片目錄下的 wallpaper.xml
 */
$dir = '/home/user/Pictures/Wallpapers';  // 修改為你的圖片目錄
$outputFile = $dir . '/wallpaper.xml';

// 掃描圖片 (支援 jpg/jpeg/png/gif)
$allowedExt = ['jpg', 'jpeg', 'png', 'gif'];
$files = [];

$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    if (in_array($ext, $allowedExt, true)) {
        $files[] = $dir . '/' . $file;
    }
}
closedir($dh);

// 自然排序 (img1.jpg, img2.jpg, img10.jpg)
usort($files, 'strnatcmp');

if (empty($files)) {
    die("目錄無圖片文件: $dir\n");
}

// 構建 XML
$xml = new XMLWriter();
$xml->openMemory();
$xml->setIndent(true);
$xml->setIndentString('  ');
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('background');

// 起始時間 (固定 2000-01-01 00:00:00)
$xml->startElement('starttime');
$xml->writeElement('year', '2000');
$xml->writeElement('month', '01');
$xml->writeElement('day', '01');
$xml->writeElement('hour', '00');
$xml->writeElement('minute', '00');
$xml->writeElement('second', '00');
$xml->endElement(); // starttime

$count = count($files);
$staticDuration = 300.0;   // 靜態顯示 5 分鐘 (秒)
$transitionDuration = 5.0; // 過渡 5 秒

for ($i = 0; $i < $count; $i++) {
    $current = $files[$i];
    $next = $files[($i + 1) % $count]; // 循環到第一張
    
    // 靜態
    $xml->startElement('static');
    $xml->writeElement('duration', (string)$staticDuration);
    $xml->writeElement('file', $current);
    $xml->endElement();
    
    // 過渡
    $xml->startElement('transition');
    $xml->writeElement('duration', (string)$transitionDuration);
    $xml->writeElement('from', $current);
    $xml->writeElement('to', $next);
    $xml->endElement();
}

$xml->endElement(); // background
$xmlContent = $xml->outputMemory(true);

// 寫入文件
file_put_contents($outputFile, $xmlContent);
echo "已生成: $outputFile ($count 張圖片)\n";

// 應用方法:
// gsettings set org.gnome.desktop.background picture-uri "file://$outputFile"
// 或圖形界面:設置 → 背景 → 選擇 XML 文件
?>

生成的 XML 結構示例

<?xml version="1.0" encoding="UTF-8"?>
<background>
  <starttime>
    <year>2000</year>
    <month>01</month>
    <day>01</day>
    <hour>00</hour>
    <minute>00</minute>
    <second>00</second>
  </starttime>
  <static>
    <duration>300.0</duration>
    <file>/home/user/Pictures/Wallpapers/img1.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/home/user/Pictures/Wallpapers/img1.jpg</file>
    <to>/home/user/Pictures/Wallpapers/img2.jpg</file>
  </transition>
  <static>
    <duration>300.0</duration>
    <file>/home/user/Pictures/Wallpapers/img2.jpg</file>
  </static>
  <transition>...</transition>
  ...
</background>

進階功能擴展

需求實現方式
按文件名數字排序usort($files, 'strnatcmp') 已包含
隨機順序shuffle($files)
不同時長解析 EXIF 拍攝時間、或配置文件指定
多顯示器不同壁紙生成多個 XML,配合 gsettings 針對性設置
定時自動更新Cron 任務每日運行腳本,掃描新圖片重新生成
Web 界面管理Laravel/Symfony + 前端拖拽排序、預覽、一鍵生成

應用命令

# 終端應用
gsettings set org.gnome.desktop.background picture-uri "file:///home/user/Pictures/Wallpapers/wallpaper.xml"

# 開機自動應用 (加入啟動應用)
# 或創建 systemd --user 服務

跨桌面環境兼容性

  • GNOME / Unity / Cinnamon / MATE / Pantheon:原生支援
  • KDE Plasma:不支援此 XML 格式,需用 plasma-wallpaperviewer 或腳本輪換
  • XFCE / LXDE:需額外工具 (如 xfdesktop 定時器腳本)
  • Windows:不適用,使用「幻燈片」功能或第三方工具

總結

利用 PHP XMLWriter 生成標準動態壁紙配置,代碼簡潔、可擴展。結合 Cron 定時掃描新圖片,即可實現「丟進資料夾自動輪換」的個性化桌面體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved