国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.7KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /*
  4. * PHP QR Code encoder
  5. *
  6. * Image output of code using GD2
  7. *
  8. * PHP QR Code is distributed under LGPL 3
  9. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. define('QR_IMAGE', true);
  26. class QRimage {
  27. public static $black = array(255,255,255);
  28. public static $white = array(0,0,0);
  29. //----------------------------------------------------------------------
  30. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
  31. {
  32. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  33. if ($filename === false) {
  34. Header("Content-type: image/png");
  35. ImagePng($image);
  36. } else {
  37. if ($saveandprint===TRUE){
  38. ImagePng($image, $filename);
  39. header("Content-type: image/png");
  40. ImagePng($image);
  41. } else {
  42. ImagePng($image, $filename);
  43. }
  44. }
  45. ImageDestroy($image);
  46. }
  47. //----------------------------------------------------------------------
  48. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
  49. {
  50. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  51. if ($filename === false) {
  52. Header("Content-type: image/jpeg");
  53. ImageJpeg($image, null, $q);
  54. } else {
  55. ImageJpeg($image, $filename, $q);
  56. }
  57. ImageDestroy($image);
  58. }
  59. //----------------------------------------------------------------------
  60. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
  61. {
  62. $h = count($frame);
  63. $w = strlen($frame[0]);
  64. $imgW = $w + 2*$outerFrame;
  65. $imgH = $h + 2*$outerFrame;
  66. $base_image =ImageCreate($imgW, $imgH);
  67. $col[0] = ImageColorAllocate($base_image,QRImage::$black[0],QRImage::$black[1],QRImage::$black[2]);
  68. $col[1] = ImageColorAllocate($base_image,QRImage::$white[0],QRImage::$white[1],QRImage::$white[2]);
  69. imagefill($base_image, 0, 0, $col[0]);
  70. for($y=0; $y<$h; $y++) {
  71. for($x=0; $x<$w; $x++) {
  72. if ($frame[$y][$x] == '1') {
  73. ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
  74. }
  75. }
  76. }
  77. $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  78. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  79. ImageDestroy($base_image);
  80. return $target_image;
  81. }
  82. }