qrimage.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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)
  28. {
  29. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  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)
  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. $col[0] = ImageColorAllocate($base_image,255,255,255);
  65. $col[1] = ImageColorAllocate($base_image,0,0,0);
  66. imagefill($base_image, 0, 0, $col[0]);
  67. for($y=0; $y<$h; $y++) {
  68. for($x=0; $x<$w; $x++) {
  69. if ($frame[$y][$x] == '1') {
  70. ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
  71. }
  72. }
  73. }
  74. $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  75. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  76. ImageDestroy($base_image);
  77. return $target_image;
  78. }
  79. }