天天看点

phpQrcode生成支付二维码logo

一、主要功能:

1,利用phpQrcode生成二维码。

2,将二维码中间合并logo。

3,生成的二维码图片可以不指定具体的路径存储。

二、效果:

phpQrcode生成支付二维码logo

三、注意

1,logo图片四周最好是圆角,并且四周最好有一圈空白,这样展示效果比较好。

2,logo图片不宜过大,否则二维码扫不出来。

<?php
	class Dg_Helper_Qrcode
	{
		/**
		 * 生成二维码,参数同phpQrcode,注意增加了第二个参数
		 * @param  string  $text         生成的数据
		 * @param  string  $logo         合并的logo
		 * @return
		 */
		static function png($text, $logo = false, $outfile = false, $level = 'H', $size = 5, $margin = 4, $saveandprint=false)
		{
			include "phpQrcode/qrlib.php";
			if ( $outfile ) {
				QRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
				$QR = file_get_contents($outfile);
			} else {
				ob_start();
				$QR = QRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
				$QR = ob_get_contents();
				ob_end_clean();
			}

			//合并logo
			if ( $logo !== false && $logo = file_get_contents($logo) ) {
				$QR = imagecreatefromstring($QR);
				$logo = imagecreatefromstring($logo);

				$QR_width    = imagesx($QR);//二维码图片宽度
				$QR_height   = imagesy($QR);//二维码图片高度
				$logo_width  = imagesx($logo);//logo图片宽度
				$logo_height = imagesy($logo);//logo图片高度

				//重新组合图片并调整大小
				// $logo_qr_width  = $QR_width / 5;
				// $scale          = $logo_width/$logo_qr_width;
				// $logo_qr_height = $logo_height/$scale;
				// $from_width     = ($QR_width - $logo_qr_width) / 2;
				// imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
				
				//logo不失真
				$dstX = $QR_width/2 - $logo_width/2;
				$dstY = $QR_width/2 - $logo_width/2;
				imagecopyresampled($QR, $logo, $dstX, $dstY, 0, 0, $logo_width, $logo_height, $logo_width, $logo_height);

				if ( $outfile ) {
					imagepng($QR, $outfile);
				} else {
					ob_start();
	                imagepng($QR);
	                $QR =  ob_get_contents();
	                ob_end_clean();
	            }
			}
			
			return $QR;
		}
	}