Files
PhotoGal/web/scripts/process_images.php
2024-08-27 19:03:24 -05:00

85 lines
2.9 KiB
PHP

<?php
// web/process_images.php
require_once 'vendor/autoload.php';
use Hpz\Photogal\Database;
use Hpz\Photogal\Image;
use Hpz\Photogal\ThumbnailGenerator;
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');
}
// Specify the paths
// Specify the paths
$albumsDir = 'public/images/';
$dbFilePath = $_ENV['DB_FILE_PATH'];
// Create a new Database connection
$database = new Database($dbFilePath);
$image = new Image($database);
// Function to process a directory
function processDirectory($albumDir, $albumName, Image $image)
{
$dirIterator = new RecursiveDirectoryIterator($albumDir);
$iterator = new RecursiveIteratorIterator($dirIterator);
foreach ($iterator as $file) {
if ($file->isFile()) {
$filePath = $file->getPathname();
$fileInfo = pathinfo($filePath);
$fileName = $fileInfo['basename'];
if (in_array(strtolower($fileInfo['extension']), ['jpg', 'jpeg', 'png', 'gif'])) {
$thumbDir = 'public/images/' . $albumName . '/thumbs/';
$bigDir = 'public/images/' . $albumName . '/big/';
$thumbPath = $thumbDir . $fileInfo['filename'] . '.webp';
$bigPath = $bigDir . $fileInfo['filename'] . '.webp';
if (!file_exists($thumbPath)) {
if (!is_dir($thumbDir)) {
mkdir($thumbDir, 0777, true);
}
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);
}
ThumbnailGenerator::createBigImage($filePath, $bigPath);
echo "Created big image for $fileName in $albumName\n";
}
$relativePath = $albumName . '/photos';
$modifiedDate = filemtime($filePath);
$addedDate = time();
$title = $fileInfo['filename'];
$exists = $image->imageExists($albumName, $fileName, $relativePath);
if (!$exists) {
$image->addImage($albumName, $fileName, $relativePath, $modifiedDate, $addedDate, $title);
echo "Added $fileName to database in album $albumName\n";
}
}
}
}
}
$albums = scandir($albumsDir);
foreach ($albums as $album) {
if ($album !== '.' && $album !== '..' && is_dir($albumsDir . $album)) {
if (strpos($album, 'videos') === false) {
processDirectory($albumsDir . $album . '/photos/', $album, $image);
}
}
}