openvk

Форк
0
/
RebuildImagesCommand.php 
86 строк · 2.7 Кб
1
<?php declare(strict_types=1);
2
namespace openvk\CLI;
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;
10

11
class RebuildImagesCommand extends Command
12
{
13
    private $images;
14

15
    protected static $defaultName = "build-images";
16

17
    function __construct()
18
    {
19
        $this->images = DatabaseConnection::i()->getContext()->table("photos");
20

21
        parent::__construct();
22
    }
23

24
    protected function configure(): void
25
    {
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?");
29
    }
30

31
    protected function execute(InputInterface $input, OutputInterface $output): int
32
    {
33
        $header  = $output->section();
34
        $counter = $output->section();
35

36
        $header->writeln([
37
            "Image Rebuild Utility",
38
            "=====================",
39
            "",
40
        ]);
41

42
        $filter = ["deleted" => false];
43
        if($input->getOption("upgrade-only"))
44
            $filter["sizes"] = NULL;
45

46
        $selection = $this->images->select("id")->where($filter);
47
        $totalPics = $selection->count();
48
        $header->writeln([
49
            "Total of $totalPics images found.",
50
            "",
51
        ]);
52

53
        $errors  = 0;
54
        $count   = 0;
55
        $avgTime = NULL;
56
        $begin   = new \DateTimeImmutable("now");
57
        foreach($selection as $idHolder) {
58
            $start = microtime(true);
59

60
            try {
61
                $photo = (new Photos)->get($idHolder->id);
62
                $photo->getSizes(true, true);
63
                $photo->getDimensions();
64
            } catch(ImageException $ex) {
65
                $errors++;
66
            }
67

68
            $timeConsumed = microtime(true) - $start;
69
            if(!$avgTime)
70
                $avgTime = $timeConsumed;
71
            else
72
                $avgTime = ($avgTime + $timeConsumed) / 2;
73

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));
78

79
            $counter->overwrite("Processed " . ++$count . " images... ($pct% $int left $errors/$count fail)");
80
        }
81

82
        $counter->overwrite("Processing finished :3");
83

84
        return Command::SUCCESS;
85
    }
86
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.