国内流行的内容管理系统(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.

98 lines
3.7KB

  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. public static $black = array(255,255,255);
  27. public static $white = array(0,0,0);
  28. //----------------------------------------------------------------------
  29. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
  30. {
  31. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  32. if ($filename === false) {
  33. Header("Content-type: image/png");
  34. ImagePng($image);
  35. } else {
  36. if($saveandprint===TRUE){
  37. ImagePng($image, $filename);
  38. header("Content-type: image/png");
  39. ImagePng($image);
  40. }else{
  41. ImagePng($image, $filename);
  42. }
  43. }
  44. ImageDestroy($image);
  45. }
  46. //----------------------------------------------------------------------
  47. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
  48. {
  49. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  50. if ($filename === false) {
  51. Header("Content-type: image/jpeg");
  52. ImageJpeg($image, null, $q);
  53. } else {
  54. ImageJpeg($image, $filename, $q);
  55. }
  56. ImageDestroy($image);
  57. }
  58. //----------------------------------------------------------------------
  59. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
  60. {
  61. $h = count($frame);
  62. $w = strlen($frame[0]);
  63. $imgW = $w + 2*$outerFrame;
  64. $imgH = $h + 2*$outerFrame;
  65. $base_image =ImageCreate($imgW, $imgH);
  66. $col[0] = ImageColorAllocate($base_image,QRImage::$black[0],QRImage::$black[1],QRImage::$black[2]);
  67. $col[1] = ImageColorAllocate($base_image,QRImage::$white[0],QRImage::$white[1],QRImage::$white[2]);
  68. imagefill($base_image, 0, 0, $col[0]);
  69. for($y=0; $y<$h; $y++) {
  70. for($x=0; $x<$w; $x++) {
  71. if ($frame[$y][$x] == '1') {
  72. ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
  73. }
  74. }
  75. }
  76. $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  77. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  78. ImageDestroy($base_image);
  79. return $target_image;
  80. }
  81. }