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

资源魔 39 0

将二维码天生到布景图片上,而且文字天生到布景图上,能够有限制扩增

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);
    	}
    	/**
	     * 天生二维码
	     * @param  [type]  $website  [天生二维码地点]
	     * @param  string  $filename [天生二维路经称号]
	     * @param  string  $level    [这个参数可通报的值辨别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%),这个参数管制二维码容错率,没有同的参数示意二维码可被笼罩的区域百分比,也就是被笼罩的区域还能辨认;]
	     * @param  integer $size     [管制天生图片的巨细,默许为4]
	     * @param  integer $margin [管制天生二维码的空缺区域巨细]
	     * @return [type]            [description]
	     */
	      public function qrencode($website, $filename = false, $level = "L", $size = 4, $margin = 2)
	      {
	      		include "./phpqrcode/qrlib.php";
        			QRcode::png($website, $filename, $level, $size, 2);
	      }
	      /**
	     * 把二维码图片天生到布景图片上及文字
	     * @param  string  $source      布景图片
	     * @param  string  $text1       文字形容
	     * @param  string  $child1      二维码图
	     * @param  integer $textwidth   文字横向地位
	     * @param  integer $textherght  文字高度
	     * @param  integer $$fontSize   字体巨细
	     * @param  integer $cate1,$cate2,$cate3 颜色表
	     * @param  string $font         文字字体
	     * @return [type]               [description]
	     */
	     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;
	     }
	      /**
	     * [generateImg description]
	     * @param  string  $source        布景图片
	     * @param  string  $codeurl       二维码图片
	     * @param  integer  $sourcewidth  二维码横向所正在地位
	     * @param  integer  $sourceheight 二维码高度地位
	     * @param  integer $codewidth    二维码宽度
	     * @param  integer $codeheight   二维码高度
	     * @return [type]                [description]
	     */
	      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.百度.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 "<img src='" . $generateImg . "'>";
	
	``		

更多相干常识,请拜访PHP中文网!

以上就是教你用php将二维码以及文字连系到一个布景图片上!的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!