yii 整合phpqrcode產生二維碼附線上示範地址,yiiphpqrcode
1,先到官網下載包 http://phpqrcode.sourceforge.net/
下載官網提供的類庫後,只需要使用phpqrcode.php就可以產生二維碼了,當然您的PHP環境必須開啟支援GD2。
phpqrcode.php提供了一個關鍵的png()方法,其中
參數$text表示產生二位的的資訊文本;
參數$outfile表示是否輸出二維碼圖片 檔案,預設否;
參數$level表示容錯率,也就是有被覆蓋的地區還能識別,分別是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%);
參數$size表示產生圖片大小,預設是3;參數$margin表示二維碼周圍邊框空白地區間距值;
參數$saveandprint表示是否儲存二維碼並顯示。
2,下載後把解壓後的phpqrcode檔案夾放到extensions檔案夾下,如:
3,引入類 phpqrcode
Yii::$enableIncludePath = false;Yii::import ('application.extensions.phpqrcode.phpqrcode', 1 );
下面是完整的產生二維碼的方法
public function actionQrcode(){$this->breadcrumbs=array_merge($this->breadcrumbs,array('產生二維碼'));$qrcode_path='';$file_tmp_name='';$errors=array();if(!empty($_POST)){$content = trim($_POST['content']); //二維碼內容$contentSize=$this->getStringLength($content);if($contentSize>290){$errors[]='字數過長,不能多於150個字元!';}Yii::$enableIncludePath = false;Yii::import ('application.extensions.phpqrcode.phpqrcode', 1 );if(isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])){if($_FILES['upimage']['size']>512000){$errors[]="你上傳的檔案過大,最大不能超過500K。";}$file_tmp_name=$_FILES['upimage']['tmp_name'];$fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png");if(!in_array($_FILES['upimage']['type'],$fileext)){$errors[]="你上傳的檔案格式不正確,僅支援 png, jpg, gif格式。";}}$tpgs=$_POST['tpgs'];//圖片格式$bas_path=dirname ( Yii::app ()->BasePath );$qrcode_bas_path=$bas_path.'/upload/qrcode/';if(!is_dir($qrcode_bas_path)){mkdir($qrcode_bas_path, 0777, true);}$uniqid_rand=date("Ymdhis").uniqid(). rand(1,1000);$qrcode_path=$qrcode_bas_path.$uniqid_rand. "_1.".$tpgs;$qrcode_path_new=$qrcode_bas_path.$uniqid_rand."_2.".$tpgs;if(Helper::getOS()=='Linux'){$mv = move_uploaded_file($file_tmp_name, $qrcode_path);}else{//解決windows下中文檔案名稱亂碼的問題$save_path = Helper::safeEncoding($qrcode_path,'GB2312');if(!$save_path){$errors[]='上傳失敗,請重試!';}$mv = move_uploaded_file($file_tmp_name, $qrcode_path);}if(empty($errors)){$errorCorrectionLevel = $_POST['errorCorrectionLevel'];//容錯層級$matrixPointSize = $_POST['matrixPointSize'];//產生圖片大小$matrixMarginSize = $_POST['matrixMarginSize'];//邊距大小//產生二維碼圖片QRcode::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);$QR = $qrcode_path_new;//已經產生的原始二維碼圖$logo = $qrcode_path;//準備好的logo圖片if (file_exists($logo)) {$QR = imagecreatefromstring(file_get_contents($QR));$logo = imagecreatefromstring(file_get_contents($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);//輸出圖片// header("Content-type: image/png");imagepng($QR,$qrcode_path);imagedestroy($QR);}else{$qrcode_path=$qrcode_path_new;}$qrcode_path=str_replace($bas_path,'', $qrcode_path);}else{$qrcode_path='';}}$data=array('data'=>array('errors'=>$errors,'qrcode_path'=>$qrcode_path));$this->render ( 'qrcode',$data);}
前台的上傳介面:
request->hostInfo;$matrixPointSize=6;$matrixMarginSize=2;$errorCorrectionLevel='M';$tpgs='gif';if(!empty($_POST)){$content=$_POST['content'];$matrixPointSize=$_POST['matrixPointSize'];$matrixMarginSize=$_POST['matrixMarginSize'];$errorCorrectionLevel=$_POST['errorCorrectionLevel'];$tpgs=$_POST['tpgs'];}$arrayCorrectionLevel=array('L'=>'L - Low (7%)','M'=>'M - Medium (15%)','Q'=>'Q - Quartile (25%)','H'=>'H - High (30%)');$arrayTpgs=array('gif'=>'gif格式','png'=>'png格式','jpg格式');?>線上產生二維碼beginWidget ( 'CActiveForm', array ('id' => 'qrcode-form','htmlOptions' => array ('id' => 'view_table','class' => 'add-form padding-10','enctype' => 'multipart/form-data'),'enableAjaxValidation' => false) );?>尺寸大小 > 邊距大小 > 容錯層級'form-control'));?>儲存格式'form-control'));?>二維碼內容'form-control','maxlength'=>150));?>二維碼logo圖片選擇檔案endWidget(); ?>二維碼產生失敗
" target="_blank">右鍵另存新檔二維碼renderPartial('/component/duoshuo_common');?>
示範地址
http://www.bkjia.com/PHPjc/1070041.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1070041.htmlTechArticleyii 整合phpqrcode產生二維碼附線上示範地址,yiiphpqrcode 1,先到官網下載包 http://phpqrcode.sourceforge.net/ 下載官網提供的類庫後,只需要使用p...