Educational Guide on PHP Code and Operation
# gallery :
Around 1996, I built my first website using a photo gallery system. Back then, everything was based on static HTML code.
Internet connections were also incredibly slow. I had to come up with various solutions to make photos load faster.
That's how thumbnails came into use: smaller versions of images that you could click on to view the original, full-sized photo.
Maintaining the website was quite time-consuming in those days. Even a small change, such as updating bird names, meant manually editing the HTML pages.
Not long after that, I discovered the possibilities of PHP. I started reading more and more about it and became fascinated by what it could do. PHP is a powerful scripting language that allows you to execute all kinds of commands on the server.
Before long, I was completely hooked on PHP programming!
It made things so much easier and saved an enormous amount of manual work. Original images could now be automatically resized to the appropriate thumbnail dimensions.
I could also automatically sort photos by date and display an overview of the 10 latest photos.
Even my blogging system is entirely written from scratch in PHP.
What started as a simple photo gallery gradually evolved into a website where my passion for photography, nature and programming come together.
I'd also like to share my source code here and show how everything works, how the system is built, and which technologies are behind it.
Schema System Synchronization File: SSSF
// Album (orginal foto's) and Thumbs
// my files is album = fotos and thumbs = album.
Map Album
|-> Dieren [Animalia]
|-> Vogels [Aves]
-> Insecten [Insecta]
Map Thumbs
|-> Dieren [Animalia]
|-> Vogels [Aves]
-> Insecten [Insecta]
Schema System Synchronization File (SSSF)
What is an SSSF?
SSSF is a system that synchronizes image files from an original directory and automatically resizes them before placing the thumbnails into a separate directory.
It supports image formats such as .jpg, .jpeg, .png and .webp.
As illustrated in the directory structure below, the original folders are mirrored in the Thumbs directory:
Map Album
|-> Dieren [Animalia]
|-> Vogels [Aves]
-> Insecten [Insecta]
Map Thumbs
|-> Dieren [Animalia]
|-> Vogels [Aves]
-> Insecten [Insecta]
A small PHP script running in the admin panel scans the original directories and checks whether a resized thumbnail already exists in the corresponding Thumbs directory.
If a thumbnail does not yet exist, the script automatically creates it and saves it in the appropriate directory.
The Source Code of the SSSF Generator
<?php
// Belangrijk: Geef het script genoeg tijd en geheugen voor 2606 foto's
set_time_limit(1200); // 20 minuten
ini_set('memory_limit', '512M');
$root = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/';
$bron_pad = $root . "fotos/"; // originele foto's
$doel_pad = $root . "album/"; // thumbs foto's
echo "<h1>Vogel Thumbnails Generator</h1>";
echo "Bron: " . htmlspecialchars($bron_pad, ENT_QUOTES, 'UTF-8') . "<br>";
echo "Doel: " . htmlspecialchars($doel_pad, ENT_QUOTES, 'UTF-8') . "<hr>";
if (!is_dir($bron_pad)) {
die("FOUT: Bronmap niet gevonden!");
}
function verwerkMappen(string $dir, string $bron_root, string $doel_root): int {
$items = array_diff(scandir($dir), ['.', '..']);
$aantal = 0;
foreach ($items as $item) {
$pad = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($pad)) {
$nieuwe_map = str_replace($bron_root, $doel_root, $pad);
if (!is_dir($nieuwe_map)) {
mkdir($nieuwe_map, 0755, true);
echo "<strong>Nieuwe map:</strong> " . htmlspecialchars(basename($pad), ENT_QUOTES, 'UTF-8') . "<br>";
}
$aantal += verwerkMappen($pad, $bron_root, $doel_root);
} else {
if (preg_match('/\.(jpe?g|png|webp)$/i', $item)) {
$doel_file = str_replace($bron_root, $doel_root, $pad);
if (!file_exists($doel_file)) {
if (maakWebpThumb($pad, $doel_file, 200)) {
echo "Gemaakt: " . htmlspecialchars($item, ENT_QUOTES, 'UTF-8') . "<br>";
$aantal++;
}
}
}
}
}
return $aantal;
}
preg_match('/\.webp$/i', $item)
I have configured it to use only the .webp format because it is one of the most lightweight image formats available.
function maakWebpThumb($bron, $doel, $max_breedte) {
// Laad de WebP
$img = @imagecreatefromwebp($bron);
if (!$img) return false;
$orig_w = imagesx($img);
$orig_h = imagesy($img);
// Bereken nieuwe breedte en hoogte
// We zetten de breedte vast op 200px, de hoogte volgt de verhouding
$w = $max_breedte;
$h = floor($orig_h * ($max_breedte / $orig_w));
$tmp_img = imagecreatetruecolor($w, $h);
// Behoud transparantie (voor het geval dat)
imagealphablending($tmp_img, false);
imagesavealpha($tmp_img, true);
// Schalen
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $w, $h, $orig_w, $orig_h);
// Opslaan als WebP
$succes = imagewebp($tmp_img, $doel, 85);
imagedestroy($img);
imagedestroy($tmp_img);
return $succes;
}
My standard thumbnail width is 200 pixels. The height is automatically adjusted to maintain the original aspect ratio.