First commit with actual files.

main
Nicolas Ong 2023-09-09 22:07:49 +02:00
parent 84acf37f18
commit 82e74fe9f7
8 changed files with 205 additions and 0 deletions

16
config.php Normal file
View File

@ -0,0 +1,16 @@
<?php
define("PORTFOLIO_TITLE", "portfolio title");
define("PORTFOLIO_URL", "portfolio URL");
define("PORTFOLIO_LANGUAGE", "country code");
define("PORTFOLIO_CREATION_YEAR", 2023);
define("AUTHOR_NICKNAME", "portfolio author's nickname");
define("AUTHOR_REALNAME", "portfolio author's realname - optional, leave blank if none");
$links = [
"link id" => [
"label" => "label",
"url" => "https://example.com",
"self" => false
]
];

3
css/portfolio.css Normal file
View File

@ -0,0 +1,3 @@
@charset "utf-8";
* { box-sizing: border-box; }

10
index.php Normal file
View File

@ -0,0 +1,10 @@
<?php
spl_autoload_register(
function ($class_name) {
require_once "lib/$class_name.php";
}
);
$portfolio = new Portfolio();
$portfolio->run();

17
lib/Gallery.php Normal file
View File

@ -0,0 +1,17 @@
<?php
final class Gallery
{
public string $label;
public array $images = [];
public function __construct(string $label)
{
$this->label = $label;
}
public function add(Image $image)
{
$this->images[] = $image;
}
}

21
lib/Image.php Normal file
View File

@ -0,0 +1,21 @@
<?php
final class Image
{
public string $full;
public string $thumbnail;
public string $title;
public string $alt;
public int $width;
public int $height;
public function __construct(string $full, string $thumbnail, string $title, string $alt, int $width, int $height)
{
$this->full = $full;
$this->thumbnail = $thumbnail;
$this->title = $title;
$this->alt = $alt;
$this->width = $width;
$this->height = $height;
}
}

17
lib/Link.php Normal file
View File

@ -0,0 +1,17 @@
<?php
final class Link
{
public string $id;
public string $label;
public string $url;
public bool $self;
public function __construct(string $id, string $label, string $url, bool $self)
{
$this->id = $id;
$this->label = $label;
$this->url = $url;
$this->self = $self;
}
}

81
lib/Portfolio.php Normal file
View File

@ -0,0 +1,81 @@
<?php
final class Portfolio
{
private array $links = [];
public function __construct()
{
$this->loadConfig();
}
public function run()
{
$this->render();
}
private function loadConfig()
{
require_once "config.php";
foreach ($links as $id => $link) {
$this->links[] = new Link($id, $link["label"], $link["url"], $link["self"]);
}
}
private function render()
{
$title = PORTFOLIO_TITLE;
$url = PORTFOLIO_URL;
$galleries = $this->loadGalleries();
$links = $this->links;
$footer = PORTFOLIO_CREATION_YEAR .
". " . AUTHOR_NICKNAME .
(AUTHOR_REALNAME !== "" ? " (" . AUTHOR_REALNAME . ")" : "");
include "views/index.phtml";
}
private function loadGalleries(): array
{
$galleries = [];
foreach (array_diff(scandir("./galleries"), [".", ".."]) as $dir) {
$path = "./galleries/$dir";
$meta = "$gallery_path/metadata.txt";
if (!is_file($meta)) {
continue;
}
$galleries[] = $this->loadGallery($path, trim(file_get_contents($meta)));
}
return $galleries;
}
private function loadGallery(string $dir, string $title): Gallery
{
$gallery = new Gallery($title);
foreach (array_diff(scandir($dir, SCANDIR_SORT_DESCENDING), [".", ".."]) as $file) {
$path = "$dir/$file";
$meta = "$path.meta.txt";
if (!is_file($meta)) {
continue;
}
$gallery->add($this->loadImage($dir, $file, $meta));
}
return $gallery;
}
private function loadImage(string $dir, string $file, string $meta): Image
{
$path = "$dir/" . rawurlencode($file);
$thumbnail = str_replace(".jpg", "_thb.jpg", $image_path);
list($title, $alt) = file($image_meta);
list($width, $height) = getimagesize("$dir/$file");
return new Image(
$path,
$thumbnail,
trim($title),
trim($alt),
(int) $width,
(int) $height
);
}
}

40
views/index.phtml Normal file
View File

@ -0,0 +1,40 @@
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title><?=$title?></title>
<link href="css/portfolio.css" rel="stylesheet" title="Portfolio" type="text/css">
</head>
<body>
<header>
<h1><a href="<?=$url?>"><?=$title?></a></h1>
</header>
<main>
<?php foreach ($galleries as $gallery): ?>
<h2><?=$gallery->label?></h2>
<ul class="gallery">
<?php foreach ($gallery->images as $image): ?>
<li>
<a class="thumbnail" href="<?=$image->full?>">
<figure>
<img src="<?=$image->thumbnail?>" alt="<?=$image->alt?>" data-width="<?=$image->width?>" data-height="<?=$image->height?>">
<figcaption><?=$image->title?></figcaption>
</figure>
</a>
</li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
</main>
<aside>
<ul class="links">
<?php foreach ($links as $link): ?>
<li class="icon">
<a class="icon <?=$link->id?>" href="<?=$link->url?>"<?=$link->self ? " rel=\"me\"" : ""?>><?=$link->label?></a>
</li>
<?php endforeach ?>
</ul>
</aside>
<footer><p><?=$footer?></p></footer>
</body>
</html>