Skip to content

Instantly share code, notes, and snippets.

@lukaskleinschmidt
Created March 2, 2020 09:17
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskleinschmidt/40014aad9884b449a2f183e53f147c3a to your computer and use it in GitHub Desktop.
Save lukaskleinschmidt/40014aad9884b449a2f183e53f147c3a to your computer and use it in GitHub Desktop.
Kirby Thumbnails
<?php
// bootstrap/kirby.php
include dirname(__DIR__) . '/vendor/autoload.php';
$kirby = new Kirby([
'roots' => [
'base' => $base = dirname(__DIR__),
'index' => $base . '/public',
'content' => $base . '/content',
'site' => $base . '/site',
'storage' => $storage = $base . '/storage',
'accounts' => $storage . '/accounts',
'cache' => $storage . '/cache',
'sessions' => $storage . '/sessions',
]
]);
return $kirby;
<?php
// public/index.php
$kirby = require dirname(__DIR__) . '/bootstrap/kirby.php';
echo $kirby->render();
<?php
// tools/thumbs.php
$kirby = require_once dirname(__DIR__) . '/bootstrap/kirby.php';
set_time_limit(0);
echo "\n";
echo "Generating thumbnail jobs";
echo "\n";
foreach ($kirby->site()->index() as $page) {
$page->render();
// Output to terminal
echo "\033[36m";
echo $page->id();
echo "\033[0m";
echo "\n";
}
echo "\n";
echo "Generating thumbnails";
echo "\n";
$generated = [];
foreach (Kirby\Toolkit\Dir::index($kirby->roots()->index() . '/media', true) as $path) {
if (! Str::endsWith($path, '.json')) {
continue;
}
$parts = explode('/', $path);
$count = count($parts);
$type = $parts[0];
$hash = $parts[$count - 3];
$path = implode('/', array_splice($parts, 1, $count - 4));
$filename = str_replace('.json', '', array_pop($parts));
// Output to terminal
echo "\033[36m";
echo $path . '/' . $filename;
echo "\033[0m";
echo "\n";
$generated[] = $path . '/' . $filename;
if ($type === 'assets') {
Kirby\Cms\Media::thumb($path, $hash, $filename);
continue;
}
switch ($type) {
case 'pages':
$model = $kirby->page($path);
break;
case 'site':
$model = $kirby->site();
break;
case 'user':
$model = $kirby->user($path);
break;
}
Kirby\Cms\Media::link($model, $hash, $filename);
}
if (! count($generated)) {
echo "\033[36m";
echo "Nothing to generate";
echo "\033[0m";
echo "\n";
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment