-
-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathQRNetpbmPixmap.php
More file actions
101 lines (89 loc) · 2.36 KB
/
QRNetpbmPixmap.php
File metadata and controls
101 lines (89 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Class QRNetpbmPixmap
*
* @created 19.12.2025
* @author wgevaert
* @copyright 2025 wgevaert
* @license MIT
*/
declare(strict_types=1);
namespace chillerlan\QRCode\Output;
use function pack;
use function str_repeat;
class QRNetpbmPixmap extends QRNetpbmAbstract{
public const MIME_TYPE = 'image/x-portable-pixmap';
protected const HEADER_ASCII = 'P3';
protected const HEADER_BINARY = 'P6';
protected function prepareModuleValue(mixed $value):mixed{
if (!is_array($value) || count($value) !== 3){
throw new UnexpectedValueException( 'Module value should be array of length 3 for NetpbmPixmap' );
}
$newValue = [];
foreach($value as $rgbValue) {
$rgbValue = intval( $rgbValue );
if ( $rgbValue < 0 ) {
$rgbValue = 0;
}
if ( $rgbValue > $this->options->netpbmMaxValue ) {
$rgbValue = $this->options->netpbmMaxValue;
}
$newValue []= $rgbValue;
}
return $newValue;
}
protected function getDefaultModuleValue(bool $isDark):mixed{
return array_fill(0, 3, $isDark ? 0 : $this->options->netpbmMaxValue);
}
public static function moduleValueIsValid(mixed $value):bool{
if ( !is_array($value) || count($value) !== 3 ) {
return false;
}
foreach ($value as $rgbVal) {
if (!is_int($rgbVal) || $rgbVal < 0 || $rgbVal >= 65536) {
return false;
}
}
return true;
}
protected function getBodyASCII():string{
$body = '';
$maxLength = (70 - strlen(' '.(string)$this->options->netpbmMaxValue) + 1);
foreach($this->matrix->getMatrix() as $row){
$line = '';
$rowString = '';
foreach ($row as $module) {
for ($i = 0; $i < $this->scale; $i++) {
foreach($this->getModuleValue($module) as $rgbValue) {
$line .= $rgbValue;
if (strlen($line) >= $maxLength) {
$rowString .= $line."\n";
$line = '';
} else {
$line .= ' ';
}
}
}
}
$body .= str_repeat(trim($rowString.$line)."\n", $this->scale);
}
return $body;
}
protected function getBodyBinary():string{
$format = $this->options->netpbmMaxValue > 255 ? 'n*' : 'C*';
$body = '';
foreach ($this->matrix->getMatrix() as $row) {
$line = '';
foreach($row as $module) {
$m = $this->getModuleValue($module);
$f = pack($format, ...$m);
$line .= str_repeat(
$f,
$this->scale
);
}
$body .= str_repeat($line, $this->scale);
}
return $body;
}
}