SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/phpqrcode/qrvect.php

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