QRFpdf.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, is_array;
  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 setModuleValues():void{
  43. foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
  44. $v = $this->options->moduleValues[$M_TYPE] ?? null;
  45. if(!is_array($v) || count($v) < 3){
  46. $this->moduleValues[$M_TYPE] = $defaultValue
  47. ? [0, 0, 0]
  48. : [255, 255, 255];
  49. }
  50. else{
  51. $this->moduleValues[$M_TYPE] = array_values($v);
  52. }
  53. }
  54. }
  55. /**
  56. * @inheritDoc
  57. *
  58. * @return string|\FPDF
  59. */
  60. public function dump(string $file = null){
  61. $file ??= $this->options->cachefile;
  62. $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
  63. $fpdf->AddPage();
  64. $prevColor = null;
  65. foreach($this->matrix->matrix() as $y => $row){
  66. foreach($row as $x => $M_TYPE){
  67. /** @var int $M_TYPE */
  68. $color = $this->moduleValues[$M_TYPE];
  69. if($prevColor === null || $prevColor !== $color){
  70. /** @phan-suppress-next-line PhanParamTooFewUnpack */
  71. $fpdf->SetFillColor(...$color);
  72. $prevColor = $color;
  73. }
  74. $fpdf->Rect($x * $this->scale, $y * $this->scale, 1 * $this->scale, 1 * $this->scale, 'F');
  75. }
  76. }
  77. if($this->options->returnResource){
  78. return $fpdf;
  79. }
  80. $pdfData = $fpdf->Output('S');
  81. if($file !== null){
  82. $this->saveToFile($pdfData, $file);
  83. }
  84. if($this->options->imageBase64){
  85. $pdfData = $this->base64encode($pdfData, 'application/pdf');
  86. }
  87. return $pdfData;
  88. }
  89. }