QROutputAbstract.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Class QROutputAbstract
  4. *
  5. * @created 09.12.2015
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2015 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Output;
  11. use chillerlan\QRCode\QROptions;
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\Settings\SettingsContainerInterface;
  14. use Closure;
  15. use function base64_encode, dirname, file_put_contents, is_writable, ksort, sprintf;
  16. /**
  17. * common output abstract
  18. */
  19. abstract class QROutputAbstract implements QROutputInterface{
  20. /**
  21. * the current size of the QR matrix
  22. *
  23. * @see \chillerlan\QRCode\Data\QRMatrix::getSize()
  24. */
  25. protected int $moduleCount;
  26. /**
  27. * the side length of the QR image (modules * scale)
  28. */
  29. protected int $length;
  30. /**
  31. * an (optional) array of color values for the several QR matrix parts
  32. *
  33. * @phpstan-var array<int, mixed>
  34. */
  35. protected array $moduleValues;
  36. /**
  37. * the (filled) data matrix object
  38. */
  39. protected QRMatrix $matrix;
  40. /**
  41. * the options instance
  42. */
  43. protected SettingsContainerInterface|QROptions $options;
  44. /**
  45. * @see \chillerlan\QRCode\QROptions::$excludeFromConnect
  46. * @var int[]
  47. */
  48. protected array $excludeFromConnect;
  49. /**
  50. * @see \chillerlan\QRCode\QROptions::$keepAsSquare
  51. * @var int[]
  52. */
  53. protected array $keepAsSquare;
  54. /** @see \chillerlan\QRCode\QROptions::$scale */
  55. protected int $scale;
  56. /** @see \chillerlan\QRCode\QROptions::$connectPaths */
  57. protected bool $connectPaths;
  58. /** @see \chillerlan\QRCode\QROptions::$eol */
  59. protected string $eol;
  60. /** @see \chillerlan\QRCode\QROptions::$drawLightModules */
  61. protected bool $drawLightModules;
  62. /** @see \chillerlan\QRCode\QROptions::$drawCircularModules */
  63. protected bool $drawCircularModules;
  64. /** @see \chillerlan\QRCode\QROptions::$circleRadius */
  65. protected float $circleRadius;
  66. protected float $circleDiameter;
  67. /**
  68. * QROutputAbstract constructor.
  69. */
  70. public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
  71. $this->options = $options;
  72. $this->matrix = $matrix;
  73. if($this->options->invertMatrix){
  74. $this->matrix->invert();
  75. }
  76. $this->copyVars();
  77. $this->setMatrixDimensions();
  78. $this->setModuleValues();
  79. }
  80. /**
  81. * Creates copies of several QROptions values to avoid calling the magic getters
  82. * in long loops for a significant performance increase.
  83. *
  84. * These variables are usually used in the "module" methods and are called up to 31329 times (at version 40).
  85. */
  86. protected function copyVars():void{
  87. $vars = [
  88. 'connectPaths',
  89. 'excludeFromConnect',
  90. 'eol',
  91. 'drawLightModules',
  92. 'drawCircularModules',
  93. 'keepAsSquare',
  94. 'circleRadius',
  95. ];
  96. foreach($vars as $property){
  97. $this->{$property} = $this->options->{$property};
  98. }
  99. $this->circleDiameter = ($this->circleRadius * 2);
  100. }
  101. /**
  102. * Sets/updates the matrix dimensions
  103. *
  104. * Call this method if you modify the matrix from within your custom module in case the dimensions have been changed
  105. */
  106. protected function setMatrixDimensions():void{
  107. $this->moduleCount = $this->matrix->getSize();
  108. $this->scale = $this->options->scale;
  109. $this->length = ($this->moduleCount * $this->scale);
  110. }
  111. /**
  112. * Returns a 2 element array with the current output width and height
  113. *
  114. * The type and units of the values depend on the output class. The default value is the current module count * scale.
  115. *
  116. * @return int[]
  117. */
  118. protected function getOutputDimensions():array{
  119. return [$this->length, $this->length];
  120. }
  121. /**
  122. * Sets the initial module values
  123. */
  124. protected function setModuleValues():void{
  125. // first fill the map with the default values
  126. foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
  127. $this->moduleValues[$M_TYPE] = $this->getDefaultModuleValue($defaultValue);
  128. }
  129. // now loop over the options values to replace defaults and add extra values
  130. /** @var int $M_TYPE */
  131. foreach($this->options->moduleValues as $M_TYPE => $value){
  132. if($this::moduleValueIsValid($value)){
  133. $this->moduleValues[$M_TYPE] = $this->prepareModuleValue($value);
  134. }
  135. }
  136. }
  137. /**
  138. * Prepares the value for the given input (return value depends on the output class)
  139. */
  140. abstract protected function prepareModuleValue(mixed $value):mixed;
  141. /**
  142. * Returns a default value for either dark or light modules (return value depends on the output class)
  143. */
  144. abstract protected function getDefaultModuleValue(bool $isDark):mixed;
  145. /**
  146. * Returns the prepared value for the given $M_TYPE
  147. *
  148. * @throws \chillerlan\QRCode\Output\QRCodeOutputException if $moduleValues[$M_TYPE] doesn't exist
  149. */
  150. protected function getModuleValue(int $M_TYPE):mixed{
  151. if(!isset($this->moduleValues[$M_TYPE])){
  152. throw new QRCodeOutputException(sprintf('$M_TYPE %012b not found in module values map', $M_TYPE));
  153. }
  154. return $this->moduleValues[$M_TYPE];
  155. }
  156. /**
  157. * Returns the prepared module value at the given coordinate [$x, $y] (convenience)
  158. */
  159. protected function getModuleValueAt(int $x, int $y):mixed{
  160. return $this->getModuleValue($this->matrix->get($x, $y));
  161. }
  162. /**
  163. * Returns a base64 data URI for the given string and mime type
  164. */
  165. protected function toBase64DataURI(string $data, string|null $mime = null):string{
  166. return sprintf('data:%s;base64,%s', ($mime ?? static::MIME_TYPE), base64_encode($data));
  167. }
  168. /**
  169. * Saves the qr $data to a $file. If $file is null, nothing happens.
  170. *
  171. * @see file_put_contents()
  172. * @see \chillerlan\QRCode\QROptions::$cachefile
  173. *
  174. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  175. */
  176. protected function saveToFile(string $data, string|null $file = null):void{
  177. if($file === null){
  178. return;
  179. }
  180. if(!is_writable(dirname($file))){
  181. throw new QRCodeOutputException(sprintf('Cannot write data to cache file: %s', $file));
  182. }
  183. if(file_put_contents($file, $data) === false){
  184. throw new QRCodeOutputException(sprintf('Cannot write data to cache file: %s (file_put_contents error)', $file));
  185. }
  186. }
  187. /**
  188. * collects the modules per QRMatrix::M_* type and runs a $transform function on each module and
  189. * returns an array with the transformed modules
  190. *
  191. * The transform callback is called with the following parameters:
  192. *
  193. * $x - current column
  194. * $y - current row
  195. * $M_TYPE - field value
  196. * $M_TYPE_LAYER - (possibly modified) field value that acts as layer id
  197. *
  198. * @return array<int, mixed>
  199. */
  200. protected function collectModules(Closure $transform):array{
  201. $paths = [];
  202. // collect the modules for each type
  203. foreach($this->matrix->getMatrix() as $y => $row){
  204. foreach($row as $x => $M_TYPE){
  205. $M_TYPE_LAYER = $M_TYPE;
  206. if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){
  207. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  208. $M_TYPE_LAYER = QRMatrix::M_DATA;
  209. if($this->matrix->isDark($M_TYPE)){
  210. $M_TYPE_LAYER = QRMatrix::M_DATA_DARK;
  211. }
  212. }
  213. // collect the modules per $M_TYPE
  214. $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  215. if(!empty($module)){
  216. $paths[$M_TYPE_LAYER][] = $module;
  217. }
  218. }
  219. }
  220. // beautify output
  221. ksort($paths);
  222. return $paths;
  223. }
  224. }