教你用php将二维码和文字结合到一个背景图片上!

将二维码生成到背景图片上,并且文字生成到背景图上,可以无限制扩增

1.生成前的图片

教你用php将二维码和文字结合到一个背景图片上!

生成后的图片

教你用php将二维码和文字结合到一个背景图片上!

  1. class codeImg
  2. {
  3. private $date,$img,$main,$width,$height,$target,$white;
  4. public function constr($source)
  5. {
  6. $this->date = '' . date('Ymd') . '/';
  7. $this->img = $this->date . md5($source) . '.jpg';
  8. $this->main = imagecreatefromjpeg($source);
  9. $this->width = imagesx($this->main);
  10. $this->height = imagesy($this->main);
  11. $this->target = imagecreatetruecolor($this->width, $this->height);
  12. $this->white = imagecolorallocate($this->target, 255, 255, 255);
  13. imagefill($this->target, 0, 0, $this->white);
  14. imagecopyresampled($this->target, $this->main, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
  15. }
  16. /**
  17. * 生成二维码
  18. * @param [type] $website [生成二维码地址]
  19. * @param string $filename [生成二维路经名称]
  20. * @param string $level [这个参数可传递的值分别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%),这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比,也就是被覆盖的区域还能识别;]
  21. * @param integer $size [控制生成图片的大小,默认为4]
  22. * @param integer $margin [控制生成二维码的空白区域大小]
  23. * @return [type] [description]
  24. */
  25. public function qrencode($website, $filename = false, $level = "L", $size = 4, $margin = 2)
  26. {
  27. include "./phpqrcode/qrlib.php";
  28. QRcode::png($website, $filename, $level, $size, 2);
  29. }
  30. /**
  31. * 把二维码图片生成到背景图片上及文字
  32. * @param string $source 背景图片
  33. * @param string $text1 文字描述
  34. * @param string $child1 二维码图
  35. * @param integer $textwidth 文字横向位置
  36. * @param integer $textherght 文字高度
  37. * @param integer $$fontSize 字体大小
  38. * @param integer $cate1,$cate2,$cate3 颜色表
  39. * @param string $font 文字字体
  40. * @return [type] [description]
  41. */
  42. public function generateFont($source, $text1, $textwidth, $textherght, $fontSize = 18, $cate1 = 255, $cate2 = 250, $cate3 = 250, $font = './font/fangsong_GB2312.ttf')
  43. {
  44. $this->constr($source);
  45. $fontColor = imagecolorallocate($this->target, $cate1, $cate2, $cate3); //字的RGB颜色
  46. $fontBox = imagettfbbox($fontSize, 0, $font, $text1); //文字水平居中实质
  47. imagettftext($this->target, $fontSize, 0, $textwidth, $textherght, $fontColor, $font, $text1);
  48. $this->createImg();
  49. return $this->img;
  50. }
  51. /**
  52. * [generateImg description]
  53. * @param string $source 背景图片
  54. * @param string $codeurl 二维码图片
  55. * @param integer $sourcewidth 二维码横向所在位置
  56. * @param integer $sourceheight 二维码高度位置
  57. * @param integer $codewidth 二维码宽度
  58. * @param integer $codeheight 二维码高度
  59. * @return [type] [description]
  60. */
  61. public function generateImg($source, $codeurl, $sourcewidth, $sourceheight, $codewidth = 100, $codeheight = 100)
  62. {
  63. $this->constr($source);
  64. $child1 = imagecreatefrompng($codeurl);
  65. $codewidth = $codewidth > 0 ? $codewidth :imagesx($child1);
  66. $codeheight = $codeheight > 0 ? $codeheight : imagesy($child1);
  67. imagecopyresampled($this->target, $child1, $sourcewidth, $sourceheight, 0, 0, $codewidth, $codeheight,imagesx($child1),imagesy($child1));
  68. imagedestroy($child1);
  69. $this->createImg();
  70. return $this->img;
  71. }
  72. function createImg()
  73. {
  74. @mkdir('./' . $this->date);
  75. imagejpeg($this->target, './' . $this->img, 95);
  76. imagedestroy($this->main);
  77. imagedestroy($this->target);
  78. }
  79. }
  80. $source = "./img/1000.jpg";
  81. $codeImg = new codeImg($source);
  82. $website = "http://www.baidu.com";
  83. $codeurl = "./temp/code.png";
  84. $codeImg->qrencode($website, $codeurl);
  85. #################################
  86. $text = "开始了开始了hahhah呵呵";
  87. $textwidth = 100;
  88. $textherght = 50;
  89. $generateFont = $codeImg->generateFont($source, $text, $textwidth, $textherght);
  90. ##########################################################
  91. $sourcewidth = 200;
  92. $sourceheight = 150;
  93. $generateImg = $codeImg->generateImg($generateFont, $codeurl, $sourcewidth, $sourceheight);
  94. echo "<img src='" . $generateImg . "'>";
  95. ``