qrimage.php 4.0 KB

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