EndroidQrCodeWithLogoProvider.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace RobThree\Auth\Providers\Qr;
  3. use Endroid\QrCode\Logo\Logo;
  4. use Endroid\QrCode\Writer\PngWriter;
  5. class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider
  6. {
  7. protected $logoPath;
  8. protected $logoSize;
  9. /**
  10. * Adds an image to the middle of the QR Code.
  11. * @param string $path Path to an image file
  12. * @param array|int $size Just the width, or [width, height]
  13. */
  14. public function setLogo($path, $size = null)
  15. {
  16. $this->logoPath = $path;
  17. $this->logoSize = (array)$size;
  18. }
  19. public function getQRCodeImage($qrtext, $size)
  20. {
  21. if (!$this->endroid4) {
  22. return $this->qrCodeInstance($qrtext, $size)->writeString();
  23. }
  24. $logo = null;
  25. if ($this->logoPath) {
  26. $logo = Logo::create($this->logoPath);
  27. if ($this->logoSize) {
  28. $logo->setResizeToWidth($this->logoSize[0]);
  29. if (isset($this->logoSize[1])) {
  30. $logo->setResizeToHeight($this->logoSize[1]);
  31. }
  32. }
  33. }
  34. $writer = new PngWriter();
  35. return $writer->write($this->qrCodeInstance($qrtext, $size), $logo)->getString();
  36. }
  37. protected function qrCodeInstance($qrtext, $size) {
  38. $qrCode = parent::qrCodeInstance($qrtext, $size);
  39. if (!$this->endroid4 && $this->logoPath) {
  40. $qrCode->setLogoPath($this->logoPath);
  41. if ($this->logoSize) {
  42. $qrCode->setLogoSize($this->logoSize[0], isset($this->logoSize[1]) ? $this->logoSize[1] : null);
  43. }
  44. }
  45. return $qrCode;
  46. }
  47. }