栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1.生成前的图片

企业微信截图_15940995574805.png

生成后的图片


企业微信截图_15940995698843.png

class codeImg
{
	private $date,$img,$main,$width,$height,$target,$white;
    	public function constr($source)
    	{
    		$this->date   = '' . date('Ymd') . '/';
	 $this->img    = $this->date . md5($source) . '.jpg';
	 $this->main   = imagecreatefromjpeg($source);
	 $this->width  = imagesx($this->main);
	 $this->height = imagesy($this->main);
	 $this->target = imagecreatetruecolor($this->width, $this->height);
	 $this->white  = imagecolorallocate($this->target, 255, 255, 255);
 		imagefill($this->target, 0, 0, $this->white);
 		imagecopyresampled($this->target, $this->main, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
    	}
    	
	      public function qrencode($website, $filename = false, $level = "L", $size = 4, $margin = 2)
	      {
	      		include "./phpqrcode/qrlib.php";
 			QRcode::png($website, $filename, $level, $size, 2);
	      }
	      
	     public function generateFont($source, $text1, $textwidth, $textherght, $fontSize = 18, $cate1 = 255, $cate2 = 250, $cate3 = 250, $font = './font/fangsong_GB2312.ttf')
	     {
	     	$this->constr($source);
 		$fontColor = imagecolorallocate($this->target, $cate1, $cate2, $cate3); //字的RGB颜色
 		$fontBox   = imagettfbbox($fontSize, 0, $font, $text1); //文字水平居中实质
 		imagettftext($this->target, $fontSize, 0, $textwidth, $textherght, $fontColor, $font, $text1);
 		$this->createImg();
 		return $this->img;
	     }
	      
	      public function generateImg($source, $codeurl, $sourcewidth, $sourceheight, $codewidth = 100, $codeheight = 100)
	      {
			$this->constr($source);
 			$child1 = imagecreatefrompng($codeurl);
 			$codewidth = $codewidth > 0 ? $codewidth :imagesx($child1);
 			$codeheight = $codeheight > 0 ? $codeheight : imagesy($child1);
 			imagecopyresampled($this->target, $child1, $sourcewidth, $sourceheight, 0, 0, $codewidth, $codeheight,imagesx($child1),imagesy($child1));
 			imagedestroy($child1);
 			$this->createImg();
 			return $this->img;
		}
		function createImg()
		{
			@mkdir('./' . $this->date);
			 imagejpeg($this->target, './' . $this->img, 95);
 			imagedestroy($this->main);
 			imagedestroy($this->target);
		}
	}
		$source= "./img/1000.jpg";
		$codeImg = new codeImg($source);
		$website = "http://www.baidu.com";
		$codeurl = "./temp/code.png";
		$codeImg->qrencode($website, $codeurl);
		#################################
		$text  = "开始了开始了hahhah呵呵";
		$textwidth    = 100;
		$textherght   = 50;
		$generateFont = $codeImg->generateFont($source, $text, $textwidth, $textherght);
		##########################################################
		$sourcewidth  = 200;
		$sourceheight = 150;
		$generateImg  = $codeImg->generateImg($generateFont, $codeurl, $sourcewidth, $sourceheight);
		echo "";
	
	``		

更多相关知识,请访问考高分网!

以上就是教你用php将二维码和文字结合到一个背景图片上!的详细内容,更多请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/262970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号