add routing, move code to classes; misc changes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
// web/process_images.php
|
||||
// scripts/process_images.php
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
@@ -11,11 +11,10 @@ ini_set('memory_limit', '512M');
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
if (!isset($_ENV['DB_FILE_PATH'])) {
|
||||
die('DB_FILE_PATH is not set in the .env file');
|
||||
if (!isset($_ENV['DB_FILE_PATH']) || !isset($_ENV['IMG_THUMB_WIDTH']) || !isset($_ENV['IMG_THUMB_HEIGHT']) || !isset($_ENV['IMG_THUMB_QUALITY'])) {
|
||||
die('Required environment variables are not set in the .env file');
|
||||
}
|
||||
|
||||
// Specify the paths
|
||||
// Specify the paths
|
||||
$albumsDir = 'public/images/';
|
||||
$dbFilePath = $_ENV['DB_FILE_PATH'];
|
||||
@@ -24,10 +23,32 @@ $dbFilePath = $_ENV['DB_FILE_PATH'];
|
||||
$database = new Database($dbFilePath);
|
||||
$image = new Image($database);
|
||||
|
||||
// Function to check for albums in the database and remove those that don't exist in the images folder
|
||||
function cleanUpAlbums(Database $database, $albumsDir)
|
||||
{
|
||||
$db = $database->getConnection();
|
||||
$albumsQuery = "SELECT DISTINCT album FROM images";
|
||||
$albumsResult = $db->query($albumsQuery);
|
||||
|
||||
while ($row = $albumsResult->fetchArray(SQLITE3_ASSOC)) {
|
||||
$album = $row['album'];
|
||||
$albumPath = $albumsDir . $album;
|
||||
|
||||
if (!is_dir($albumPath)) {
|
||||
// Remove all images related to this album from the database
|
||||
$deleteQuery = $db->prepare("DELETE FROM images WHERE album = :album");
|
||||
$deleteQuery->bindValue(':album', $album, SQLITE3_TEXT);
|
||||
$deleteQuery->execute();
|
||||
|
||||
echo "Removed album '$album' from database as it does not exist in the images folder\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function to process a directory
|
||||
function processDirectory($albumDir, $albumName, Image $image)
|
||||
{
|
||||
$dirIterator = new RecursiveDirectoryIterator($albumDir);
|
||||
$dirIterator = new RecursiveDirectoryIterator($albumDir, RecursiveDirectoryIterator::SKIP_DOTS);
|
||||
$iterator = new RecursiveIteratorIterator($dirIterator);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
@@ -43,16 +64,18 @@ function processDirectory($albumDir, $albumName, Image $image)
|
||||
$bigPath = $bigDir . $fileInfo['filename'] . '.webp';
|
||||
|
||||
if (!file_exists($thumbPath)) {
|
||||
if (!is_dir($thumbDir)) {
|
||||
mkdir($thumbDir, 0777, true);
|
||||
if (!is_dir($thumbDir) && !mkdir($thumbDir, 0777, true) && !is_dir($thumbDir)) {
|
||||
error_log("Failed to create directory: $thumbDir");
|
||||
continue;
|
||||
}
|
||||
ThumbnailGenerator::createThumbnail($filePath, $thumbPath, $_ENV['IMG_THUMB_WIDTH'], $_ENV['IMG_THUMB_HEIGHT'], $_ENV['IMG_THUMB_QUALITY']);
|
||||
echo "Created thumbnail for $fileName in $albumName\n";
|
||||
}
|
||||
|
||||
if (!file_exists($bigPath)) {
|
||||
if (!is_dir($bigDir)) {
|
||||
mkdir($bigDir, 0777, true);
|
||||
if (!is_dir($bigDir) && !mkdir($bigDir, 0777, true) && !is_dir($bigDir)) {
|
||||
error_log("Failed to create directory: $bigDir");
|
||||
continue;
|
||||
}
|
||||
ThumbnailGenerator::createBigImage($filePath, $bigPath);
|
||||
echo "Created big image for $fileName in $albumName\n";
|
||||
@@ -74,6 +97,10 @@ function processDirectory($albumDir, $albumName, Image $image)
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up albums that no longer exist in the images directory
|
||||
cleanUpAlbums($database, $albumsDir);
|
||||
|
||||
// Process existing albums and images
|
||||
$albums = scandir($albumsDir);
|
||||
foreach ($albums as $album) {
|
||||
if ($album !== '.' && $album !== '..' && is_dir($albumsDir . $album)) {
|
||||
|
||||
Reference in New Issue
Block a user