phpQrcode生成支付二維碼logo

一、主要功能:
1,利用phpQrcode生成二維碼。
2,將二維碼中間合併logo。
3,生成的二維碼圖片可以不指定具體的路徑存儲。

二、效果:



三、注意

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;
		}
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章