-
-
Notifications
You must be signed in to change notification settings - Fork 330
Add QRPbm output format for Portable BitMap outputs #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wgevaert
wants to merge
17
commits into
chillerlan:main
Choose a base branch
from
wgevaert:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0a069c3
Add QRPbm output format for Portable BitMap outputs
wgevaert e70a744
Add pbm output to readme
wgevaert f5a2d2b
Actually run tests against new class
wgevaert aa748d2
Formatting
wgevaert 9b94cfe
Call instance methods non-statically
wgevaert aff03cc
Also support binary bitmaps
wgevaert 78ae3a3
Missing import
wgevaert 03802e8
Formatting
wgevaert ddef97d
Formatting
wgevaert e5f60fd
Remove superseded QRPbm class
wgevaert a520c0f
Add mime type and return base64 if needed
wgevaert b2f1286
Add grayscale, and incorporate suggestions from review
wgevaert a9eda61
Add support for Pixmap and fix codestyle
wgevaert ce85a50
Remove now obsolete classes
wgevaert 5182a50
Bugfixes and review comment changes
wgevaert 2bb89f8
Add tests
wgevaert e6f3319
Remove superfluous check on maxvalue
wgevaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
| /** | ||
| * Class QRPbm | ||
| * | ||
| * @created 11.12.2025 | ||
| * @author wgevaert | ||
| * @copyright 2025 wgevaert | ||
| * @license MIT | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace chillerlan\QRCode\Output; | ||
|
|
||
| use chillerlan\QRCode\Output\QROutputAbstract; | ||
|
|
||
| abstract class QRNetpbmBitmapAbstract extends QROutputAbstract { | ||
|
|
||
| protected function prepareModuleValue( mixed $value ): mixed { | ||
| if ( !is_bool( $value ) ) { | ||
| throw new UnexpectedValueException( "Expected boolean module value" ); | ||
| } | ||
| return $value; | ||
| } | ||
|
|
||
| protected function getDefaultModuleValue( bool $isDark ): bool { | ||
| return $isDark; | ||
| } | ||
|
|
||
| public static function moduleValueIsValid( mixed $value ): bool { | ||
| return is_bool( $value ); | ||
| } | ||
|
|
||
| abstract protected function getHeader(): string; | ||
|
|
||
| protected function getBody(): string { | ||
| $body = ''; | ||
| foreach ( $this->matrix->getBooleanMatrix() as $row ) { | ||
| $line = ''; | ||
| foreach ($row as $isDark) { | ||
| $line .= str_repeat( $isDark ? '1' : '0', $this->scale ); | ||
| } | ||
| $line .= "\n"; | ||
| $body .= str_repeat( $line, $this->scale ); | ||
| } | ||
| return trim($body,"\n"); | ||
| } | ||
|
|
||
| public function dump( string|null $file = null ): mixed { | ||
| $qrString = $this->getHeader() . "\n" | ||
| .$this->length.' '.$this->length."\n".$this->getBody(); | ||
| $this->saveToFile( $qrString, $file ); | ||
|
|
||
| return $qrString; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
| /** | ||
| * Class QRPbm | ||
| * | ||
| * @created 11.12.2025 | ||
| * @author wgevaert | ||
| * @copyright 2025 wgevaert | ||
| * @license MIT | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace chillerlan\QRCode\Output; | ||
|
|
||
| use chillerlan\QRCode\Output\QRNetpbmBitmapAbstract; | ||
|
|
||
| class QRNetpbmBitmapAscii extends QRNetpbmBitmapAbstract { | ||
| protected function getHeader(): string { | ||
| return 'P1'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
| /** | ||
| * Class QRPbm | ||
| * | ||
| * @created 11.12.2025 | ||
| * @author wgevaert | ||
| * @copyright 2025 wgevaert | ||
| * @license MIT | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace chillerlan\QRCode\Output; | ||
|
|
||
| use chillerlan\QRCode\Output\QRNetpbmBitmapAbstract; | ||
|
|
||
| class QRNetpbmBitmapBinary extends QRNetpbmBitmapAbstract { | ||
| protected function getHeader(): string { | ||
| return 'P4'; | ||
| } | ||
|
|
||
| protected function getBody(): string { | ||
| $asciiBody = parent::getBody(); | ||
| $body = ''; | ||
| foreach ( explode("\n", $asciiBody) as $row ) { | ||
| $body .= $this->asciiBinToBinary( $row ); | ||
| } | ||
| return $body; | ||
| } | ||
|
|
||
| private function asciiBinToBinary( string $asciiBin ): string { | ||
| $binaryString = ''; | ||
| foreach( str_split( $asciiBin, 8 ) as $currentChunk ) { | ||
| $currentChunk = str_pad( $currentChunk, 8, '0' ); | ||
| $binaryString .= pack( "C", bindec( $currentChunk ) ); | ||
| } | ||
| return $binaryString; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * Class QRPbmTest | ||
| * | ||
| * @created 11.12.2025 | ||
| * @author wgevaert | ||
| * @copyright 2025 wgevaert | ||
| * @license MIT | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace chillerlan\QRCodeTest\Output; | ||
|
|
||
| use chillerlan\QRCode\QROptions; | ||
| use chillerlan\QRCode\Data\QRMatrix; | ||
| use chillerlan\QRCode\Output\{QROutputInterface, QRNetpbmBitmapBinary}; | ||
| use chillerlan\Settings\SettingsContainerInterface; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
|
|
||
| /** | ||
| * Tests the QRNetpbmBitmapBinary output class | ||
| */ | ||
| final class QRNetpbmBitmapBinaryTest extends QROutputTestAbstract { | ||
|
|
||
| protected function getOutputInterface( | ||
| SettingsContainerInterface|QROptions $options, | ||
| QRMatrix $matrix, | ||
| ):QROutputInterface{ | ||
| return new QRNetpbmBitmapBinary($options, $matrix); | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-return array<string, array{0: mixed, 1: bool}> | ||
| */ | ||
| public static function moduleValueProvider():array{ | ||
| return [ | ||
| 'invalid: wrong type' => [[], false], | ||
| 'invalid: wrong type' => ['abc', false], | ||
| 'valid: true' => [true, true], | ||
| 'valid: false' => [false, true], | ||
| ]; | ||
| } | ||
|
|
||
| #[Test] | ||
| public function setModuleValues():void{ | ||
| $this->options->moduleValues = [ | ||
| // data | ||
| QRMatrix::M_DATA_DARK => true, | ||
| QRMatrix::M_DATA => false, | ||
| ]; | ||
|
|
||
| $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix); | ||
| $data = $this->outputInterface->dump(); | ||
|
|
||
| $this::assertStringContainsString("\0", $data); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.