Skip to content

Instantly share code, notes, and snippets.

@TheExpertNoob
Last active September 27, 2023 17:42
Show Gist options
  • Save TheExpertNoob/06358fba4de0c7346b912ae48771fdb3 to your computer and use it in GitHub Desktop.
Save TheExpertNoob/06358fba4de0c7346b912ae48771fdb3 to your computer and use it in GitHub Desktop.

Random backgrounds in Tinfoil theme:

Call the png in Tinfoil theme settings.json:

https://yourdomain.com/images/halloween.png

Create a .htaccess file on the hosting server inside a folder called "images":

RewriteEngine On  
RewriteRule ^halloween\.png$ halloween.php [L]

Inside that same "images" folder, create a folder named "halloween" and a text file named "halloween.php"
Dump all your png images into that halloween folder.
Edit the "halloween.php" file to include:

<?php
// Define the directory where your PNG images are stored
$imageDirectory = 'halloween/';

// Get a list of all PNG files in the directory
$pngFiles = glob($imageDirectory . '*.png');

// Check if there are any PNG files in the directory
if (count($pngFiles) > 0) {
    // Select a random PNG file from the list
    $randomImage = $pngFiles[array_rand($pngFiles)];

    // Set the appropriate Content-Type header
    header('Content-Type: image/png');

    // Output the selected image
    readfile($randomImage);
} else {
    // If no PNG files are found, display an error message
    echo 'No PNG images found in the directory.';
}
?>

Restart the server to pick up the new htaccess file (may or may not be necessary).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment