-
-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathQRNetpbmGraymap.php
More file actions
85 lines (73 loc) · 2.04 KB
/
QRNetpbmGraymap.php
File metadata and controls
85 lines (73 loc) · 2.04 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
<?php
/**
* Class QRPbm
*
* @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 QRNetpbmGraymap extends QRNetpbmAbstract{
public const MIME_TYPE = 'image/x-portable-graymap';
protected const HEADER_ASCII = 'P2';
protected const HEADER_BINARY = 'P5';
protected function prepareModuleValue(mixed $value):mixed{
$value = intval( $value );
if ( $value < 0 ) {
return 0;
}
if ( $value > $this->options->netpbmMaxValue ) {
return $this->options->netpbmMaxValue;
}
return $value;
}
protected function getDefaultModuleValue(bool $isDark):mixed{
return $isDark ? 0 : $this->options->netpbmMaxValue;
}
public static function moduleValueIsValid(mixed $value):bool{
// Since this is called statically, we cannot know what $this->options->netpbmMaxValue will be.
return is_int($value) && 0 < $value && $value < 65536;
}
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++) {
$line .= $this->getModuleValue($module);
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{
$body = '';
foreach ($this->matrix->getMatrix() as $row) {
$line = '';
foreach($row as $module) {
$line .= str_repeat($this->pack($this->getModuleValue($module)), $this->scale);
}
$body .= str_repeat($line, $this->scale);
}
return $body;
}
protected function pack( int $moduleValue ) {
if ( $this->options->netpbmMaxValue > 255 ) {
return pack('n', $moduleValue);
}
return pack('C', $moduleValue);
}
}