QROutputAbstract.php 6.9 KB

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