added QRString*

This commit is contained in:
smiley
2015-12-09 04:21:47 +01:00
parent d74ac93106
commit e1ae12a533
4 changed files with 145 additions and 9 deletions
+12 -9
View File
@@ -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;
+100
View File
@@ -0,0 +1,100 @@
<?php
/**
* Class QRString
*
* @filesource QRString.php
* @created 05.12.2015
* @package chillerlan\QRCode\Output
* @author Smiley <smiley@chillerlan.net>
* @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 .= '<p>';
for($col = 0; $col < $this->pixelCount; $col++){
$tag = $this->matrix[$row][$col]
? 'b' // dark
: 'i'; // light
$html .= '<'.$tag.'></'.$tag.'>';
}
$html .= '</p>'.PHP_EOL;
}
return $html;
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
/**
* Class QRStringOptions
*
* @filesource QRStringOptions.php
* @created 08.12.2015
* @package chillerlan\QRCode\Output
* @author Smiley <smiley@chillerlan.net>
* @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;
}
+4
View File
@@ -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%.