当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-05 07:20:18 浏览: 评论:0 

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

1.生成前的图片

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

生成后的图片

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

  1. class codeImg 
  2.  
  3. { 
  4.  
  5.     private $date,$img,$main,$width,$height,$target,$white; 
  6.  
  7.         public function constr($source) 
  8.  
  9.         { 
  10.  
  11.             $this->date   = '' . date('Ymd') . '/'; 
  12.  
  13.             $this->img    = $this->date . md5($source) . '.jpg'; 
  14.  
  15.             $this->main   = imagecreatefromjpeg($source); 
  16.  
  17.             $this->width  = imagesx($this->main); 
  18.  
  19.             $this->height = imagesy($this->main); 
  20.  
  21.             $this->target = imagecreatetruecolor($this->width, $this->height); 
  22.  
  23.             $this->white  = imagecolorallocate($this->target, 255, 255, 255); 
  24.  
  25.                 imagefill($this->target, 0, 0, $this->white); 
  26.  
  27.                 imagecopyresampled($this->target, $this->main, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height); 
  28.  
  29.         } 
  30.  
  31.         /** 
  32.  
  33.          * 生成二维码 
  34.  
  35.          * @param  [type]  $website  [生成二维码地址] 
  36.  
  37.          * @param  string  $filename [生成二维路经名称] 
  38.  
  39.          * @param  string  $level    [这个参数可传递的值分别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%),这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比,也就是被覆盖的区域还能识别;] 
  40.  
  41.          * @param  integer $size     [控制生成图片的大小,默认为4] 
  42.  
  43.          * @param  integer $margin [控制生成二维码的空白区域大小] 
  44.  
  45.          * @return [type]            [description] 
  46.  
  47.          */ 
  48.  
  49.           public function qrencode($website, $filename = false, $level = "L", $size = 4, $margin = 2) 
  50.  
  51.           { 
  52.  
  53.                 include "./phpqrcode/qrlib.php"; 
  54.  
  55.                     QRcode::png($website, $filename, $level, $size, 2); 
  56.  
  57.           } 
  58.  
  59.           /** 
  60.  
  61.          * 把二维码图片生成到背景图片上及文字 
  62.  
  63.          * @param  string  $source      背景图片 
  64.  
  65.          * @param  string  $text1       文字描述 
  66.  
  67.          * @param  string  $child1      二维码图 
  68.  
  69.          * @param  integer $textwidth   文字横向位置 
  70.  
  71.          * @param  integer $textherght  文字高度 
  72.  
  73.          * @param  integer $$fontSize   字体大小 
  74.  
  75.          * @param  integer $cate1,$cate2,$cate3 颜色表 
  76.  
  77.          * @param  string $font         文字字体 
  78.  
  79.          * @return [type]               [description] 
  80.  
  81.          */ 
  82.  
  83.          public function generateFont($source, $text1, $textwidth, $textherght, $fontSize = 18, $cate1 = 255, $cate2 = 250, $cate3 = 250, $font = './font/fangsong_GB2312.ttf') 
  84.  
  85.          { 
  86.  
  87.             $this->constr($source); 
  88.  
  89.                 $fontColor = imagecolorallocate($this->target, $cate1, $cate2, $cate3); //字的RGB颜色 
  90.  
  91.                 $fontBox   = imagettfbbox($fontSize, 0, $font, $text1); //文字水平居中实质 
  92.  
  93.                 imagettftext($this->target, $fontSize, 0, $textwidth, $textherght, $fontColor, $font, $text1); 
  94.  
  95.                 $this->createImg(); 
  96.  
  97.                 return $this->img; 
  98.  
  99.          } 
  100.  
  101.           /** 
  102.  
  103.          * [generateImg description] 
  104.  
  105.          * @param  string  $source        背景图片 
  106.  
  107.          * @param  string  $codeurl       二维码图片 
  108.  
  109.          * @param  integer  $sourcewidth  二维码横向所在位置 
  110.  
  111.          * @param  integer  $sourceheight 二维码高度位置 
  112.  
  113.          * @param  integer $codewidth    二维码宽度 
  114.  
  115.          * @param  integer $codeheight   二维码高度 
  116.  
  117.          * @return [type]                [description] 
  118.  
  119.          */ 
  120.  
  121.           public function generateImg($source, $codeurl, $sourcewidth, $sourceheight, $codewidth = 100, $codeheight = 100) 
  122.  
  123.           { 
  124.  
  125.             $this->constr($source); 
  126.  
  127.                     $child1 = imagecreatefrompng($codeurl); 
  128.  
  129.                     $codewidth = $codewidth > 0 ? $codewidth :imagesx($child1); 
  130.  
  131.                     $codeheight = $codeheight > 0 ? $codeheight : imagesy($child1); 
  132.  
  133.                     imagecopyresampled($this->target, $child1, $sourcewidth, $sourceheight, 0, 0, $codewidth, $codeheight,imagesx($child1),imagesy($child1)); 
  134.  
  135.                     imagedestroy($child1); 
  136.  
  137.                     $this->createImg(); 
  138.  
  139.                     return $this->img; 
  140.  
  141.         } 
  142.  
  143.         function createImg() 
  144.  
  145.         { 
  146.  
  147.             @mkdir('./' . $this->date); 
  148.  
  149.                  imagejpeg($this->target, './' . $this->img, 95); 
  150.  
  151.                     imagedestroy($this->main); 
  152.  
  153.                     imagedestroy($this->target); 
  154.  
  155.         } 
  156.  
  157.     } 
  158.  
  159.         $source       = "./img/1000.jpg"; 
  160.  
  161.         $codeImg = new codeImg($source); 
  162.  
  163.         $website = "http://www.baidu.com"; 
  164.  
  165.         $codeurl = "./temp/code.png"; 
  166.  
  167.         $codeImg->qrencode($website, $codeurl); 
  168.  
  169.         ################################# 
  170.  
  171.         $text         = "开始了开始了hahhah呵呵"; 
  172.  
  173.         $textwidth    = 100; 
  174.  
  175.         $textherght   = 50; 
  176.  
  177.         $generateFont = $codeImg->generateFont($source, $text, $textwidth, $textherght); 
  178.  
  179.         ########################################################## 
  180.  
  181.         $sourcewidth  = 200; 
  182.  
  183.         $sourceheight = 150; 
  184.  
  185.         $generateImg  = $codeImg->generateImg($generateFont, $codeurl, $sourcewidth, $sourceheight); 
  186.  
  187.         echo "<img src='" . $generateImg . "'>"; 
  188.  
  189.       
  190.  
  191.     ``

Tags: php二维码 php背景图片

分享到: