QROptionsTrait.php 12 KB

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