endroid / qr-code

QR Code Generator
https://endroid.nl
MIT License
4.46k stars 728 forks source link

How can I generate a PNG file without alpha channel? #264

Closed Huseriato closed 4 years ago

Huseriato commented 4 years ago

This is the code I used:

      $sepaQr = new SepaQr();
      $sepaQr->setName('Recipients Name');
      $sepaQr->setIban('DE12345678901234567890');
      $sepaQr->setBic('SOMEBICNUM');
      $sepaQr->setAmount(100.00);
      $sepaQr->setRemittanceText('R-123456789-0');
      $sepaQr->setSize(600);
      $sepaQr->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

      $filename = tempnam(sys_get_temp_dir(), 'qrcode');

      try
      {
        $sepaQr->writeFile($filename);
        // Using the image with FPDF 1.52 here does not work (there is a big dependency in our software here)
      }
      finally
      {
        unlink($filename);
      }

What I found in Writers/PngWriter.php inside function createInterpolatedImage is imagesavealpha($image, true);, which is always set. If I remove the line, all is fine. I tried this:

        if($qrCode->getBackgroundColor()['a'] > 0)
          imagesavealpha($image, true);

So the alpha channel is only activated, if the background is opaque. But I don't know, if this should be the way to do this.

endroid commented 4 years ago

Hi @Huseriato what exactly is the issue when alpha channel information is saved, besides the small increase in file size?

Huseriato commented 4 years ago

I want to add this QR-Code to a bill, using FPDF. I'm using an older version of this lib (update not possible), which supports no alpha channel for png. The only other supported image format is jpg, but qr-code lib can't generate jpegs.

However, if the background of the qr code is fully opaque, there is no need to save an alpha channel for the resulting png.

endroid commented 4 years ago

Hi @Huseriato the current master includes the fix.