QRFpdf.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Class QRFpdf
  4. *
  5. * @created 03.06.2020
  6. * @author Maximilian Kresse
  7. * @license MIT
  8. *
  9. * @see https://github.com/chillerlan/php-qrcode/pull/49
  10. */
  11. namespace chillerlan\QRCode\Output;
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\Settings\SettingsContainerInterface;
  14. use FPDF;
  15. use function array_values, class_exists, count, intval, is_array, is_numeric, max, min;
  16. /**
  17. * QRFpdf output module (requires fpdf)
  18. *
  19. * @see https://github.com/Setasign/FPDF
  20. * @see http://www.fpdf.org/
  21. */
  22. class QRFpdf extends QROutputAbstract{
  23. protected FPDF $fpdf;
  24. protected ?array $prevColor = null;
  25. /**
  26. * QRFpdf constructor.
  27. *
  28. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  29. */
  30. public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
  31. if(!class_exists(FPDF::class)){
  32. // @codeCoverageIgnoreStart
  33. throw new QRCodeOutputException(
  34. 'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
  35. ' as dependency but the class "\\FPDF" couldn\'t be found.'
  36. );
  37. // @codeCoverageIgnoreEnd
  38. }
  39. parent::__construct($options, $matrix);
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. public static function moduleValueIsValid($value):bool{
  45. if(!is_array($value) || count($value) < 3){
  46. return false;
  47. }
  48. // check the first 3 values of the array
  49. foreach(array_values($value) as $i => $val){
  50. if($i > 2){
  51. break;
  52. }
  53. if(!is_numeric($val)){
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. /**
  60. * @param array $value
  61. *
  62. * @inheritDoc
  63. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  64. */
  65. protected function prepareModuleValue($value):array{
  66. $values = [];
  67. foreach(array_values($value) as $i => $val){
  68. if($i > 2){
  69. break;
  70. }
  71. $values[] = max(0, min(255, intval($val)));
  72. }
  73. if(count($values) !== 3){
  74. throw new QRCodeOutputException('invalid color value');
  75. }
  76. return $values;
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. protected function getDefaultModuleValue(bool $isDark):array{
  82. return ($isDark) ? [0, 0, 0] : [255, 255, 255];
  83. }
  84. /**
  85. * Initializes an FPDF instance
  86. */
  87. protected function initFPDF():FPDF{
  88. return new FPDF('P', $this->options->fpdfMeasureUnit, $this->getOutputDimensions());
  89. }
  90. /**
  91. * @inheritDoc
  92. *
  93. * @return string|\FPDF
  94. */
  95. public function dump(string $file = null){
  96. $this->fpdf = $this->initFPDF();
  97. $this->fpdf->AddPage();
  98. if($this::moduleValueIsValid($this->options->bgColor)){
  99. $bgColor = $this->prepareModuleValue($this->options->bgColor);
  100. [$width, $height] = $this->getOutputDimensions();
  101. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  102. $this->fpdf->SetFillColor(...$bgColor);
  103. $this->fpdf->Rect(0, 0, $width, $height, 'F');
  104. }
  105. $this->prevColor = null;
  106. foreach($this->matrix->getMatrix() as $y => $row){
  107. foreach($row as $x => $M_TYPE){
  108. $this->module($x, $y, $M_TYPE);
  109. }
  110. }
  111. if($this->options->returnResource){
  112. return $this->fpdf;
  113. }
  114. $pdfData = $this->fpdf->Output('S');
  115. $this->saveToFile($pdfData, $file);
  116. if($this->options->outputBase64){
  117. $pdfData = $this->toBase64DataURI($pdfData, 'application/pdf');
  118. }
  119. return $pdfData;
  120. }
  121. /**
  122. * Renders a single module
  123. */
  124. protected function module(int $x, int $y, int $M_TYPE):void{
  125. if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
  126. return;
  127. }
  128. $color = $this->getModuleValue($M_TYPE);
  129. if($color !== null && $color !== $this->prevColor){
  130. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  131. $this->fpdf->SetFillColor(...$color);
  132. $this->prevColor = $color;
  133. }
  134. $this->fpdf->Rect(($x * $this->scale), ($y * $this->scale), $this->scale, $this->scale, 'F');
  135. }
  136. }