PHP根据指定url生成二维码图片
生活随笔
收集整理的這篇文章主要介紹了
PHP根据指定url生成二维码图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、composer安裝
http://packagist.p2hp.com/packages/codeitnowin/barcode
?
二、使用
調(diào)用generateQrCode()方法即可實(shí)現(xiàn)生成二維碼圖片并輸出下載給用戶
<?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){// 進(jìn)行基礎(chǔ)定義$url = 'http://www.xiaobudiu.top?uid=' . $uid;//定義需要生成二維碼圖片的url$out_put_file_name = '小卜丟飯團(tuán)子'.date('His');//定義生成的二維碼圖片名稱$tmp_path = \Yii::$app->basePath.'/runtime/tmp_file/';// 定義存儲(chǔ)臨時(shí)文件的臨時(shí)文件夾路徑// 判斷如果臨時(shí)文件夾不存在,則創(chuàng)建臨時(shí)文件夾(用于存儲(chǔ)生成的臨時(shí)二維碼圖片)$generate_res = $this->generateDir($tmp_path);if (!$generate_res) {return Helper::msg('0', '創(chuàng)建圖片臨時(shí)文件夾失敗');}// 根據(jù)url生成二維碼圖片base64 str$image_base64_str = $this->generateImgBase64Str($url);// 將base64轉(zhuǎn)圖片格式,并將圖片存儲(chǔ)到指定文件夾(返回值為圖片的絕對(duì)路徑)$file = $this->base64_to_img($image_base64_str, $tmp_path, 'admin_');if (!$file) {return Helper::msg('0', '生成二維碼圖片失敗');}// 設(shè)置請(qǐng)求頭header('Content-Disposition:attachment;filename='.$out_put_file_name.'.png');header('Content-Length:' . filesize($file));// 讀取文件并寫入到輸出緩沖readfile($file);// 為了節(jié)省本地資源,刪除臨時(shí)圖片(可選)unlink($file);}/*** 判斷目錄是否存在,如果不存在,則創(chuàng)建,存在則返回* @param $path* @return mixed* @throws \yii\base\Exception*/public static function generateDir($path){if (FileHelper::createDirectory($path, 0775, true)) {return $path;}return false;}/*** 根據(jù)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轉(zhuǎn)圖片格式,并將圖片存儲(chǔ)到指定文件夾*/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的同學(xué)參考 -------------------------/*** yii框架中的創(chuàng)建文件夾方法,即上面調(diào)用的同名方法* @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、效果
瀏覽器直接進(jìn)行下載:
打開下載的圖片,效果:
?
總結(jié)
以上是生活随笔為你收集整理的PHP根据指定url生成二维码图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日一言学做人,古之学问,博大精深
- 下一篇: PHP格式化全国省市区列表