much better

This commit is contained in:
max
2026-08-11 17:06:51 +01:00
parent fd2d3b1d9a
commit 60eb1064c8
2 changed files with 58 additions and 11 deletions

View File

@@ -1,24 +1,63 @@
<?php
// todo: look for images in a directory
// for each image, see if a thumbnail exists in a thumbnail subdirectory
const SIZE = 256;
$photos = [];
foreach (scandir('./images') as $inode) {
var_dump($inode);
echo "Scanning file: $inode\n";
$file_name = basename($inode);
if ( ! file_exists("./images/thumbnails/$file_name")) {
// todo: make thumbnail - needs imagick/gd or something
if ( ! preg_match('/\.(gif|jpe?g|png|webp)$/i', $inode)) {
continue;
}
// maybe each photo can have a text file associated with it, for a description / alt text / etc?
// can this be stored in jpeg metadata?
$file_name = basename($inode);
$thumbnail_path = "./images/thumbnails/$file_name";
if ( ! file_exists($thumbnail_path)) {
$image_bytes = file_get_contents("./images/$file_name");
if ( ! $image_bytes) {
echo "Warning: couldn't get contents of file $file_name\n";
continue;
}
$image = imagecreatefromstring($image_bytes);
if ( ! $image) {
echo "Warning: couldn't parse image $file_name\n";
continue;
}
echo "Creating thumbnail: $thumbnail_path\n";
$source_width = imagesx($image);
$source_height = imagesy($image);
$dest_width = ($source_width / $source_height) * SIZE;
$thumbnail = imagecreate($dest_width, SIZE);
imagecopyresampled(
$thumbnail,
$image,
0, 0,
0,0,
$dest_width, SIZE,
$source_width, $source_height
);
imagejpeg($thumbnail, $thumbnail_path);
}
// could generate a "view photo on its own" kinda HTML page with a mid-size version of the photo
// if file is "blah.jpg", look for "blah.jpg.txt" and use this as alt text
$alt_text = file_get_contents("./images/$file_name.txt");
if ( ! $alt_text) {
echo "Warning: no alt text for file $file_name\n";
}
$photos[] = sprintf(
'
<div class="photo">
@@ -28,7 +67,8 @@ foreach (scandir('./images') as $inode) {
</div>
',
'todo: full-size image or photo page',
'todo: thumbnail filename',
$thumbnail_path,
$alt_text ?: ''
);
}
@@ -36,6 +76,6 @@ file_put_contents(
'index.html',
sprintf(
file_get_contents('template.html'),
'Whee'
implode('', $photos)
)
);