mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-09-05 06:57:39 +00:00
:octocat: use function (could've done that before...)
This commit is contained in:
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function array_search, ord;
|
||||
|
||||
/**
|
||||
* Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
|
||||
*/
|
||||
@@ -51,13 +53,13 @@ class AlphaNum extends QRDataAbstract{
|
||||
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
||||
*/
|
||||
protected function getCharCode(string $chr):int{
|
||||
$i = \array_search($chr, $this::ALPHANUM_CHAR_MAP);
|
||||
$i = array_search($chr, $this::ALPHANUM_CHAR_MAP);
|
||||
|
||||
if($i !== false){
|
||||
return $i;
|
||||
}
|
||||
|
||||
throw new QRCodeDataException('illegal char: "'.$chr.'" ['.\ord($chr).']');
|
||||
throw new QRCodeDataException('illegal char: "'.$chr.'" ['.ord($chr).']');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-1
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function ord;
|
||||
|
||||
/**
|
||||
* Byte mode, ISO-8859-1 or UTF-8
|
||||
*/
|
||||
@@ -36,7 +38,7 @@ class Byte extends QRDataAbstract{
|
||||
$i = 0;
|
||||
|
||||
while($i < $this->strlen){
|
||||
$this->bitBuffer->put(\ord($data[$i]), 8);
|
||||
$this->bitBuffer->put(ord($data[$i]), 8);
|
||||
$i++;
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function mb_strlen, ord, strlen;
|
||||
|
||||
/**
|
||||
* Kanji mode: double-byte characters from the Shift JIS character set
|
||||
*/
|
||||
@@ -33,7 +35,7 @@ class Kanji extends QRDataAbstract{
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getLength(string $data):int{
|
||||
return \mb_strlen($data, 'SJIS');
|
||||
return mb_strlen($data, 'SJIS');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,10 +45,10 @@ class Kanji extends QRDataAbstract{
|
||||
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
||||
*/
|
||||
protected function write(string $data):void{
|
||||
$len = \strlen($data);
|
||||
$len = strlen($data);
|
||||
|
||||
for($i = 0; $i + 1 < $len; $i += 2){
|
||||
$c = ((0xff & \ord($data[$i])) << 8) | (0xff & \ord($data[$i + 1]));
|
||||
$c = ((0xff & ord($data[$i])) << 8) | (0xff & ord($data[$i + 1]));
|
||||
|
||||
if(0x8140 <= $c && $c <= 0x9FFC){
|
||||
$c -= 0x8140;
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
namespace chillerlan\QRCode\Data;
|
||||
|
||||
use function abs, call_user_func;
|
||||
|
||||
/**
|
||||
* The sole purpose of this class is to receive a QRMatrix object and run the pattern tests on it.
|
||||
*
|
||||
@@ -56,7 +58,7 @@ class MaskPatternTester{
|
||||
$penalty = 0;
|
||||
|
||||
for($level = 1; $level <= 4; $level++){
|
||||
$penalty += \call_user_func([$this, 'testLevel'.$level]);
|
||||
$penalty += call_user_func([$this, 'testLevel'.$level]);
|
||||
}
|
||||
|
||||
return (int)$penalty;
|
||||
@@ -212,7 +214,7 @@ class MaskPatternTester{
|
||||
}
|
||||
}
|
||||
|
||||
return (\abs(100 * $count / $this->moduleCount / $this->moduleCount - 50) / 5) * 10;
|
||||
return (abs(100 * $count / $this->moduleCount / $this->moduleCount - 50) / 5) * 10;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function substr;
|
||||
|
||||
/**
|
||||
* Numeric mode: decimal digits 0 through 9
|
||||
*/
|
||||
@@ -36,18 +38,18 @@ class Number extends QRDataAbstract{
|
||||
$i = 0;
|
||||
|
||||
while($i + 2 < $this->strlen){
|
||||
$this->bitBuffer->put($this->parseInt(\substr($data, $i, 3)), 10);
|
||||
$this->bitBuffer->put($this->parseInt(substr($data, $i, 3)), 10);
|
||||
$i += 3;
|
||||
}
|
||||
|
||||
if($i < $this->strlen){
|
||||
|
||||
if($this->strlen - $i === 1){
|
||||
$this->bitBuffer->put($this->parseInt(\substr($data, $i, $i + 1)), 4);
|
||||
$this->bitBuffer->put($this->parseInt(substr($data, $i, $i + 1)), 4);
|
||||
}
|
||||
// @codeCoverageIgnoreStart
|
||||
elseif($this->strlen - $i === 2){
|
||||
$this->bitBuffer->put($this->parseInt(\substr($data, $i, $i + 2)), 7);
|
||||
$this->bitBuffer->put($this->parseInt(substr($data, $i, $i + 2)), 7);
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
|
||||
+15
-14
@@ -16,6 +16,8 @@ use chillerlan\QRCode\{QRCode, QRCodeException};
|
||||
use chillerlan\QRCode\Helpers\{BitBuffer, Polynomial};
|
||||
use chillerlan\Settings\SettingsContainerInterface;
|
||||
|
||||
use function array_fill, array_merge, count, max, range, strlen;
|
||||
|
||||
/**
|
||||
* Processes the binary data and maps it on a matrix which is then being returned
|
||||
*/
|
||||
@@ -169,7 +171,7 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
* @return int
|
||||
*/
|
||||
protected function getLength(string $data):int{
|
||||
return \strlen($data);
|
||||
return strlen($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,7 +184,7 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
$maxlength = 0;
|
||||
|
||||
// guess the version number within the given range
|
||||
foreach(\range($this->options->versionMin, $this->options->versionMax) as $version){
|
||||
foreach(range($this->options->versionMin, $this->options->versionMax) as $version){
|
||||
$maxlength = $this::MAX_LENGTH[$version][QRCode::DATA_MODES[$this->datamode]][QRCode::ECC_MODES[$this->options->eccLevel]];
|
||||
|
||||
if($this->strlen <= $maxlength){
|
||||
@@ -213,7 +215,6 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
protected function writeBitBuffer(string $data):QRDataInterface{
|
||||
$this->bitBuffer = new BitBuffer;
|
||||
|
||||
// @todo: fixme, get real length
|
||||
$MAX_BITS = $this::MAX_BITS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
|
||||
|
||||
$this->bitBuffer
|
||||
@@ -270,13 +271,13 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
protected function maskECC():array{
|
||||
[$l1, $l2, $b1, $b2] = $this::RSBLOCKS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
|
||||
|
||||
$rsBlocks = \array_fill(0, $l1, [$b1, $b2]);
|
||||
$rsBlocks = array_fill(0, $l1, [$b1, $b2]);
|
||||
$rsCount = $l1 + $l2;
|
||||
$this->ecdata = \array_fill(0, $rsCount, null);
|
||||
$this->ecdata = array_fill(0, $rsCount, null);
|
||||
$this->dcdata = $this->ecdata;
|
||||
|
||||
if($l2 > 0){
|
||||
$rsBlocks = \array_merge($rsBlocks, \array_fill(0, $l2, [$b1 + 1, $b2 + 1]));
|
||||
$rsBlocks = array_merge($rsBlocks, array_fill(0, $l2, [$b1 + 1, $b2 + 1]));
|
||||
}
|
||||
|
||||
$totalCodeCount = 0;
|
||||
@@ -288,9 +289,9 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
[$rsBlockTotal, $dcCount] = $block;
|
||||
|
||||
$ecCount = $rsBlockTotal - $dcCount;
|
||||
$maxDcCount = \max($maxDcCount, $dcCount);
|
||||
$maxEcCount = \max($maxEcCount, $ecCount);
|
||||
$this->dcdata[$key] = \array_fill(0, $dcCount, null);
|
||||
$maxDcCount = max($maxDcCount, $dcCount);
|
||||
$maxEcCount = max($maxEcCount, $ecCount);
|
||||
$this->dcdata[$key] = array_fill(0, $dcCount, null);
|
||||
|
||||
foreach($this->dcdata[$key] as $a => $_z){
|
||||
$this->dcdata[$key][$a] = 0xff & $this->bitBuffer->buffer[$a + $offset];
|
||||
@@ -307,13 +308,13 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
$totalCodeCount += $rsBlockTotal;
|
||||
}
|
||||
|
||||
$data = \array_fill(0, $totalCodeCount, null);
|
||||
$data = array_fill(0, $totalCodeCount, null);
|
||||
$index = 0;
|
||||
|
||||
$mask = function($arr, $count) use (&$data, &$index, $rsCount){
|
||||
for($x = 0; $x < $count; $x++){
|
||||
for($y = 0; $y < $rsCount; $y++){
|
||||
if($x < \count($arr[$y])){
|
||||
if($x < count($arr[$y])){
|
||||
$data[$index] = $arr[$y][$x];
|
||||
$index++;
|
||||
}
|
||||
@@ -342,19 +343,19 @@ abstract class QRDataAbstract implements QRDataInterface{
|
||||
$rsPoly->multiply($modPoly->getNum());
|
||||
}
|
||||
|
||||
$rsPolyCount = \count($rsPoly->getNum());
|
||||
$rsPolyCount = count($rsPoly->getNum());
|
||||
|
||||
$modPoly
|
||||
->setNum($this->dcdata[$key], $rsPolyCount - 1)
|
||||
->mod($rsPoly->getNum())
|
||||
;
|
||||
|
||||
$this->ecdata[$key] = \array_fill(0, $rsPolyCount - 1, null);
|
||||
$this->ecdata[$key] = array_fill(0, $rsPolyCount - 1, null);
|
||||
$num = $modPoly->getNum();
|
||||
|
||||
return [
|
||||
$num,
|
||||
\count($num) - \count($this->ecdata[$key]),
|
||||
count($num) - count($this->ecdata[$key]),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
+15
-13
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function array_fill, array_key_exists, array_push, array_unshift, count, floor, in_array, max, min, range;
|
||||
|
||||
/**
|
||||
* @link http://www.thonky.com/qr-code-tutorial/format-version-information
|
||||
*/
|
||||
@@ -202,18 +204,18 @@ class QRMatrix{
|
||||
*/
|
||||
public function __construct(int $version, int $eclevel){
|
||||
|
||||
if(!\in_array($version, \range(1, 40), true)){
|
||||
if(!in_array($version, range(1, 40), true)){
|
||||
throw new QRCodeDataException('invalid QR Code version');
|
||||
}
|
||||
|
||||
if(!\array_key_exists($eclevel, QRCode::ECC_MODES)){
|
||||
if(!array_key_exists($eclevel, QRCode::ECC_MODES)){
|
||||
throw new QRCodeDataException('invalid ecc level');
|
||||
}
|
||||
|
||||
$this->version = $version;
|
||||
$this->eclevel = $eclevel;
|
||||
$this->moduleCount = $this->version * 4 + 17;
|
||||
$this->matrix = \array_fill(0, $this->moduleCount, \array_fill(0, $this->moduleCount, $this::M_NULL));
|
||||
$this->matrix = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -412,7 +414,7 @@ class QRMatrix{
|
||||
*/
|
||||
public function setTimingPattern():QRMatrix{
|
||||
|
||||
foreach(\range(8, $this->moduleCount - 8 - 1) as $i){
|
||||
foreach(range(8, $this->moduleCount - 8 - 1) as $i){
|
||||
|
||||
if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
|
||||
continue;
|
||||
@@ -440,7 +442,7 @@ class QRMatrix{
|
||||
if($bits !== false){
|
||||
|
||||
for($i = 0; $i < 18; $i++){
|
||||
$a = (int)\floor($i / 3);
|
||||
$a = (int)floor($i / 3);
|
||||
$b = $i % 3 + $this->moduleCount - 8 - 3;
|
||||
$v = !$test && (($bits >> $i) & 1) === 1;
|
||||
|
||||
@@ -509,23 +511,23 @@ class QRMatrix{
|
||||
}
|
||||
|
||||
$size = $size !== null
|
||||
? \max(0, \min($size, \floor($this->moduleCount / 2)))
|
||||
? max(0, min($size, floor($this->moduleCount / 2)))
|
||||
: 4;
|
||||
|
||||
for($y = 0; $y < $this->moduleCount; $y++){
|
||||
for($i = 0; $i < $size; $i++){
|
||||
\array_unshift($this->matrix[$y], $this::M_QUIETZONE);
|
||||
\array_push($this->matrix[$y], $this::M_QUIETZONE);
|
||||
array_unshift($this->matrix[$y], $this::M_QUIETZONE);
|
||||
array_push($this->matrix[$y], $this::M_QUIETZONE);
|
||||
}
|
||||
}
|
||||
|
||||
$this->moduleCount += ($size * 2);
|
||||
|
||||
$r = \array_fill(0, $this->moduleCount, $this::M_QUIETZONE);
|
||||
$r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE);
|
||||
|
||||
for($i = 0; $i < $size; $i++){
|
||||
\array_unshift($this->matrix, $r);
|
||||
\array_push($this->matrix, $r);
|
||||
array_unshift($this->matrix, $r);
|
||||
array_push($this->matrix, $r);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@@ -543,7 +545,7 @@ class QRMatrix{
|
||||
*/
|
||||
public function mapData(array $data, int $maskPattern):QRMatrix{
|
||||
$this->maskPattern = $maskPattern;
|
||||
$byteCount = \count($data);
|
||||
$byteCount = count($data);
|
||||
$size = $this->moduleCount - 1;
|
||||
|
||||
for($i = $size, $y = $size, $inc = -1, $byteIndex = 0, $bitIndex = 7; $i > 0; $i -= 2){
|
||||
@@ -616,7 +618,7 @@ class QRMatrix{
|
||||
$y % 2,
|
||||
$x % 3,
|
||||
$a % 3,
|
||||
(\floor($y / 2) + \floor($x / 3)) % 2,
|
||||
(floor($y / 2) + floor($x / 3)) % 2,
|
||||
$m % 2 + $m % 3,
|
||||
($m % 2 + $m % 3) % 2,
|
||||
($m % 3 + $a % 2) % 2
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
namespace chillerlan\QRCode\Helpers;
|
||||
|
||||
use function count, floor;
|
||||
|
||||
class BitBuffer{
|
||||
|
||||
/**
|
||||
@@ -55,9 +57,9 @@ class BitBuffer{
|
||||
* @return \chillerlan\QRCode\Helpers\BitBuffer
|
||||
*/
|
||||
public function putBit(bool $bit):BitBuffer{
|
||||
$bufIndex = \floor($this->length / 8);
|
||||
$bufIndex = floor($this->length / 8);
|
||||
|
||||
if(\count($this->buffer) <= $bufIndex){
|
||||
if(count($this->buffer) <= $bufIndex){
|
||||
$this->buffer[] = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Helpers;
|
||||
|
||||
use chillerlan\QRCode\QRCodeException;
|
||||
|
||||
use function array_fill, count;
|
||||
|
||||
/**
|
||||
* @link http://www.thonky.com/qr-code-tutorial/error-correction-coding
|
||||
*/
|
||||
@@ -87,13 +89,13 @@ class Polynomial{
|
||||
*/
|
||||
public function setNum(array $num, int $shift = null):Polynomial{
|
||||
$offset = 0;
|
||||
$numCount = \count($num);
|
||||
$numCount = count($num);
|
||||
|
||||
while($offset < $numCount && $num[$offset] === 0){
|
||||
$offset++;
|
||||
}
|
||||
|
||||
$this->num = \array_fill(0, $numCount - $offset + ($shift ?? 0), 0);
|
||||
$this->num = array_fill(0, $numCount - $offset + ($shift ?? 0), 0);
|
||||
|
||||
for($i = 0; $i < $numCount - $offset; $i++){
|
||||
$this->num[$i] = $num[$i + $offset];
|
||||
@@ -108,7 +110,7 @@ class Polynomial{
|
||||
* @return \chillerlan\QRCode\Helpers\Polynomial
|
||||
*/
|
||||
public function multiply(array $e):Polynomial{
|
||||
$n = \array_fill(0, \count($this->num) + \count($e) - 1, 0);
|
||||
$n = array_fill(0, count($this->num) + count($e) - 1, 0);
|
||||
|
||||
foreach($this->num as $i => $vi){
|
||||
$vi = $this->glog($vi);
|
||||
@@ -132,7 +134,7 @@ class Polynomial{
|
||||
public function mod(array $e):Polynomial{
|
||||
$n = $this->num;
|
||||
|
||||
if(\count($n) - \count($e) < 0){
|
||||
if(count($n) - count($e) < 0){
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
+26
-21
@@ -13,6 +13,11 @@
|
||||
namespace chillerlan\QRCode\Output;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
use Exception;
|
||||
|
||||
use function array_values, base64_encode, call_user_func, count, imagecolorallocate, imagecolortransparent,
|
||||
imagecreatetruecolor, imagedestroy, imagefilledrectangle, imagegif, imagejpeg, imagepng, in_array,
|
||||
is_array, ob_end_clean, ob_get_contents, ob_start, range;
|
||||
|
||||
/**
|
||||
* Converts the matrix into GD images, raw or base64 output
|
||||
@@ -45,13 +50,13 @@ class QRImage extends QROutputAbstract{
|
||||
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
|
||||
$v = $this->options->moduleValues[$M_TYPE] ?? null;
|
||||
|
||||
if(!\is_array($v) || \count($v) < 3){
|
||||
if(!is_array($v) || count($v) < 3){
|
||||
$this->moduleValues[$M_TYPE] = $defaultValue
|
||||
? [0, 0, 0]
|
||||
: [255, 255, 255];
|
||||
}
|
||||
else{
|
||||
$this->moduleValues[$M_TYPE] = \array_values($v);
|
||||
$this->moduleValues[$M_TYPE] = array_values($v);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,18 +69,18 @@ class QRImage extends QROutputAbstract{
|
||||
* @return string
|
||||
*/
|
||||
public function dump(string $file = null):string{
|
||||
$this->image = \imagecreatetruecolor($this->length, $this->length);
|
||||
$this->image = imagecreatetruecolor($this->length, $this->length);
|
||||
|
||||
// avoid: Indirect modification of overloaded property $imageTransparencyBG has no effect
|
||||
// https://stackoverflow.com/a/10455217
|
||||
$tbg = $this->options->imageTransparencyBG;
|
||||
$background = \imagecolorallocate($this->image, ...$tbg);
|
||||
$background = imagecolorallocate($this->image, ...$tbg);
|
||||
|
||||
if((bool)$this->options->imageTransparent && \in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
|
||||
\imagecolortransparent($this->image, $background);
|
||||
if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
|
||||
imagecolortransparent($this->image, $background);
|
||||
}
|
||||
|
||||
\imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);
|
||||
imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);
|
||||
|
||||
foreach($this->matrix->matrix() as $y => $row){
|
||||
foreach($row as $x => $M_TYPE){
|
||||
@@ -86,7 +91,7 @@ class QRImage extends QROutputAbstract{
|
||||
$imageData = $this->dumpImage($file);
|
||||
|
||||
if((bool)$this->options->imageBase64){
|
||||
$imageData = 'data:image/'.$this->options->outputType.';base64,'.\base64_encode($imageData);
|
||||
$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
|
||||
}
|
||||
|
||||
return $imageData;
|
||||
@@ -100,13 +105,13 @@ class QRImage extends QROutputAbstract{
|
||||
* @return void
|
||||
*/
|
||||
protected function setPixel(int $x, int $y, array $rgb):void{
|
||||
\imagefilledrectangle(
|
||||
imagefilledrectangle(
|
||||
$this->image,
|
||||
$x * $this->scale,
|
||||
$y * $this->scale,
|
||||
($x + 1) * $this->scale,
|
||||
($y + 1) * $this->scale,
|
||||
\imagecolorallocate($this->image, ...$rgb)
|
||||
imagecolorallocate($this->image, ...$rgb)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -120,22 +125,22 @@ class QRImage extends QROutputAbstract{
|
||||
protected function dumpImage(string $file = null):string{
|
||||
$file = $file ?? $this->options->cachefile;
|
||||
|
||||
\ob_start();
|
||||
ob_start();
|
||||
|
||||
try{
|
||||
\call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
|
||||
call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
|
||||
}
|
||||
// not going to cover edge cases
|
||||
// @codeCoverageIgnoreStart
|
||||
catch(\Exception $e){
|
||||
catch(Exception $e){
|
||||
throw new QRCodeOutputException($e->getMessage());
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$imageData = \ob_get_contents();
|
||||
\imagedestroy($this->image);
|
||||
$imageData = ob_get_contents();
|
||||
imagedestroy($this->image);
|
||||
|
||||
\ob_end_clean();
|
||||
ob_end_clean();
|
||||
|
||||
if($file !== null){
|
||||
$this->saveToFile($imageData, $file);
|
||||
@@ -148,10 +153,10 @@ class QRImage extends QROutputAbstract{
|
||||
* @return void
|
||||
*/
|
||||
protected function png():void{
|
||||
\imagepng(
|
||||
imagepng(
|
||||
$this->image,
|
||||
null,
|
||||
\in_array($this->options->pngCompression, \range(-1, 9), true)
|
||||
in_array($this->options->pngCompression, range(-1, 9), true)
|
||||
? $this->options->pngCompression
|
||||
: -1
|
||||
);
|
||||
@@ -162,17 +167,17 @@ class QRImage extends QROutputAbstract{
|
||||
* @return void
|
||||
*/
|
||||
protected function gif():void{
|
||||
\imagegif($this->image);
|
||||
imagegif($this->image);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function jpg():void{
|
||||
\imagejpeg(
|
||||
imagejpeg(
|
||||
$this->image,
|
||||
null,
|
||||
\in_array($this->options->jpegQuality, \range(0, 100), true)
|
||||
in_array($this->options->jpegQuality, range(0, 100), true)
|
||||
? $this->options->jpegQuality
|
||||
: 85
|
||||
);
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Output;
|
||||
|
||||
use Imagick, ImagickDraw, ImagickPixel;
|
||||
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* ImageMagick output module
|
||||
* requires ext-imagick
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Output;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function is_string, sprintf, strip_tags, trim;
|
||||
|
||||
/**
|
||||
* Converts the matrix into markup types: HTML, SVG, ...
|
||||
*/
|
||||
@@ -39,13 +41,13 @@ class QRMarkup extends QROutputAbstract{
|
||||
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
|
||||
$v = $this->options->moduleValues[$M_TYPE] ?? null;
|
||||
|
||||
if(!\is_string($v)){
|
||||
if(!is_string($v)){
|
||||
$this->moduleValues[$M_TYPE] = $defaultValue
|
||||
? $this->options->markupDark
|
||||
: $this->options->markupLight;
|
||||
}
|
||||
else{
|
||||
$this->moduleValues[$M_TYPE] = \trim(\strip_tags($v), '\'"');
|
||||
$this->moduleValues[$M_TYPE] = trim(strip_tags($v), '\'"');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -85,7 +87,7 @@ class QRMarkup extends QROutputAbstract{
|
||||
protected function svg():string{
|
||||
$matrix = $this->matrix->matrix();
|
||||
|
||||
$svg = \sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount)
|
||||
$svg = sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount)
|
||||
.$this->options->eol
|
||||
.'<defs>'.$this->options->svgDefs.'</defs>'
|
||||
.$this->options->eol;
|
||||
|
||||
@@ -15,6 +15,8 @@ namespace chillerlan\QRCode\Output;
|
||||
use chillerlan\QRCode\{Data\QRMatrix, QRCode};
|
||||
use chillerlan\Settings\SettingsContainerInterface;
|
||||
|
||||
use function call_user_func, dirname, file_put_contents, get_called_class, in_array, is_writable;
|
||||
|
||||
/**
|
||||
* common output abstract
|
||||
*/
|
||||
@@ -73,7 +75,7 @@ abstract class QROutputAbstract implements QROutputInterface{
|
||||
$this->scale = $this->options->scale;
|
||||
$this->length = $this->moduleCount * $this->scale;
|
||||
|
||||
$class = \get_called_class();
|
||||
$class = get_called_class();
|
||||
|
||||
if(\array_key_exists($class, QRCode::OUTPUT_MODES) && \in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){
|
||||
$this->outputMode = $this->options->outputType;
|
||||
@@ -100,11 +102,11 @@ abstract class QROutputAbstract implements QROutputInterface{
|
||||
*/
|
||||
protected function saveToFile(string $data, string $file):bool{
|
||||
|
||||
if(!\is_writable(\dirname($file))){
|
||||
if(!is_writable(dirname($file))){
|
||||
throw new QRCodeOutputException('Could not write data to cache file: '.$file);
|
||||
}
|
||||
|
||||
return (bool)\file_put_contents($file, $data);
|
||||
return (bool)file_put_contents($file, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,7 +115,7 @@ abstract class QROutputAbstract implements QROutputInterface{
|
||||
* @return string|mixed
|
||||
*/
|
||||
public function dump(string $file = null){
|
||||
$data = \call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
|
||||
$data = call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
|
||||
$file = $file ?? $this->options->cachefile;
|
||||
|
||||
if($file !== null){
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace chillerlan\QRCode\Output;
|
||||
|
||||
use chillerlan\QRCode\QRCode;
|
||||
|
||||
use function implode, is_string, json_encode;
|
||||
|
||||
/**
|
||||
* Converts the matrix data into string types
|
||||
*/
|
||||
@@ -58,17 +60,17 @@ class QRString extends QROutputAbstract{
|
||||
$r[] = $this->moduleValues[$M_TYPE];
|
||||
}
|
||||
|
||||
$str[] = \implode('', $r);
|
||||
$str[] = implode('', $r);
|
||||
}
|
||||
|
||||
return \implode($this->options->eol, $str);
|
||||
return implode($this->options->eol, $str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function json():string{
|
||||
return \json_encode($this->matrix->matrix());
|
||||
return json_encode($this->matrix->matrix());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-11
@@ -20,6 +20,8 @@ use chillerlan\QRCode\Output\{
|
||||
};
|
||||
use chillerlan\Settings\SettingsContainerInterface;
|
||||
|
||||
use function array_search, call_user_func_array, class_exists, in_array, mb_internal_encoding, min, ord, strlen;
|
||||
|
||||
/**
|
||||
* Turns a text string into a Model 2 QR Code
|
||||
*
|
||||
@@ -111,9 +113,9 @@ class QRCode{
|
||||
*/
|
||||
public function __construct(SettingsContainerInterface $options = null){
|
||||
// save the current mb encoding (in case it differs from UTF-8)
|
||||
$this->mbCurrentEncoding = \mb_internal_encoding();
|
||||
$this->mbCurrentEncoding = mb_internal_encoding();
|
||||
// use UTF-8 from here on
|
||||
\mb_internal_encoding('UTF-8');
|
||||
mb_internal_encoding('UTF-8');
|
||||
|
||||
$this->options = $options ?? new QROptions;
|
||||
}
|
||||
@@ -123,7 +125,7 @@ class QRCode{
|
||||
*/
|
||||
public function __destruct(){
|
||||
// restore the previous mb_internal_encoding, so that we don't mess up the rest of the script
|
||||
\mb_internal_encoding($this->mbCurrentEncoding);
|
||||
mb_internal_encoding($this->mbCurrentEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +185,7 @@ class QRCode{
|
||||
$penalties[$pattern] = $tester->testPattern();
|
||||
}
|
||||
|
||||
return \array_search(\min($penalties), $penalties, true);
|
||||
return array_search(min($penalties), $penalties, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,7 +201,7 @@ class QRCode{
|
||||
foreach(['Number', 'AlphaNum', 'Kanji', 'Byte'] as $mode){
|
||||
$dataInterface = __NAMESPACE__.'\\Data\\'.$mode;
|
||||
|
||||
if(\call_user_func_array([$this, 'is'.$mode], [$data]) && \class_exists($dataInterface)){
|
||||
if(call_user_func_array([$this, 'is'.$mode], [$data]) && class_exists($dataInterface)){
|
||||
return new $dataInterface($this->options, $data);
|
||||
}
|
||||
|
||||
@@ -218,13 +220,13 @@ class QRCode{
|
||||
*/
|
||||
protected function initOutputInterface(string $data):QROutputInterface{
|
||||
|
||||
if($this->options->outputType === $this::OUTPUT_CUSTOM && \class_exists($this->options->outputInterface)){
|
||||
if($this->options->outputType === $this::OUTPUT_CUSTOM && class_exists($this->options->outputInterface)){
|
||||
return new $this->options->outputInterface($this->options, $this->getMatrix($data));
|
||||
}
|
||||
|
||||
foreach($this::OUTPUT_MODES as $outputInterface => $modes){
|
||||
|
||||
if(in_array($this->options->outputType, $modes, true) && \class_exists($outputInterface)){
|
||||
if(in_array($this->options->outputType, $modes, true) && class_exists($outputInterface)){
|
||||
return new $outputInterface($this->options, $this->getMatrix($data));
|
||||
}
|
||||
|
||||
@@ -264,10 +266,10 @@ class QRCode{
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkString(string $string, array $charmap):bool{
|
||||
$len = \strlen($string);
|
||||
$len = strlen($string);
|
||||
|
||||
for($i = 0; $i < $len; $i++){
|
||||
if(!\in_array($string[$i], $charmap, true)){
|
||||
if(!in_array($string[$i], $charmap, true)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -284,10 +286,10 @@ class QRCode{
|
||||
*/
|
||||
public function isKanji(string $string):bool{
|
||||
$i = 0;
|
||||
$len = \strlen($string);
|
||||
$len = strlen($string);
|
||||
|
||||
while($i + 1 < $len){
|
||||
$c = ((0xff&\ord($string[$i])) << 8)|(0xff&\ord($string[$i + 1]));
|
||||
$c = ((0xff & ord($string[$i])) << 8) | (0xff & ord($string[$i + 1]));
|
||||
|
||||
if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
|
||||
return false;
|
||||
|
||||
+12
-10
@@ -12,6 +12,8 @@
|
||||
|
||||
namespace chillerlan\QRCode;
|
||||
|
||||
use function array_values, count, is_array, is_numeric, max, min;
|
||||
|
||||
trait QROptionsTrait{
|
||||
|
||||
/**
|
||||
@@ -248,11 +250,11 @@ trait QROptionsTrait{
|
||||
* @return void
|
||||
*/
|
||||
protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
|
||||
$min = \max(1, \min(40, $versionMin));
|
||||
$max = \max(1, \min(40, $versionMax));
|
||||
$min = max(1, min(40, $versionMin));
|
||||
$max = max(1, min(40, $versionMax));
|
||||
|
||||
$this->versionMin = \min($min, $max);
|
||||
$this->versionMax = \max($min, $max);
|
||||
$this->versionMin = min($min, $max);
|
||||
$this->versionMax = max($min, $max);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,7 +298,7 @@ trait QROptionsTrait{
|
||||
protected function set_maskPattern(int $maskPattern):void{
|
||||
|
||||
if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
|
||||
$this->maskPattern = \max(0, \min(7, $maskPattern));
|
||||
$this->maskPattern = max(0, min(7, $maskPattern));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -310,7 +312,7 @@ trait QROptionsTrait{
|
||||
protected function set_imageTransparencyBG($imageTransparencyBG):void{
|
||||
|
||||
// invalid value - set to white as default
|
||||
if(!\is_array($imageTransparencyBG) || \count($imageTransparencyBG) < 3){
|
||||
if(!is_array($imageTransparencyBG) || count($imageTransparencyBG) < 3){
|
||||
$this->imageTransparencyBG = [255, 255, 255];
|
||||
|
||||
return;
|
||||
@@ -318,16 +320,16 @@ trait QROptionsTrait{
|
||||
|
||||
foreach($imageTransparencyBG as $k => $v){
|
||||
|
||||
if(!\is_numeric($v)){
|
||||
if(!is_numeric($v)){
|
||||
throw new QRCodeException('Invalid RGB value.');
|
||||
}
|
||||
|
||||
// clamp the values
|
||||
$this->imageTransparencyBG[$k] = \max(0, \min(255, (int)$v));
|
||||
$this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
|
||||
}
|
||||
|
||||
// use the array values to not run into errors with the spread operator (...$arr)
|
||||
$this->imageTransparencyBG = \array_values($this->imageTransparencyBG);
|
||||
$this->imageTransparencyBG = array_values($this->imageTransparencyBG);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -338,7 +340,7 @@ trait QROptionsTrait{
|
||||
protected function set_version(int $version):void{
|
||||
|
||||
if($version !== QRCode::VERSION_AUTO){
|
||||
$this->version = \max(1, \min(40, $version));
|
||||
$this->version = max(1, min(40, $version));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user