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