diff --git a/examples/custom.php b/examples/custom.php index b7fd973ef..0b2bcee99 100644 --- a/examples/custom.php +++ b/examples/custom.php @@ -5,24 +5,27 @@ require_once '../vendor/autoload.php'; use chillerlan\QRCode\QRCode; use chillerlan\QRCode\QRConst; use chillerlan\QRCode\QROptions; +use chillerlan\QRCode\Output\QRString; +use chillerlan\QRCode\Output\QRStringOptions; $starttime = microtime(true); +$qrStringOptions = new QRStringOptions; +$qrStringOptions->type = QRConst::OUTPUT_STRING_TEXT; +$qrStringOptions->textDark = '+'; +$qrStringOptions->textLight = '-'; + $qrOptions = new QROptions; $qrOptions->typeNumber = QRConst::TYPE_05; $qrOptions->errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M; +$qrOptions->output = new QRString($qrStringOptions); // google authenticator // https://chart.googleapis.com/chart?chs=200x200&chld=M%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Ftest%3Fsecret%3DB3JX4VCVJDVNXNZ5%26issuer%3Dchillerlan.net -$qrcode = new QRCode; -$qrcode->setOptions($qrOptions, 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net'); -$qrcode->getRawData(); +$qrcode = new QRCode('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=buildwars.net', $qrOptions); +var_dump($qrcode->getRawData()); -for($row = 0; $row < $qrcode->pixelCount; $row++){ - for($col = 0; $col < $qrcode->pixelCount; $col++){ - echo $qrcode->matrix[$row][$col] ? '*' : ' '; - } - echo PHP_EOL; -} +$qrcode->setData('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', $qrOptions); +var_dump($qrcode->output()); echo 'QRCode: '.round((microtime(true)-$starttime), 5).PHP_EOL; diff --git a/src/Output/QRString.php b/src/Output/QRString.php new file mode 100644 index 000000000..79d6f55c4 --- /dev/null +++ b/src/Output/QRString.php @@ -0,0 +1,100 @@ + + * @copyright 2015 Smiley + * @license MIT + */ + +namespace chillerlan\QRCode\Output; +use chillerlan\QRCode\QRConst; + +/** + * + */ +class QRString extends QROutputBase implements QROutputInterface{ + + /** + * @var \chillerlan\QRCode\Output\QRStringOptions + */ + protected $options; + + /** + * @var \chillerlan\QRCode\Output\QRStringOptions $outputOptions + */ + public function __construct(QRStringOptions $outputOptions = null){ + $this->options = $outputOptions; + + if(!$this->options instanceof QRStringOptions){ + $this->options = new QRStringOptions; + } + + } + + /** + * @return string + * @throws \chillerlan\QRCode\Output\QRCodeOutputException + */ + public function dump(){ + + switch($this->options->type){ + case QRConst::OUTPUT_STRING_TEXT: return $this->toText(); + case QRConst::OUTPUT_STRING_JSON: return $this->toJSON(); + case QRConst::OUTPUT_STRING_HTML: return $this->toHTML(); + default: + throw new QRCodeOutputException('Invalid string output type!'); + } + + } + + /** + * @return string + */ + public function toText(){ + $text = ''; + + for($row = 0; $row < $this->pixelCount ; $row++){ + for($col = 0; $col < $this->pixelCount; $col++){ + $text .= $this->matrix[$row][$col] ? $this->options->textDark : $this->options->textLight; + } + $text .= $this->options->textNewline; + } + + return $text; + } + + /** + * @return string + */ + public function toJSON(){ + return json_encode($this->matrix); + } + + /** + * @return string + */ + public function toHTML(){ + $html = ''; + + for($row = 0; $row < $this->pixelCount; $row++){ + $html .= '
'; + + for($col = 0; $col < $this->pixelCount; $col++){ + $tag = $this->matrix[$row][$col] + ? 'b' // dark + : 'i'; // light + + $html .= '<'.$tag.'>'.$tag.'>'; + } + + $html .= '
'.PHP_EOL; + } + + return $html; + } + +} diff --git a/src/Output/QRStringOptions.php b/src/Output/QRStringOptions.php new file mode 100644 index 000000000..3ef06c3b9 --- /dev/null +++ b/src/Output/QRStringOptions.php @@ -0,0 +1,29 @@ + + * @copyright 2015 Smiley + * @license MIT + */ + +namespace chillerlan\QRCode\Output; +use chillerlan\QRCode\QRConst; + +/** + * + */ +class QRStringOptions{ + + public $type = QRConst::OUTPUT_STRING_TEXT; + + public $textDark = '#'; + + public $textLight = ' '; + + public $textNewline = PHP_EOL; + +} diff --git a/src/QRConst.php b/src/QRConst.php index c6ccfa321..65c4090c1 100644 --- a/src/QRConst.php +++ b/src/QRConst.php @@ -17,6 +17,10 @@ namespace chillerlan\QRCode; */ class QRConst{ + const OUTPUT_STRING_TEXT = 0; + const OUTPUT_STRING_JSON = 1; + const OUTPUT_STRING_HTML = 2; + const ERROR_CORRECT_LEVEL_L = 1; // 7%. const ERROR_CORRECT_LEVEL_M = 0; // 15%. const ERROR_CORRECT_LEVEL_Q = 3; // 25%.