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