qrvect.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, $cmyk = false)
  28. {
  29. $vect = self::vectEPS($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color, $cmyk);
  30. if ($filename === false) {
  31. header("Content-Type: application/postscript");
  32. header('Content-Disposition: filename="qrcode.eps"');
  33. echo $vect;
  34. } else {
  35. if($saveandprint===TRUE){
  36. QRtools::save($vect, $filename);
  37. header("Content-Type: application/postscript");
  38. header('Content-Disposition: filename="qrcode.eps"');
  39. echo $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, $cmyk = false)
  47. {
  48. $h = count($frame);
  49. $w = strlen($frame[0]);
  50. $imgW = $w + 2*$outerFrame;
  51. $imgH = $h + 2*$outerFrame;
  52. if ($cmyk)
  53. {
  54. // convert color value into decimal eps format
  55. $c = round((($fore_color & 0xFF000000) >> 16) / 255, 5);
  56. $m = round((($fore_color & 0x00FF0000) >> 16) / 255, 5);
  57. $y = round((($fore_color & 0x0000FF00) >> 8) / 255, 5);
  58. $k = round(($fore_color & 0x000000FF) / 255, 5);
  59. $fore_color_string = $c.' '.$m.' '.$y.' '.$k.' setcmykcolor'."\n";
  60. // convert color value into decimal eps format
  61. $c = round((($back_color & 0xFF000000) >> 16) / 255, 5);
  62. $m = round((($back_color & 0x00FF0000) >> 16) / 255, 5);
  63. $y = round((($back_color & 0x0000FF00) >> 8) / 255, 5);
  64. $k = round(($back_color & 0x000000FF) / 255, 5);
  65. $back_color_string = $c.' '.$m.' '.$y.' '.$k.' setcmykcolor'."\n";
  66. }
  67. else
  68. {
  69. // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
  70. $r = round((($fore_color & 0xFF0000) >> 16) / 255, 5);
  71. $b = round((($fore_color & 0x00FF00) >> 8) / 255, 5);
  72. $g = round(($fore_color & 0x0000FF) / 255, 5);
  73. $fore_color_string = $r.' '.$b.' '.$g.' setrgbcolor'."\n";
  74. // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
  75. $r = round((($back_color & 0xFF0000) >> 16) / 255, 5);
  76. $b = round((($back_color & 0x00FF00) >> 8) / 255, 5);
  77. $g = round(($back_color & 0x0000FF) / 255, 5);
  78. $back_color_string = $r.' '.$b.' '.$g.' setrgbcolor'."\n";
  79. }
  80. $output =
  81. '%!PS-Adobe EPSF-3.0'."\n".
  82. '%%Creator: PHPQrcodeLib'."\n".
  83. '%%Title: QRcode'."\n".
  84. '%%CreationDate: '.date('Y-m-d')."\n".
  85. '%%DocumentData: Clean7Bit'."\n".
  86. '%%LanguageLevel: 2'."\n".
  87. '%%Pages: 1'."\n".
  88. '%%BoundingBox: 0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint."\n";
  89. // set the scale
  90. $output .= $pixelPerPoint.' '.$pixelPerPoint.' scale'."\n";
  91. // position the center of the coordinate system
  92. $output .= $outerFrame.' '.$outerFrame.' translate'."\n";
  93. // redefine the 'rectfill' operator to shorten the syntax
  94. $output .= '/F { rectfill } def'."\n";
  95. // set the symbol color
  96. $output .= $back_color_string;
  97. $output .= '-'.$outerFrame.' -'.$outerFrame.' '.($w + 2*$outerFrame).' '.($h + 2*$outerFrame).' F'."\n";
  98. // set the symbol color
  99. $output .= $fore_color_string;
  100. // Convert the matrix into pixels
  101. for($i=0; $i<$h; $i++) {
  102. for($j=0; $j<$w; $j++) {
  103. if( $frame[$i][$j] == '1') {
  104. $y = $h - 1 - $i;
  105. $x = $j;
  106. $output .= $x.' '.$y.' 1 1 F'."\n";
  107. }
  108. }
  109. }
  110. $output .='%%EOF';
  111. return $output;
  112. }
  113. //----------------------------------------------------------------------
  114. public static function svg($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color)
  115. {
  116. $vect = self::vectSVG($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
  117. if ($filename === false) {
  118. header("Content-Type: image/svg+xml");
  119. //header('Content-Disposition: attachment, filename="qrcode.svg"');
  120. echo $vect;
  121. } else {
  122. if($saveandprint===TRUE){
  123. QRtools::save($vect, $filename);
  124. header("Content-Type: image/svg+xml");
  125. //header('Content-Disposition: filename="'.$filename.'"');
  126. echo $vect;
  127. }else{
  128. QRtools::save($vect, $filename);
  129. }
  130. }
  131. }
  132. //----------------------------------------------------------------------
  133. private static function vectSVG($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  134. {
  135. $h = count($frame);
  136. $w = strlen($frame[0]);
  137. $imgW = $w + 2*$outerFrame;
  138. $imgH = $h + 2*$outerFrame;
  139. $output =
  140. '<?xml version="1.0" encoding="utf-8"?>'."\n".
  141. '<svg version="1.1" baseProfile="full" width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" viewBox="0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint.'"
  142. 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".
  143. '<desc></desc>'."\n";
  144. $output =
  145. '<?xml version="1.0" encoding="utf-8"?>'."\n".
  146. '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">'."\n".
  147. '<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".
  148. '<desc></desc>'."\n";
  149. if(!empty($back_color)) {
  150. $backgroundcolor = str_pad(dechex($back_color), 6, "0", STR_PAD_LEFT);
  151. $output .= '<rect width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" fill="#'.$backgroundcolor.'" cx="0" cy="0" />'."\n";
  152. }
  153. $output .=
  154. '<defs>'."\n".
  155. '<rect id="p" width="'.$pixelPerPoint.'" height="'.$pixelPerPoint.'" />'."\n".
  156. '</defs>'."\n".
  157. '<g fill="#'.str_pad(dechex($fore_color), 6, "0", STR_PAD_LEFT).'">'."\n";
  158. // Convert the matrix into pixels
  159. for($i=0; $i<$h; $i++) {
  160. for($j=0; $j<$w; $j++) {
  161. if( $frame[$i][$j] == '1') {
  162. $y = ($i + $outerFrame) * $pixelPerPoint;
  163. $x = ($j + $outerFrame) * $pixelPerPoint;
  164. $output .= '<use x="'.$x.'" y="'.$y.'" xlink:href="#p" />'."\n";
  165. }
  166. }
  167. }
  168. $output .=
  169. '</g>'."\n".
  170. '</svg>';
  171. return $output;
  172. }
  173. }