|
17 | 17 |
|
18 | 18 | class WebPConversionCommand extends Command |
19 | 19 | { |
20 | | - protected static $defaultName = 'codebuds:webp:convert'; |
21 | | - |
22 | | - protected const CONVERTABLE_MIME_TYPES = [ |
23 | | - 'image/jpg', |
24 | | - 'image/jpeg', |
25 | | - 'image/png', |
26 | | - 'image/gif', |
27 | | - ]; |
28 | | - |
29 | | - public function __construct(private int $quality, private string $projectDir) |
30 | | - { |
31 | | - parent::__construct(); |
32 | | - } |
33 | | - |
34 | | - protected function configure(): void |
35 | | - { |
36 | | - $this |
37 | | - ->setDescription('Generate webps in directories') |
38 | | - ->addArgument('directories', InputArgument::IS_ARRAY, 'Directories for webP generation') |
39 | | - ->addOption('create', null, InputOption::VALUE_NONE, 'Generate new files') |
40 | | - ->addOption('force', null, InputOption::VALUE_NONE, 'Recreate all the files') |
41 | | - ->addOption('quality', null, InputOption::VALUE_OPTIONAL, 'Set webp generation quality', $this->quality) |
42 | | - ->addOption('suffix', null, InputOption::VALUE_OPTIONAL, 'Add a filename suffix', ''); |
43 | | - } |
44 | | - |
45 | | - protected function execute(InputInterface $input, OutputInterface $output): int |
46 | | - { |
47 | | - $io = new SymfonyStyle($input, $output); |
48 | | - $directories = $input->getArgument('directories'); |
49 | | - |
50 | | - if (!$directories) { |
51 | | - $io->error('no directories passed'); |
52 | | - |
53 | | - return 1; |
54 | | - } |
55 | | - |
56 | | - $stopwatch = new Stopwatch(); |
57 | | - |
58 | | - $stopwatch->start('WebP_transforms'); |
59 | | - |
60 | | - $images = []; |
61 | | - |
62 | | - array_walk($directories, function ($directory) use (&$images) { |
63 | | - $fullPath = $directory[0] === '/' ?: "{$this->projectDir}/{$directory}"; |
64 | | - $images = $this->scanDirs($fullPath); |
65 | | - }); |
66 | | - |
67 | | - if ($input->getOption('create')) { |
68 | | - $progressBar = new ProgressBar($output, count($images)); |
69 | | - $progressBar->start(); |
70 | | - |
71 | | - $errors = []; |
72 | | - array_walk($images, static function ($image) use ($progressBar, &$errors, $input) { |
73 | | - $progressBar->advance(); |
74 | | - try { |
75 | | - $options = [ |
76 | | - 'quality' => (int)$input->getOption('quality'), |
77 | | - 'force' => $input->getOption('force'), |
78 | | - 'saveFile' => $input->getOption('create'), |
79 | | - 'filenameSuffix' => $input->getOption('suffix'), |
80 | | - ]; |
81 | | - WebPConverter::createWebPImage($image["path"], $options); |
82 | | - } catch (Exception $exception) { |
83 | | - $errors[] = $exception->getMessage(); |
84 | | - } |
85 | | - }); |
86 | | - $progressBar->finish(); |
87 | | - |
88 | | - if ($errors) { |
89 | | - $io->error($errors); |
90 | | - } |
91 | | - } else { |
92 | | - $io->text(count($images) . " images found, add --create to start webP generation"); |
93 | | - } |
94 | | - |
95 | | - $event = $stopwatch->stop('WebP_transforms'); |
96 | | - $io->text("Time : " . $event); |
97 | | - |
98 | | - return 0; |
99 | | - } |
100 | | - |
101 | | - private function scanDirs(string $fullPath): array |
102 | | - { |
103 | | - $finder = new Finder(); |
104 | | - $elements = []; |
105 | | - $finder->files()->in($fullPath); |
106 | | - $mimeTypeFinder = new FileinfoMimeTypeGuesser(); |
107 | | - if ($finder->hasResults()) { |
108 | | - foreach ($finder as $file) { |
109 | | - $type = $mimeTypeFinder->guessMimeType($file->getPathname()); |
110 | | - if (!in_array($type, self::CONVERTABLE_MIME_TYPES, true)){ |
111 | | - continue; |
112 | | - } |
113 | | - $elements[] = [ |
114 | | - "name" => $file->getFilenameWithoutExtension(), |
115 | | - "extension" => $file->getExtension(), |
116 | | - "path" => $file |
117 | | - ]; |
118 | | - } |
119 | | - } |
120 | | - return $elements; |
121 | | - } |
| 20 | + protected static $defaultName = 'codebuds:webp:convert'; |
| 21 | + |
| 22 | + protected const CONVERTABLE_MIME_TYPES = [ |
| 23 | + 'image/jpeg', |
| 24 | + 'image/png', |
| 25 | + 'image/gif', |
| 26 | + ]; |
| 27 | + |
| 28 | + public function __construct(private int $quality, private string $projectDir) |
| 29 | + { |
| 30 | + parent::__construct(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function configure(): void |
| 34 | + { |
| 35 | + $this |
| 36 | + ->setDescription('Generate webps in directories') |
| 37 | + ->addArgument('directories', InputArgument::IS_ARRAY, 'Directories for webP generation') |
| 38 | + ->addOption('create', null, InputOption::VALUE_NONE, 'Generate new files') |
| 39 | + ->addOption('force', null, InputOption::VALUE_NONE, 'Recreate all the files') |
| 40 | + ->addOption('quality', null, InputOption::VALUE_OPTIONAL, 'Set webp generation quality', $this->quality) |
| 41 | + ->addOption('suffix', null, InputOption::VALUE_OPTIONAL, 'Add a filename suffix', ''); |
| 42 | + } |
| 43 | + |
| 44 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 45 | + { |
| 46 | + $io = new SymfonyStyle($input, $output); |
| 47 | + $directories = $input->getArgument('directories'); |
| 48 | + |
| 49 | + if (!$directories) { |
| 50 | + $io->error('no directories passed'); |
| 51 | + |
| 52 | + return 1; |
| 53 | + } |
| 54 | + |
| 55 | + $stopwatch = new Stopwatch(); |
| 56 | + |
| 57 | + $stopwatch->start('WebP_transforms'); |
| 58 | + |
| 59 | + $images = []; |
| 60 | + |
| 61 | + array_walk($directories, function ($directory) use (&$images) { |
| 62 | + $fullPath = $directory[0] === '/' ?: "{$this->projectDir}/{$directory}"; |
| 63 | + $images = $this->scanDirs($fullPath); |
| 64 | + }); |
| 65 | + |
| 66 | + if ($input->getOption('create')) { |
| 67 | + $progressBar = new ProgressBar($output, count($images)); |
| 68 | + $progressBar->start(); |
| 69 | + |
| 70 | + $errors = []; |
| 71 | + array_walk($images, static function ($image) use ($progressBar, &$errors, $input) { |
| 72 | + $progressBar->advance(); |
| 73 | + try { |
| 74 | + $options = [ |
| 75 | + 'quality' => (int)$input->getOption('quality'), |
| 76 | + 'force' => $input->getOption('force'), |
| 77 | + 'saveFile' => $input->getOption('create'), |
| 78 | + 'filenameSuffix' => $input->getOption('suffix'), |
| 79 | + ]; |
| 80 | + WebPConverter::createWebPImage($image["path"], $options); |
| 81 | + } catch (Exception $exception) { |
| 82 | + $errors[] = $exception->getMessage(); |
| 83 | + } |
| 84 | + }); |
| 85 | + $progressBar->finish(); |
| 86 | + |
| 87 | + if ($errors) { |
| 88 | + $io->error($errors); |
| 89 | + } |
| 90 | + } else { |
| 91 | + $io->text(count($images) . " images found, add --create to start webP generation"); |
| 92 | + } |
| 93 | + |
| 94 | + $event = $stopwatch->stop('WebP_transforms'); |
| 95 | + $io->text("Time : " . $event); |
| 96 | + |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + private function scanDirs(string $fullPath): array |
| 101 | + { |
| 102 | + $finder = new Finder(); |
| 103 | + $elements = []; |
| 104 | + $finder->files()->in($fullPath); |
| 105 | + $mimeTypeFinder = new FileinfoMimeTypeGuesser(); |
| 106 | + if ($finder->hasResults()) { |
| 107 | + foreach ($finder as $file) { |
| 108 | + $type = $mimeTypeFinder->guessMimeType($file->getPathname()); |
| 109 | + if (!in_array($type, self::CONVERTABLE_MIME_TYPES, true)) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + $elements[] = [ |
| 113 | + "name" => $file->getFilenameWithoutExtension(), |
| 114 | + "extension" => $file->getExtension(), |
| 115 | + "path" => $file |
| 116 | + ]; |
| 117 | + } |
| 118 | + } |
| 119 | + return $elements; |
| 120 | + } |
122 | 121 | } |
0 commit comments