SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/phpqrcode/qrimage.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_IMAGE', true);
  26.  
  27. class QRimage {
  28.  
  29. //----------------------------------------------------------------------
  30. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color)
  31. {
  32. $image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
  33.  
  34. if ($filename === false) {
  35. Header("Content-type: image/png");
  36. ImagePng($image);
  37. } else {
  38. if($saveandprint===TRUE){
  39. ImagePng($image, $filename);
  40. header("Content-type: image/png");
  41. ImagePng($image);
  42. }else{
  43. ImagePng($image, $filename);
  44. }
  45. }
  46.  
  47. ImageDestroy($image);
  48. }
  49.  
  50. //----------------------------------------------------------------------
  51. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
  52. {
  53. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  54.  
  55. if ($filename === false) {
  56. Header("Content-type: image/jpeg");
  57. ImageJpeg($image, null, $q);
  58. } else {
  59. ImageJpeg($image, $filename, $q);
  60. }
  61.  
  62. ImageDestroy($image);
  63. }
  64.  
  65. //----------------------------------------------------------------------
  66. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  67. {
  68. $h = count($frame);
  69. $w = strlen($frame[0]);
  70.  
  71. $imgW = $w + 2*$outerFrame;
  72. $imgH = $h + 2*$outerFrame;
  73.  
  74. $base_image =ImageCreate($imgW, $imgH);
  75.  
  76. // convert a hexadecimal color code into decimal format (red = 255 0 0, green = 0 255 0, blue = 0 0 255)
  77. $r1 = round((($fore_color & 0xFF0000) >> 16), 5);
  78. $g1 = round((($fore_color & 0x00FF00) >> 8), 5);
  79. $b1 = round(($fore_color & 0x0000FF), 5);
  80.  
  81. // convert a hexadecimal color code into decimal format (red = 255 0 0, green = 0 255 0, blue = 0 0 255)
  82. $r2 = round((($back_color & 0xFF0000) >> 16), 5);
  83. $g2 = round((($back_color & 0x00FF00) >> 8), 5);
  84. $b2 = round(($back_color & 0x0000FF), 5);
  85.  
  86.  
  87.  
  88. $col[0] = ImageColorAllocate($base_image, $r2, $g2, $b2);
  89. $col[1] = ImageColorAllocate($base_image, $r1, $g1, $b1);
  90.  
  91. imagefill($base_image, 0, 0, $col[0]);
  92.  
  93. for($y=0; $y<$h; $y++) {
  94. for($x=0; $x<$w; $x++) {
  95. if ($frame[$y][$x] == '1') {
  96. ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
  97. }
  98. }
  99. }
  100.  
  101. $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  102. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  103. ImageDestroy($base_image);
  104.  
  105. return $target_image;
  106. }
  107. }
  108.