QRFpdf.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  24. * QRFpdf constructor.
  25. *
  26. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  27. */
  28. public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
  29. if(!class_exists(FPDF::class)){
  30. // @codeCoverageIgnoreStart
  31. throw new QRCodeOutputException(
  32. 'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
  33. ' as dependency but the class "\\FPDF" couldn\'t be found.'
  34. );
  35. // @codeCoverageIgnoreEnd
  36. }
  37. parent::__construct($options, $matrix);
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. public static function moduleValueIsValid($value):bool{
  43. if(!is_array($value) || count($value) < 3){
  44. return false;
  45. }
  46. // check the first 3 values of the array
  47. foreach(array_values($value) as $i => $val){
  48. if($i > 2){
  49. break;
  50. }
  51. if(!is_numeric($val)){
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. /**
  58. * @param array $value
  59. *
  60. * @inheritDoc
  61. */
  62. protected function prepareModuleValue($value):array{
  63. $values = [];
  64. foreach(array_values($value) as $i => $val){
  65. if($i > 2){
  66. break;
  67. }
  68. $values[] = max(0, min(255, intval($val)));
  69. }
  70. return $values;
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. protected function getDefaultModuleValue(bool $isDark):array{
  76. return ($isDark) ? [0, 0, 0] : [255, 255, 255];
  77. }
  78. /**
  79. * @inheritDoc
  80. *
  81. * @return string|\FPDF
  82. */
  83. public function dump(string $file = null){
  84. $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
  85. $fpdf->AddPage();
  86. if($this::moduleValueIsValid($this->options->bgColor)){
  87. $bgColor = $this->prepareModuleValue($this->options->bgColor);
  88. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  89. $fpdf->SetFillColor(...$bgColor);
  90. $fpdf->Rect(0, 0, $this->length, $this->length, 'F');
  91. }
  92. $prevColor = null;
  93. for($y = 0; $y < $this->moduleCount; $y++){
  94. for($x = 0; $x < $this->moduleCount; $x++){
  95. if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
  96. continue;
  97. }
  98. $color = $this->getModuleValueAt($x, $y);
  99. if($prevColor !== $color){
  100. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  101. $fpdf->SetFillColor(...$color);
  102. $prevColor = $color;
  103. }
  104. $fpdf->Rect(($x * $this->scale), ($y * $this->scale), $this->scale, $this->scale, 'F');
  105. }
  106. }
  107. if($this->options->returnResource){
  108. return $fpdf;
  109. }
  110. $pdfData = $fpdf->Output('S');
  111. $this->saveToFile($pdfData, $file);
  112. if($this->options->imageBase64){
  113. $pdfData = $this->toBase64DataURI($pdfData, 'application/pdf');
  114. }
  115. return $pdfData;
  116. }
  117. }