QRFpdf.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 class_exists, count, 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. protected 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. for($i = 0; $i < 3; $i++){
  48. if(!is_numeric($value[$i])){
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. protected function getModuleValue($value):array{
  58. $v = [];
  59. for($i = 0; $i < 3; $i++){
  60. // clamp value
  61. $v[] = (int)max(0, min(255, $value[$i]));
  62. }
  63. return $v;
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. protected function getDefaultModuleValue(bool $isDark):array{
  69. return $isDark ? [0, 0, 0] : [255, 255, 255];
  70. }
  71. /**
  72. * @inheritDoc
  73. *
  74. * @return string|\FPDF
  75. */
  76. public function dump(string $file = null){
  77. $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
  78. $fpdf->AddPage();
  79. if($this->moduleValueIsValid($this->options->bgColor)){
  80. $bgColor = $this->getModuleValue($this->options->bgColor);
  81. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  82. $fpdf->SetFillColor(...$bgColor);
  83. $fpdf->Rect(0, 0, $this->length, $this->length, 'F');
  84. }
  85. $prevColor = null;
  86. foreach($this->matrix->matrix() as $y => $row){
  87. foreach($row as $x => $M_TYPE){
  88. if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
  89. continue;
  90. }
  91. /** @var int $M_TYPE */
  92. $color = $this->moduleValues[$M_TYPE];
  93. if($prevColor !== $color){
  94. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  95. $fpdf->SetFillColor(...$color);
  96. $prevColor = $color;
  97. }
  98. $fpdf->Rect($x * $this->scale, $y * $this->scale, $this->scale, $this->scale, 'F');
  99. }
  100. }
  101. if($this->options->returnResource){
  102. return $fpdf;
  103. }
  104. $pdfData = $fpdf->Output('S');
  105. $this->saveToFile($pdfData, $file);
  106. if($this->options->imageBase64){
  107. $pdfData = $this->toBase64DataURI($pdfData, 'application/pdf');
  108. }
  109. return $pdfData;
  110. }
  111. }