1
<?php declare(strict_types=1);
3
use Chandler\Database\DatabaseConnection;
4
use openvk\Web\Models\Repositories\Photos;
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Nette\Utils\ImageException;
11
class RebuildImagesCommand extends Command
15
protected static $defaultName = "build-images";
17
function __construct()
19
$this->images = DatabaseConnection::i()->getContext()->table("photos");
21
parent::__construct();
24
protected function configure(): void
26
$this->setDescription("Create resized versions of images")
27
->setHelp("This command allows you to resize all your images after configuration change")
28
->addOption("upgrade-only", "U", InputOption::VALUE_NEGATABLE, "Only upgrade images which aren't resized?");
31
protected function execute(InputInterface $input, OutputInterface $output): int
33
$header = $output->section();
34
$counter = $output->section();
37
"Image Rebuild Utility",
38
"=====================",
42
$filter = ["deleted" => false];
43
if($input->getOption("upgrade-only"))
44
$filter["sizes"] = NULL;
46
$selection = $this->images->select("id")->where($filter);
47
$totalPics = $selection->count();
49
"Total of $totalPics images found.",
56
$begin = new \DateTimeImmutable("now");
57
foreach($selection as $idHolder) {
58
$start = microtime(true);
61
$photo = (new Photos)->get($idHolder->id);
62
$photo->getSizes(true, true);
63
$photo->getDimensions();
64
} catch(ImageException $ex) {
68
$timeConsumed = microtime(true) - $start;
70
$avgTime = $timeConsumed;
72
$avgTime = ($avgTime + $timeConsumed) / 2;
74
$eta = $begin->getTimestamp() + ceil($totalPics * $avgTime);
75
$int = (new \DateTimeImmutable("now"))->diff(new \DateTimeImmutable("@$eta"));
76
$int = $int->d . "d" . $int->h . "h" . $int->i . "m" . $int->s . "s";
77
$pct = floor(100 * ($count / $totalPics));
79
$counter->overwrite("Processed " . ++$count . " images... ($pct% $int left $errors/$count fail)");
82
$counter->overwrite("Processing finished :3");
84
return Command::SUCCESS;