qrvect.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Image output of code using GD2
  6. *
  7. * PHP QR Code is distributed under LGPL 3
  8. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. define('QR_VECT', true);
  25. class QRvect {
  26. //----------------------------------------------------------------------
  27. public static function eps($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  28. {
  29. $vect = self::vectEPS($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
  30. if ($filename === false) {
  31. header("Content-Type: application/postscript");
  32. header('Content-Disposition: filename="qrcode.eps"');
  33. return $vect;
  34. } else {
  35. if($saveandprint===TRUE){
  36. QRtools::save($vect, $filename);
  37. header("Content-Type: application/postscript");
  38. header('Content-Disposition: filename="'.$filename.'"');
  39. return $vect;
  40. }else{
  41. QRtools::save($vect, $filename);
  42. }
  43. }
  44. }
  45. //----------------------------------------------------------------------
  46. private static function vectEPS($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  47. {
  48. $h = count($frame);
  49. $w = strlen($frame[0]);
  50. $imgW = $w + 2*$outerFrame;
  51. $imgH = $h + 2*$outerFrame;
  52. // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
  53. $r = round((($fore_color & 0xFF0000) >> 16) / 255, 5);
  54. $b = round((($fore_color & 0x00FF00) >> 8) / 255, 5);
  55. $g = round(($fore_color & 0x0000FF) / 255, 5);
  56. $fore_color = $r.' '.$b.' '.$g;
  57. // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
  58. $r = round((($back_color & 0xFF0000) >> 16) / 255, 5);
  59. $b = round((($back_color & 0x00FF00) >> 8) / 255, 5);
  60. $g = round(($back_color & 0x0000FF) / 255, 5);
  61. $back_color = $r.' '.$b.' '.$g;
  62. $output =
  63. '%!PS-Adobe EPSF-3.0'."\n".
  64. '%%Creator: Zend_Matrixcode_Qrcode'."\n".
  65. '%%Title: QRcode'."\n".
  66. '%%CreationDate: '.date('Y-m-d')."\n".
  67. '%%DocumentData: Clean7Bit'."\n".
  68. '%%LanguageLevel: 2'."\n".
  69. '%%Pages: 1'."\n".
  70. '%%BoundingBox: 0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint."\n";
  71. // set the scale
  72. $output .= $pixelPerPoint.' '.$pixelPerPoint.' scale'."\n";
  73. // position the center of the coordinate system
  74. $output .= $outerFrame.' '.$outerFrame.' translate'."\n";
  75. // redefine the 'rectfill' operator to shorten the syntax
  76. $output .= '/F { rectfill } def'."\n";
  77. // set the symbol color
  78. $output .= $back_color.' setrgbcolor'."\n";
  79. $output .= '-'.$outerFrame.' -'.$outerFrame.' '.($w + 2*$outerFrame).' '.($h + 2*$outerFrame).' F'."\n";
  80. // set the symbol color
  81. $output .= $fore_color.' setrgbcolor'."\n";
  82. // Convert the matrix into pixels
  83. for($i=0; $i<$h; $i++) {
  84. for($j=0; $j<$w; $j++) {
  85. if( $frame[$i][$j] == '1') {
  86. $y = $h - 1 - $i;
  87. $x = $j;
  88. $output .= $x.' '.$y.' 1 1 F'."\n";
  89. }
  90. }
  91. }
  92. $output .='%%EOF';
  93. return $output;
  94. }
  95. //----------------------------------------------------------------------
  96. public static function svg($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color)
  97. {
  98. $vect = self::vectSVG($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
  99. if ($filename === false) {
  100. header("Content-Type: image/svg+xml");
  101. header('Content-Disposition: filename="qrcode.svg"');
  102. return $vect;
  103. } else {
  104. if($saveandprint===TRUE){
  105. QRtools::save($vect, $filename);
  106. header("Content-Type: image/svg+xml");
  107. header('Content-Disposition: filename="'.$filename.'"');
  108. return $vect;
  109. }else{
  110. QRtools::save($vect, $filename);
  111. }
  112. }
  113. }
  114. //----------------------------------------------------------------------
  115. private static function vectSVG($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  116. {
  117. $h = count($frame);
  118. $w = strlen($frame[0]);
  119. $imgW = $w + 2*$outerFrame;
  120. $imgH = $h + 2*$outerFrame;
  121. $output =
  122. '<?xml version="1.0" encoding="utf-8"?>'."\n".
  123. '<svg version="1.1" baseProfile="full" width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" viewBox="0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint.'"
  124. xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events">'."\n".
  125. '<desc></desc>'."\n";
  126. $output =
  127. '<?xml version="1.0" encoding="utf-8"?>'."\n".
  128. '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">'."\n".
  129. '<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" xmlns:xlink="http://www.w3.org/1999/xlink" width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" viewBox="0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint.'">'."\n".
  130. '<desc></desc>'."\n";
  131. if(!empty($back_color)) {
  132. $backgroundcolor = dechex($back_color);
  133. $output .= '<rect width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" fill="#'.$backgroundcolor.'" cx="0" cy="0" />'."\n";
  134. }
  135. $output .=
  136. '<defs>'."\n".
  137. '<rect id="p" width="'.$pixelPerPoint.'" height="'.$pixelPerPoint.'" />'."\n".
  138. '</defs>'."\n".
  139. '<g fill="#'.dechex($fore_color).'">'."\n";
  140. // Convert the matrix into pixels
  141. for($i=0; $i<$h; $i++) {
  142. for($j=0; $j<$w; $j++) {
  143. if( $frame[$i][$j] == '1') {
  144. $y = ($i + $outerFrame) * $pixelPerPoint;
  145. $x = ($j + $outerFrame) * $pixelPerPoint;
  146. $output .= '<use x="'.$x.'" y="'.$y.'" xlink:href="#p" />'."\n";
  147. }
  148. }
  149. }
  150. $output .=
  151. '</g>'."\n".
  152. '</svg>';
  153. return $output;
  154. }
  155. }