QROptionsTrait.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. /**
  3. * Trait QROptionsTrait
  4. *
  5. * @created 10.03.2018
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2018 smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpUnused
  11. */
  12. namespace chillerlan\QRCode;
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use chillerlan\QRCode\Decoder\{GDLuminanceSource, IMagickLuminanceSource};
  15. use function array_values, count, extension_loaded, in_array, is_numeric, max, min, sprintf, strtolower;
  16. /**
  17. * The QRCode plug-in settings & setter functionality
  18. */
  19. trait QROptionsTrait{
  20. /**
  21. * QR Code version number
  22. *
  23. * [1 ... 40] or QRCode::VERSION_AUTO
  24. */
  25. protected int $version = QRCode::VERSION_AUTO;
  26. /**
  27. * Minimum QR version
  28. *
  29. * if $version = QRCode::VERSION_AUTO
  30. */
  31. protected int $versionMin = 1;
  32. /**
  33. * Maximum QR version
  34. */
  35. protected int $versionMax = 40;
  36. /**
  37. * Error correct level
  38. *
  39. * QRCode::ECC_X where X is:
  40. *
  41. * - L => 7%
  42. * - M => 15%
  43. * - Q => 25%
  44. * - H => 30%
  45. */
  46. protected int $eccLevel = EccLevel::L;
  47. /**
  48. * Mask Pattern to use (no value in using, mostly for unit testing purposes)
  49. *
  50. * [0...7] or QRCode::MASK_PATTERN_AUTO
  51. */
  52. protected int $maskPattern = QRCode::MASK_PATTERN_AUTO;
  53. /**
  54. * Add a "quiet zone" (margin) according to the QR code spec
  55. *
  56. * @see https://www.qrcode.com/en/howto/code.html
  57. */
  58. protected bool $addQuietzone = true;
  59. /**
  60. * Size of the quiet zone
  61. *
  62. * internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules
  63. */
  64. protected int $quietzoneSize = 4;
  65. /**
  66. * The output type
  67. *
  68. * - QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
  69. * - QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
  70. * - QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
  71. * - QRCode::OUTPUT_CUSTOM
  72. */
  73. protected string $outputType = QRCode::OUTPUT_MARKUP_SVG;
  74. /**
  75. * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
  76. */
  77. protected ?string $outputInterface = null;
  78. /**
  79. * /path/to/cache.file
  80. */
  81. protected ?string $cachefile = null;
  82. /**
  83. * newline string [HTML, SVG, TEXT]
  84. */
  85. protected string $eol = PHP_EOL;
  86. /**
  87. * size of a QR code pixel [SVG, IMAGE_*], HTML via CSS
  88. */
  89. protected int $scale = 5;
  90. /**
  91. * a common css class
  92. */
  93. protected string $cssClass = 'qrcode';
  94. /**
  95. * SVG opacity
  96. */
  97. protected float $svgOpacity = 1.0;
  98. /**
  99. * anything between <defs>
  100. *
  101. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
  102. */
  103. protected string $svgDefs = '';
  104. /**
  105. * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
  106. *
  107. * viewBox="0 0 x x"
  108. *
  109. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
  110. * @see https://css-tricks.com/scale-svg/#article-header-id-3
  111. */
  112. protected ?int $svgViewBoxSize = null;
  113. /**
  114. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
  115. */
  116. protected string $svgPreserveAspectRatio = 'xMidYMid';
  117. /**
  118. * optional "width" attribute with the specified value (note that the value is not checked!)
  119. *
  120. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/width
  121. */
  122. protected ?string $svgWidth = null;
  123. /**
  124. * optional "height" attribute with the specified value (note that the value is not checked!)
  125. *
  126. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/height
  127. */
  128. protected ?string $svgHeight = null;
  129. /**
  130. * whether to connect the paths for the several module types to avoid weird glitches when using gradients etc.
  131. *
  132. * @see https://github.com/chillerlan/php-qrcode/issues/57
  133. */
  134. protected bool $connectPaths = false;
  135. /**
  136. * specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true
  137. */
  138. protected array $excludeFromConnect = [];
  139. /**
  140. * specify whether to draw the modules as filled circles
  141. *
  142. * a note for GDImage output:
  143. *
  144. * if QROptions::$scale is less or equal than 20, the image will be upscaled internally, then the modules will be drawn
  145. * using imagefilledellipse() and then scaled back to the expected size using IMG_BICUBIC which in turn produces
  146. * unexpected outcomes in combination with transparency - to avoid this, set scale to a value greater than 20.
  147. *
  148. * @see https://github.com/chillerlan/php-qrcode/issues/23
  149. * @see https://github.com/chillerlan/php-qrcode/discussions/122
  150. */
  151. protected bool $drawCircularModules = false;
  152. /**
  153. * specifies the radius of the modules when $svgDrawCircularModules is set to true
  154. */
  155. protected float $circleRadius = 0.45;
  156. /**
  157. * specifies which module types to exclude when $svgDrawCircularModules is set to true
  158. */
  159. protected array $keepAsSquare = [];
  160. /**
  161. * string substitute for dark
  162. */
  163. protected string $textDark = '🔴';
  164. /**
  165. * string substitute for light
  166. */
  167. protected string $textLight = '⭕';
  168. /**
  169. * markup substitute for dark (CSS value)
  170. */
  171. protected string $markupDark = '#000';
  172. /**
  173. * markup substitute for light (CSS value)
  174. */
  175. protected string $markupLight = '#fff';
  176. /**
  177. * Return the image resource instead of a render if applicable.
  178. * This option overrides other output options, such as $cachefile and $imageBase64.
  179. *
  180. * Supported by the following modules:
  181. *
  182. * - QRImage: resource (PHP < 8), GdImage
  183. * - QRImagick: Imagick
  184. * - QRFpdf: FPDF
  185. *
  186. * @see \chillerlan\QRCode\Output\QROutputInterface::dump()
  187. *
  188. * @var bool
  189. */
  190. protected bool $returnResource = false;
  191. /**
  192. * toggle base64 or raw image data
  193. */
  194. protected bool $imageBase64 = true;
  195. /**
  196. * toggle background transparency
  197. *
  198. * - In GdImage mode (png, gif) it sets imagecolortransparent() with QROptions::$imageTransparencyBG.
  199. * It also sets the "normal" background color without transparency switch.
  200. *
  201. * - In SVG mode (as of v5), it won't render the "light" modules,
  202. * as opacity/transparency can easily be set with css properties.
  203. *
  204. * - It has no effect in the FPDF and Imagick output modules.
  205. *
  206. * @see \chillerlan\QRCode\QROptions::$imageTransparencyBG
  207. * @see https://github.com/chillerlan/php-qrcode/discussions/121
  208. */
  209. protected bool $imageTransparent = true;
  210. /**
  211. * Sets the background color in GD mode.
  212. *
  213. * When QROptions::$imageTransparent is set to true, this color is set as transparent in imagecolortransparent()
  214. *
  215. * @see \chillerlan\QRCode\Output\QRGdImage
  216. * @see \chillerlan\QRCode\QROptions::$imageTransparent
  217. * @see imagecolortransparent()
  218. *
  219. * [R, G, B]
  220. */
  221. protected array $imageTransparencyBG = [255, 255, 255];
  222. /**
  223. * @see imagepng()
  224. */
  225. protected int $pngCompression = -1;
  226. /**
  227. * @see imagejpeg()
  228. */
  229. protected int $jpegQuality = 85;
  230. /**
  231. * Imagick output format
  232. *
  233. * @see \Imagick::setImageFormat()
  234. * @see https://www.imagemagick.org/script/formats.php
  235. */
  236. protected string $imagickFormat = 'png32';
  237. /**
  238. * Imagick background color (defaults to "transparent")
  239. *
  240. * @see \ImagickPixel::__construct()
  241. */
  242. protected ?string $imagickBG = null;
  243. /**
  244. * Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt")
  245. *
  246. * @see \FPDF::__construct()
  247. */
  248. protected string $fpdfMeasureUnit = 'pt';
  249. /**
  250. * Module values map
  251. *
  252. * - HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
  253. * - IMAGE: [63, 127, 255] // R, G, B
  254. */
  255. protected ?array $moduleValues = null;
  256. /**
  257. * use Imaagick (if available) when reading QR Codes
  258. */
  259. protected bool $readerUseImagickIfAvailable = false;
  260. /**
  261. * grayscale the image before reading
  262. */
  263. protected bool $readerGrayscale = false;
  264. /**
  265. * increase the contrast before reading
  266. *
  267. * note that applying contrast works different in GD and Imagick, so mileage may vary
  268. */
  269. protected bool $readerIncreaseContrast = false;
  270. /**
  271. * Toggles logo space creation
  272. */
  273. protected bool $addLogoSpace = false;
  274. /**
  275. * width of the logo space
  276. */
  277. protected int $logoSpaceWidth = 0;
  278. /**
  279. * height of the logo space
  280. */
  281. protected int $logoSpaceHeight = 0;
  282. /**
  283. * optional horizontal start position of the logo space (top left corner)
  284. */
  285. protected ?int $logoSpaceStartX = null;
  286. /**
  287. * optional vertical start position of the logo space (top left corner)
  288. */
  289. protected ?int $logoSpaceStartY = null;
  290. /**
  291. * clamp min/max version number
  292. */
  293. protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
  294. $min = max(1, min(40, $versionMin));
  295. $max = max(1, min(40, $versionMax));
  296. $this->versionMin = min($min, $max);
  297. $this->versionMax = max($min, $max);
  298. }
  299. /**
  300. * sets the minimum version number
  301. */
  302. protected function set_versionMin(int $version):void{
  303. $this->setMinMaxVersion($version, $this->versionMax);
  304. }
  305. /**
  306. * sets the maximum version number
  307. */
  308. protected function set_versionMax(int $version):void{
  309. $this->setMinMaxVersion($this->versionMin, $version);
  310. }
  311. /**
  312. * sets the error correction level
  313. *
  314. * @throws \chillerlan\QRCode\QRCodeException
  315. */
  316. protected function set_eccLevel(int $eccLevel):void{
  317. if(!in_array($eccLevel, [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H], true)){
  318. throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
  319. }
  320. $this->eccLevel = $eccLevel;
  321. }
  322. /**
  323. * sets/clamps the mask pattern
  324. */
  325. protected function set_maskPattern(int $maskPattern):void{
  326. if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
  327. $this->maskPattern = max(0, min(7, $maskPattern));
  328. }
  329. }
  330. /**
  331. * sets/clamps the quiet zone size
  332. */
  333. protected function set_quietzoneSize(int $quietzoneSize):void{
  334. $this->quietzoneSize = max(0, min($quietzoneSize, 75));
  335. }
  336. /**
  337. * sets the transparency background color
  338. *
  339. * @throws \chillerlan\QRCode\QRCodeException
  340. */
  341. protected function set_imageTransparencyBG(array $imageTransparencyBG):void{
  342. // invalid value - set to white as default
  343. if(count($imageTransparencyBG) < 3){
  344. $this->imageTransparencyBG = [255, 255, 255];
  345. return;
  346. }
  347. foreach($imageTransparencyBG as $k => $v){
  348. // cut off exceeding items
  349. if($k > 2){
  350. break;
  351. }
  352. if(!is_numeric($v)){
  353. throw new QRCodeException('Invalid RGB value.');
  354. }
  355. // clamp the values
  356. $this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
  357. }
  358. // use the array values to not run into errors with the spread operator (...$arr)
  359. $this->imageTransparencyBG = array_values($this->imageTransparencyBG);
  360. }
  361. /**
  362. * sets/clamps the version number
  363. */
  364. protected function set_version(int $version):void{
  365. if($version !== QRCode::VERSION_AUTO){
  366. $this->version = max(1, min(40, $version));
  367. }
  368. }
  369. /**
  370. * sets the FPDF measurement unit
  371. *
  372. * @codeCoverageIgnore
  373. */
  374. protected function set_fpdfMeasureUnit(string $unit):void{
  375. $unit = strtolower($unit);
  376. if(in_array($unit, ['cm', 'in', 'mm', 'pt'], true)){
  377. $this->fpdfMeasureUnit = $unit;
  378. }
  379. // @todo throw or ignore silently?
  380. }
  381. /**
  382. * enables Imagick for the QR Code reader if the extension is available
  383. */
  384. protected function set_readerUseImagickIfAvailable(bool $useImagickIfAvailable):void{
  385. $this->readerUseImagickIfAvailable = $useImagickIfAvailable && extension_loaded('imagick');
  386. }
  387. /**
  388. * returns the FQCN of the luminance source class to use in the reader (GD or Imagick)
  389. *
  390. * @see \chillerlan\QRCode\Decoder\LuminanceSourceInterface
  391. */
  392. public function getLuminanceSourceFQCN():string{
  393. // i still hate this
  394. return $this->readerUseImagickIfAvailable
  395. ? IMagickLuminanceSource::class
  396. : GDLuminanceSource::class;
  397. }
  398. /**
  399. * clamp the logo space values between 0 and maximum length (177 modules at version 40)
  400. */
  401. protected function clampLogoSpaceValue(int $value):int{
  402. return (int)max(0, min(177, $value));
  403. }
  404. /**
  405. * clamp/set logo space width
  406. */
  407. protected function set_logoSpaceWidth(int $value):void{
  408. $this->logoSpaceWidth = $this->clampLogoSpaceValue($value);
  409. }
  410. /**
  411. * clamp/set logo space height
  412. */
  413. protected function set_logoSpaceHeight(int $value):void{
  414. $this->logoSpaceHeight = $this->clampLogoSpaceValue($value);
  415. }
  416. /**
  417. * clamp/set horizontal logo space start
  418. */
  419. protected function set_logoSpaceStartX(?int $value):void{
  420. $this->logoSpaceStartX = $value === null ? null : $this->clampLogoSpaceValue($value);
  421. }
  422. /**
  423. * clamp/set vertical logo space start
  424. */
  425. protected function set_logoSpaceStartY(?int $value):void{
  426. $this->logoSpaceStartY = $value === null ? null : $this->clampLogoSpaceValue($value);
  427. }
  428. /**
  429. * clamp/set SVG circle radius
  430. */
  431. protected function set_circleRadius(float $circleRadius):void{
  432. $this->circleRadius = max(0.1, min(0.75, $circleRadius));
  433. }
  434. }