Files
static-photos-page/generate.php

83 lines
2.0 KiB
PHP
Raw Normal View History

2026-06-18 17:06:52 +01:00
<?php
2026-08-11 17:06:51 +01:00
const SIZE = 256;
2026-06-18 17:06:52 +01:00
$photos = [];
foreach (scandir('./images') as $inode) {
2026-08-11 17:06:51 +01:00
echo "Scanning file: $inode\n";
if ( ! preg_match('/\.(gif|jpe?g|png|webp)$/i', $inode)) {
continue;
}
2026-06-18 17:06:52 +01:00
2026-08-11 17:11:30 +01:00
$file_name = basename($inode);
$image_path = "./images/$file_name";
2026-08-11 17:06:51 +01:00
$thumbnail_path = "./images/thumbnails/$file_name";
2026-06-18 17:06:52 +01:00
2026-08-11 17:06:51 +01:00
if ( ! file_exists($thumbnail_path)) {
2026-08-11 17:11:30 +01:00
$image_bytes = file_get_contents($image_path);
2026-08-11 17:06:51 +01:00
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);
2026-06-18 17:06:52 +01:00
}
// could generate a "view photo on its own" kinda HTML page with a mid-size version of the photo
2026-08-11 17:06:51 +01:00
// 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";
}
2026-06-18 17:06:52 +01:00
$photos[] = sprintf(
'
<div class="photo">
<a href="%s">
<img src="%s" alt="%s">
</a>
</div>
',
2026-08-11 17:11:30 +01:00
$image_path,
2026-08-11 17:06:51 +01:00
$thumbnail_path,
$alt_text ?: ''
2026-06-18 17:06:52 +01:00
);
}
file_put_contents(
'index.html',
sprintf(
file_get_contents('template.html'),
2026-08-11 17:06:51 +01:00
implode('', $photos)
2026-06-18 17:06:52 +01:00
)
);