much better
This commit is contained in:
62
generate.php
62
generate.php
@@ -1,24 +1,63 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// todo: look for images in a directory
|
const SIZE = 256;
|
||||||
// for each image, see if a thumbnail exists in a thumbnail subdirectory
|
|
||||||
|
|
||||||
$photos = [];
|
$photos = [];
|
||||||
|
|
||||||
foreach (scandir('./images') as $inode) {
|
foreach (scandir('./images') as $inode) {
|
||||||
var_dump($inode);
|
echo "Scanning file: $inode\n";
|
||||||
|
|
||||||
$file_name = basename($inode);
|
if ( ! preg_match('/\.(gif|jpe?g|png|webp)$/i', $inode)) {
|
||||||
|
continue;
|
||||||
if ( ! file_exists("./images/thumbnails/$file_name")) {
|
|
||||||
// todo: make thumbnail - needs imagick/gd or something
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// maybe each photo can have a text file associated with it, for a description / alt text / etc?
|
$file_name = basename($inode);
|
||||||
// can this be stored in jpeg metadata?
|
$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
|
// 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(
|
$photos[] = sprintf(
|
||||||
'
|
'
|
||||||
<div class="photo">
|
<div class="photo">
|
||||||
@@ -28,7 +67,8 @@ foreach (scandir('./images') as $inode) {
|
|||||||
</div>
|
</div>
|
||||||
',
|
',
|
||||||
'todo: full-size image or photo page',
|
'todo: full-size image or photo page',
|
||||||
'todo: thumbnail filename',
|
$thumbnail_path,
|
||||||
|
$alt_text ?: ''
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +76,6 @@ file_put_contents(
|
|||||||
'index.html',
|
'index.html',
|
||||||
sprintf(
|
sprintf(
|
||||||
file_get_contents('template.html'),
|
file_get_contents('template.html'),
|
||||||
'Whee'
|
implode('', $photos)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,6 +3,13 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Photos</title>
|
<title>Photos</title>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
main {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user