-
-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathQRNetpbmBitmap.php
More file actions
95 lines (75 loc) · 1.95 KB
/
QRNetpbmBitmap.php
File metadata and controls
95 lines (75 loc) · 1.95 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
<?php
/**
* Class QRNetbmBitmap
*
* @created 19.12.2025
* @author wgevaert & codemasher
* @copyright 2025 wgevaert & codemasher
* @license MIT
*/
declare(strict_types=1);
namespace chillerlan\QRCode\Output;
use UnexpectedValueException;
use function is_bool;
use function str_split;
use function pack;
use function str_repeat;
class QRNetpbmBitmap extends QRNetpbmAbstract{
public const MIME_TYPE = 'image/x-portable-bitmap';
protected const HEADER_ASCII = 'P1';
protected const HEADER_BINARY = 'P4';
protected function prepareModuleValue(mixed $value):mixed{
if ( !is_bool( $value ) ) {
throw new UnexpectedValueException( 'Bitmap expected bool modules' );
}
return $value;
}
protected function getDefaultModuleValue(bool $isDark):mixed{
return $isDark;
}
public static function moduleValueIsValid(mixed $value):bool{
return is_bool($value);
}
protected function setModuleValues():void{
// noop
}
protected function getMaxValueHeaderString(): string {
return '';
}
protected function getBodyASCII():string{
$body = '';
foreach($this->matrix->getBooleanMatrix() as $row){
$line = '';
foreach($row as $isDark){
$line .= str_repeat($isDark ? '1' : '0', $this->scale);
}
// Lines should not be longer than 70 chars
$line = implode("\n", str_split($line,70))."\n";
$body .= str_repeat($line, $this->scale);
}
return $body;
}
protected function getBodyBinary():string{
$body = '';
foreach($this->matrix->getBooleanMatrix() as $row){
$rowdata = array_fill(0, (int)ceil($this->length / 8), 0);
$byte = 0;
$bit = 0b10000000;
foreach($row as $isDark){
for($i = 0; $i < $this->scale; $i++){
if($bit <= 0){
$bit = 0b10000000;
$byte++;
}
if($isDark){
$rowdata[$byte] |= $bit;
}
$bit >>= 1;
}
}
$rowdataString = pack('C*', ...$rowdata);
$body .= str_repeat($rowdataString, $this->scale);
}
return $body;
}
}