一、composer安裝
http://packagist.p2hp.com/packages/codeitnowin/barcode
?
二、使用
調用generateQrCode()方法即可實現生成二維碼圖片并輸出下載給用戶
<?php
namespace manage\Test;use CodeItNow\BarcodeBundle\Utils\QrCode;
use common\extensions\Helper;
use yii\helpers\FileHelper;class TestService
{/*** 生成二維碼圖片* @return array* @throws \yii\base\Exception*/public function generateQrCode($uid = 400570){// 進行基礎定義$url = 'http://www.xiaobudiu.top?uid=' . $uid;//定義需要生成二維碼圖片的url$out_put_file_name = '小卜丟飯團子'.date('His');//定義生成的二維碼圖片名稱$tmp_path = \Yii::$app->basePath.'/runtime/tmp_file/';// 定義存儲臨時文件的臨時文件夾路徑// 判斷如果臨時文件夾不存在,則創建臨時文件夾(用于存儲生成的臨時二維碼圖片)$generate_res = $this->generateDir($tmp_path);if (!$generate_res) {return Helper::msg('0', '創建圖片臨時文件夾失敗');}// 根據url生成二維碼圖片base64 str$image_base64_str = $this->generateImgBase64Str($url);// 將base64轉圖片格式,并將圖片存儲到指定文件夾(返回值為圖片的絕對路徑)$file = $this->base64_to_img($image_base64_str, $tmp_path, 'admin_');if (!$file) {return Helper::msg('0', '生成二維碼圖片失敗');}// 設置請求頭header('Content-Disposition:attachment;filename='.$out_put_file_name.'.png');header('Content-Length:' . filesize($file));// 讀取文件并寫入到輸出緩沖readfile($file);// 為了節省本地資源,刪除臨時圖片(可選)unlink($file);}/*** 判斷目錄是否存在,如果不存在,則創建,存在則返回* @param $path* @return mixed* @throws \yii\base\Exception*/public static function generateDir($path){if (FileHelper::createDirectory($path, 0775, true)) {return $path;}return false;}/*** 根據url生成二維碼圖片base64 str*/public function generateImgBase64Str($url){$qr_code = new QrCode();$image_base64_str = $qr_code->setText($url)->setImageType(QrCode::IMAGE_TYPE_PNG)->generate();// 生成圖片base64 strreturn $image_base64_str;}/*** base64轉圖片格式,并將圖片存儲到指定文件夾*/public function base64_to_img($base64_str, $path, $prefix = null){if($base64_str != ''){$output_file_name = $prefix.time().rand(100,999).'.png';$path .= $output_file_name;$file_handle = fopen($path, "wb");fwrite($file_handle, base64_decode($base64_str));fclose($file_handle);return($path);}return false;}# -------------------------- 備注方法,給不使用yii的同學參考 -------------------------/*** yii框架中的創建文件夾方法,即上面調用的同名方法* @param $path* @param int $mode* @param bool $recursive* @return bool* @throws \yii\base\Exception*/public function createDirectory($path, $mode = 0775, $recursive = true){if (is_dir($path)) {return true;}$parentDir = dirname($path);// recurse if parent dir does not exist and we are not at the root of the file system.if ($recursive && !is_dir($parentDir) && $parentDir !== $path) {static::createDirectory($parentDir, $mode, true);}try {if (!mkdir($path, $mode)) {return false;}} catch (\Exception $e) {if (!is_dir($path)) {// throw new \yii\base\Exception("Failed to create directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);throw new \Exception("Failed to create directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);}}try {return chmod($path, $mode);} catch (\Exception $e) {// throw new \yii\base\Exception("Failed to change permissions for directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);throw new \Exception("Failed to change permissions for directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);}}
}
?
3、效果
瀏覽器直接進行下載:
打開下載的圖片,效果:
?