QrcodeUtil.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\common\util;
  3. class QrcodeUtil
  4. {
  5. /**
  6. * @param $yqcode 二维码内容
  7. * @param int $type 0=二维码输出内容 1=保存二维码到指定位置
  8. * @param string $savepath
  9. * @return bool|false|resource
  10. */
  11. static public function create($yqcode,$type=0,$savepath='',$size=6){
  12. try{
  13. //生成二维码图片
  14. $errorCorrectionLevel = 3 ;//容错级别
  15. $matrixPointSize = $size;//生成图片大小
  16. $temp = './uploads/temp/'.md5($yqcode).mt_rand(1000,9999).'.png';
  17. if($type == 1){
  18. $dirfile = dirname($savepath);
  19. if(!file_exists($dirfile)){
  20. @mkdir($dirfile,0777);
  21. }
  22. $matrixPointSize = 4;
  23. }
  24. //判断本地是否存在临时的先删了
  25. if (file_exists($temp)) {
  26. @unlink($temp);
  27. }
  28. //生成二维码图片
  29. include_once env('root_path').'/extend/phpqrcode/phpqrcode.php';
  30. \QRcode::png($yqcode, $temp, $errorCorrectionLevel, $matrixPointSize, 2);
  31. $logo = env('root_path').'/public/logo.png';
  32. if (!empty($logo)) {
  33. $QRBuffer = self::addLogoQrcode($temp, $logo);
  34. } else {
  35. $QRBuffer = imagecreatefromstring(self::print72dip300dip(file_get_contents($temp)));
  36. }
  37. //删除临时文件
  38. if (file_exists($temp)) {
  39. @unlink($temp);
  40. }
  41. if($type == 0){
  42. return $QRBuffer;
  43. }else{
  44. imagejpeg($QRBuffer,$savepath,100);
  45. ImageDestroy($QRBuffer);
  46. return true;
  47. }
  48. }catch (\Exception $e){
  49. return false;
  50. }
  51. }
  52. //添加LOGO
  53. public static function addLogoQrcode($QRTempPath, $logoPath)
  54. {
  55. $QRBuffer = imagecreatefromstring(self::print72dip300dip(file_get_contents($QRTempPath)));
  56. $logo = imagecreatefromstring(self::print72dip300dip(file_get_contents($logoPath)));
  57. $QR_width = imagesx($QRBuffer);//二维码图片宽度
  58. $QR_height = imagesy($QRBuffer);//二维码图片高度
  59. $logo_width = imagesx($logo);//logo图片宽度
  60. $logo_height = imagesy($logo);//logo图片高度
  61. $logo_qr_width = $QR_width / 5;
  62. $scale = $logo_width/$logo_qr_width;
  63. $logo_qr_height = $logo_height/$scale;
  64. $from_width = ($QR_width - $logo_qr_width) / 2;
  65. //重新组合图片并调整大小
  66. imagecopyresampled(
  67. $QRBuffer,
  68. $logo,
  69. $from_width,
  70. $from_width,
  71. 0,
  72. 0,
  73. $logo_qr_width,
  74. $logo_qr_height,
  75. $logo_width,
  76. $logo_height
  77. );
  78. return $QRBuffer;
  79. }
  80. //浏览器默认输出为72DPI,打印要求为300DPI
  81. public static function print72dip300dip($QRBufferStr)
  82. {
  83. //数据块长度为9
  84. $len = pack("N", 9);
  85. //数据块类型标志为pHYs
  86. $sign = pack("A*", "pHYs");
  87. //X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)
  88. $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
  89. //CRC检验码由数据块符号和数据域计算得到
  90. $checksum = pack("N", crc32($sign . $data));
  91. $phys = $len . $sign . $data . $checksum;
  92. $pos = strpos($QRBufferStr, "pHYs");
  93. if ($pos > 0) {
  94. //修改pHYs数据块
  95. $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos - 4, 21);
  96. } else {
  97. //IHDR结束位置(PNG头固定长度为8,IHDR固定长度为25)
  98. $pos = 33;
  99. //将pHYs数据块插入到IHDR之后
  100. $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos, 0);
  101. }
  102. return $QRBufferStr;
  103. }
  104. }