chillerlan / php-qrcode

A PHP QR Code generator and reader with a user-friendly API.
https://smiley.codes/qrcode/
Apache License 2.0
2.01k stars 302 forks source link

Unable to add logo space with PDF output #161

Closed reconstrukt closed 2 years ago

reconstrukt commented 2 years ago

Hi,

I'm attempting to generate a QR Code with addLogoSpace option set to true , output to a PDF ('outputType' => QRCode::OUTPUT_FPDF,. But, receiving an error, details below.

Full code sample is below, w/ configuration options. Pls note I am not placing a logo in this example, just attempting to add space for it in the matrix.

I am using php v7.4.25 with the "chillerlan/php-qrcode": "dev-main" version in my composer.json.

Thank you! Matt

$dark = [255, 255, 255];      // white code 
$trans = [238, 35, 36];       // red background

$options = new QROptions([
    'version'      => 5,        
    'outputType'   => QRCode::OUTPUT_FPDF,
    'eccLevel'     => EccLevel::H,
    'addLogoSpace'    => true,
    'logoSpaceWidth'  => 13,
    'logoSpaceHeight' => 13,
    'scale'        => 5,
    'imageBase64'  => false,
    'maskPattern' => 4,
    'moduleValues' => [
        // finder
        QRMatrix::M_FINDER | QRMatrix::IS_DARK     => $dark, // dark (true)
        QRMatrix::M_FINDER_DOT | QRMatrix::IS_DARK => $dark, // finder dot, dark (true)
        QRMatrix::M_FINDER                         => $trans, // light (false), white is the transparency color by default            
        // alignment
        QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK  => $dark,
        QRMatrix::M_ALIGNMENT                      => $trans,
        // timing
        QRMatrix::M_TIMING | QRMatrix::IS_DARK     => $dark,
        QRMatrix::M_TIMING                         => $trans,
        // format
        QRMatrix::M_FORMAT | QRMatrix::IS_DARK     => $dark,
        QRMatrix::M_FORMAT                         => $trans,
        // version
        QRMatrix::M_VERSION | QRMatrix::IS_DARK    => $dark,
        QRMatrix::M_VERSION                        => $trans,
        // data
        QRMatrix::M_DATA | QRMatrix::IS_DARK       => $dark,
        QRMatrix::M_DATA                           => $trans,
        // darkmodule
        QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK => $dark,
        // separator
        QRMatrix::M_SEPARATOR                      => $trans,
        // quietzone
        QRMatrix::M_QUIETZONE                      => $trans,
    ],

]);

try{
  $qr = (new QRCode($options));
} catch(\Throwable $e){
  exit($e->getMessage());
}

$filename = uniqid() . '.pdf';
$filepath = config('filesystems.disks.temp.root') . '/' . $filename;    

$data = 'https://github.com/chillerlan/php-qrcode';

$qr->render($data, $filepath);

echo $filepath;

And i'm receiving the following error:

   chillerlan\QRCode\Data\QRCodeDataException

  code length overflow. (548 > 368 bit)

  at vendor/chillerlan/php-qrcode/src/Data/QRData.php:173
    169▕                }
    170▕
    171▕                // overflow, likely caused due to invalid version setting
    172▕                if($this->bitBuffer->getLength() > $MAX_BITS){
  ➜ 173▕                        throw new QRCodeDataException(
    174▕                                sprintf('code length overflow. (%d > %d bit)', $this->bitBuffer->getLength(), $MAX_BITS)
    175▕                        );
    176▕                }
    177▕

      +5 vendor frames
  6   app/Library/LabelRenderer.php:175
      chillerlan\QRCode\QRCode::render()
codemasher commented 2 years ago

The error means that the string you try to encode does not fit into the QR Code. Try increasing the version number or set it to Version::Auto.