├── .gitignore ├── convert ├── README.md ├── composer.json └── src ├── Application.php └── Command └── ConvertCommand.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | composer.lock 3 | vendor 4 | -------------------------------------------------------------------------------- /convert: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | run(); 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-console-qrcode 2 | A PHP program that can generate qrcode in linux console 3 | 4 | # Installation 5 | 6 | ## Standalone 7 | 8 | 1. `git clone https://github.com/leo108/php-console-qrcode` 9 | 2. `cd php-console-qrcode && composer install` 10 | 11 | ## In project 12 | 13 | `composer require leo108/php-console-qrcode` 14 | 15 | # Usage 16 | 17 | `./convert --text=leo108.com` 18 | 19 | or 20 | 21 | `./convert --stdin` and then input the text 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leo108/php-console-qrcode", 3 | "description": "A PHP program that can generate qrcode in linux console", 4 | "require": { 5 | "aferrandini/phpqrcode": "^1.0", 6 | "symfony/console": "2.*|3.*", 7 | "symfony/process": "2.*|3.*" 8 | }, 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "leo108", 13 | "email": "leo108@qq.com" 14 | } 15 | ], 16 | "autoload": { 17 | "psr-4": { 18 | "Leo108\\ConsoleQrCode\\": "src/" 19 | } 20 | }, 21 | "bin": [ 22 | "convert" 23 | ], 24 | "config": { 25 | "preferred-install": "dist", 26 | "sort-packages": true 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Application.php: -------------------------------------------------------------------------------- 1 | setArguments(); 28 | 29 | return $inputDefinition; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Command/ConvertCommand.php: -------------------------------------------------------------------------------- 1 | setName('convert') 18 | ->addOption('text', null, InputOption::VALUE_REQUIRED, 'The text you want to convert') 19 | ->addOption('lr', null, InputOption::VALUE_REQUIRED, 'The left and right padding', 1) 20 | ->addOption('tb', null, InputOption::VALUE_REQUIRED, 'The top and bottom padding', 1) 21 | ->addOption('stdin', null, InputOption::VALUE_NONE, 'Read the text from SDTIN'); 22 | } 23 | 24 | protected function execute(InputInterface $input, OutputInterface $output) 25 | { 26 | $lrPadding = $input->getOption('lr'); 27 | $tbPadding = $input->getOption('tb'); 28 | if ($input->getOption('stdin')) { 29 | $text = ''; 30 | while ($line = fgets(STDIN)) { 31 | $text .= $line; 32 | } 33 | } else { 34 | $text = $input->getOption('text'); 35 | } 36 | 37 | if (empty($text)) { 38 | throw new InvalidArgumentException('Convert text cannot be empty'); 39 | } 40 | 41 | $map = array( 42 | 0 => ' ', 43 | 1 => ' ', 44 | ); 45 | $this->initStyle($output); 46 | $text = QRcode::text($text); 47 | $length = strlen($text[0]); 48 | $paddingLine = str_repeat($map[0], $length + $lrPadding * 2)."\n"; 49 | $after = $before = str_repeat($paddingLine, $tbPadding); 50 | $output->write($before); 51 | foreach ($text as $line) { 52 | $output->write(str_repeat($map[0], $lrPadding)); 53 | for ($i = 0; $i < $length; $i++) { 54 | $type = substr($line, $i, 1); 55 | $output->write($map[$type]); 56 | } 57 | $output->writeln(str_repeat($map[0], $lrPadding)); 58 | } 59 | $output->write($after); 60 | } 61 | 62 | protected function initStyle(OutputInterface $output) 63 | { 64 | $style = new OutputFormatterStyle('black', 'black'); 65 | $output->getFormatter()->setStyle('blackc', $style); 66 | $style = new OutputFormatterStyle('white', 'white'); 67 | $output->getFormatter()->setStyle('whitec', $style); 68 | } 69 | } 70 | --------------------------------------------------------------------------------