Skip to content

[ENHANCEMENT] Support netpbm output formats #322

@wgevaert

Description

@wgevaert

Feature description

Netpbm is one of the most supported and easily understandable image formats:
https://en.wikipedia.org/wiki/Netpbm

A barebones example implementation for a black-white image (P1) would work something like in the code below. You could consider also supporting grayscale or color, but I think for now the black-white already would be great.

Code sample

/** Barebones implementation of class that I quickly threw together **/
class PbmQROutput extends QROutputAbstract {
  private const LIGHT_DARK = [ '0', '1' ];

  protected function prepareModuleValue(mixed $value):mixed{
    return $value;
  }

  protected function getDefaultModuleValue(bool $isDark):mixed{
    return $isDark ? self::LIGHT_DARK[1] : self::LIGHT_DARK[0];
  }

  public static function moduleValueIsValid(mixed $value):bool{
    return is_string($value) && in_array($value,self::LIGHT_DARK);
  }

  public function dump(string|null $file = null):mixed {
    $size = $this->matrix->getSize();
    $result = "P1\n$size $size\n";
    foreach( $this->matrix->getBooleanMatrix() as $row ) {
      foreach ( $row as $isDark ) {
        $result .= $this->getDefaultModuleValue( $isDark );
      }
      $result .= "\n";
    }
    
    if ( is_string($file) ) {
      file_put_contents($file, $result);
    }
    return $result;
  }
}


/** How you use the class **/
$options = new QROptions;
$options->outputInterface = PbmQROutput::class;
$options->quietzoneSize = 0;

$renderedQR = (new QRCode)->setOptions($options)->render( "https://example.com", "myfile.pbm" );

echo $renderedQR

Additional context

No response

Pull Request

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions