Generate preview dynamically

This commit is contained in:
Shuqi 2024-11-15 17:55:31 +08:00
parent d2fd8f7d8d
commit 269cc735a4
3 changed files with 62 additions and 10 deletions

49
php/generate-preview.php Normal file
View File

@ -0,0 +1,49 @@
<?php
function generate_preview ($icon_path, $preview_path) {
$image = new Imagick();
$image->setBackgroundColor(new ImagickPixel('#ffffff'));
$image->readImageBlob(file_get_contents($icon_path));
$image->setImageFormat("png24");
$image->setImageInterpolateMethod(Imagick::INTERPOLATE_INTEGER);
$image->resizeImage(180, 180, Imagick::FILTER_POINT, 1);
$image->borderImage('#ffffff', 510, 225);
$image->writeImage($preview_path);
}
$current_icon = false;
if (isset($argv[1])) {
$current_icon = $argv[1];
} elseif (isset($_GET['icon'])) {
$current_icon = $_GET['icon'];
}
$icon_dir = __DIR__."/icons";
$preview_dir = __DIR__."/preview";
if ($current_icon) {
$icon_path = "$icon_dir/$current_icon.svg";
$preview_path = "$preview_dir/$current_icon.png";
generate_preview($icon_path, $preview_path);
} else {
// no icon specified, generate all
$icon_json = json_decode(file_get_contents(__DIR__ . "/assets/icons.json"), true);
foreach ($icon_json as $filename => $tags) {
$icon_name = str_replace(".svg", "", $filename);
$icon_path = "$icon_dir/$filename";
$preview_path = "$preview_dir/$icon_name.png";
if (is_file($preview_path) && filemtime($preview_path) > filemtime($icon_path)) {
continue;
}
generate_preview($icon_path, $preview_path);
}
$file = fopen(__DIR__ . "/last_generate", "w");
fwrite($file, time());
fclose($file);
}

View File

@ -17,15 +17,8 @@ if (!is_file($icon_path)) {
}
// generate facebook preview
if (!is_file($preview_path)) {
$image = new Imagick();
$image->setBackgroundColor(new ImagickPixel('#ffffff'));
$image->readImageBlob(file_get_contents($icon_path));
$image->setImageFormat("png24");
$image->setImageInterpolateMethod(Imagick::INTERPOLATE_INTEGER);
$image->resizeImage(180, 180, Imagick::FILTER_POINT, 1);
$image->borderImage('#ffffff', 510, 225);
$image->writeImage($preview_path);
if (!is_file($preview_path) || filemtime($preview_path) < filemtime($icon_path)) {
require_once(__DIR__ . "/generate-preview.php");
$preview_mtime = time();
} else {
$preview_mtime = filemtime($preview_path);

View File

@ -102,4 +102,14 @@ $files = array_map(function ($file) {
})()
</script>
</body>
</html>
</html>
<?php
$last_generate = 0;
if (is_file(__DIR__ . "/last_generate")) {
$last_generate = file_get_contents(__DIR__ . "/last_generate");
}
if ($last_generate < filemtime(__DIR__ . "/assets/icons.json")) {
shell_exec('php "' . __DIR__ . '/generate-preview.php" > /dev/null 2>&1 &');
}