83 lines
2.1 KiB
PHP
83 lines
2.1 KiB
PHP
<?php
|
|
|
|
const SIZE = 256;
|
|
|
|
$photos = [];
|
|
|
|
foreach (scandir('./images') as $inode) {
|
|
echo "Scanning file: $inode\n";
|
|
|
|
if ( ! preg_match('/\.(gif|jpe?g|png|webp)$/i', $inode)) {
|
|
continue;
|
|
}
|
|
|
|
$file_name = basename($inode);
|
|
$image_path = "./images/$file_name";
|
|
$thumbnail_path = "./images/thumbnails/$file_name";
|
|
|
|
if ( ! file_exists($thumbnail_path)) {
|
|
$image_bytes = file_get_contents($image_path);
|
|
|
|
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">
|
|
<a href="%s">
|
|
<img src="%s" alt="%s">
|
|
</a>
|
|
</div>
|
|
',
|
|
$image_path,
|
|
$thumbnail_path,
|
|
htmlentities($alt_text ?: '')
|
|
);
|
|
}
|
|
|
|
file_put_contents(
|
|
'index.html',
|
|
sprintf(
|
|
file_get_contents('template.html'),
|
|
implode('', $photos)
|
|
)
|
|
);
|