Created
August 20, 2017 19:53
-
-
Save pwaldhauer/eb65d6cb02b0b55d74db9882702e06c1 to your computer and use it in GitHub Desktop.
Kirby Thumbnail Generator on steroids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('DS', DIRECTORY_SEPARATOR); | |
require(__DIR__ . DS . 'kirby' . DS . 'bootstrap.php'); | |
class ThumbThumbMock extends Thumb | |
{ | |
public function __construct($source, array $params = []) | |
{ | |
$this->source = $this->result = is_a($source, 'Media') ? $source : new Media($source); | |
$this->options = array_merge(static::$defaults, $this->params($params)); | |
} | |
} | |
class ThumbMock extends \Kirby\Component\Thumb | |
{ | |
private $jobs = []; | |
public function configure() | |
{ | |
parent::configure(); | |
} | |
public function create($file, $params) | |
{ | |
$this->jobs[] = [ | |
'file' => $file->root(), | |
'dst' => (new ThumbThumbMock($file, $params))->destination()->root(), | |
'params' => $params, | |
]; | |
return $file; | |
} | |
public function getJobs() | |
{ | |
return $this->jobs; | |
} | |
} | |
$kirby = kirby(); | |
$site = $kirby->site(); | |
$kirby->extensions(); | |
$kirby->models(); | |
$kirby->plugins(); | |
$kirby->component('thumb', 'ThumbMock'); | |
if (!isset($argv[1])) { | |
die('Usage: php images.php [path-to-page]' . PHP_EOL); | |
} | |
$page = page($argv[1]); | |
if (empty($page)) { | |
die('Page not found: ' . $argv[1] . PHP_EOL); | |
} | |
$kirby->render($page); | |
file_put_contents('image_jobs.json', json_encode($kirby->component('thumb')->getJobs(), JSON_PRETTY_PRINT)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* npm install sharp mkdirp progress | |
*/ | |
const sharp = require('sharp'); | |
const mkdirp = require('mkdirp'); | |
const path = require('path'); | |
const ProgressBar = require('progress'); | |
const jobs = require('./image_jobs.json'); | |
const bar = new ProgressBar('Thumbnailing: [:bar] :rate/tps :percent :etas', {total: jobs.length}); | |
jobs.forEach(function (e) { | |
mkdirp.sync(path.dirname(e.dst)); | |
const t = | |
sharp(e.file) | |
.resize(parseInt(e.params.width, 10), null); | |
if (e.params.blurred) { | |
t.blur(30); | |
} | |
t | |
.jpeg({quality: e.params.quality, chromaSubsampling: '4:4:4'}) | |
.toFile(e.dst) | |
.then(function () { | |
bar.tick(); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment