majiameng /
QrCode-php
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Class Smtp |
||
| 4 | * @author Tinymeng <[email protected]> |
||
| 5 | * @date: 2019/9/25 18:51 |
||
| 6 | */ |
||
| 7 | namespace tinymeng\code\Gateways; |
||
| 8 | use Exception; |
||
| 9 | use tinymeng\code\Gateways\qrcode\FrameFiller; |
||
| 10 | use tinymeng\code\Gateways\qrcode\Qrcode; |
||
| 11 | use tinymeng\code\Gateways\qrcode\QRencode; |
||
| 12 | use tinymeng\code\Gateways\qrcode\QRrawcode; |
||
| 13 | use tinymeng\code\Connector\Gateway; |
||
| 14 | |||
| 15 | class Qr extends Gateway{ |
||
| 16 | |||
| 17 | public $version; |
||
| 18 | public $width; |
||
| 19 | public $data; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Function Name: create |
||
| 23 | * @param $data |
||
| 24 | * @param bool $filePath 图片保存路径,false=>直接输出图片流 |
||
| 25 | * @param string $matrixPointSize |
||
| 26 | * @param string $errorCorrectionLevel in ['L','M','Q','H'] |
||
| 27 | * @return string |
||
| 28 | * @author Tinymeng <[email protected]> |
||
| 29 | * @date: 2019/9/27 11:28 |
||
| 30 | */ |
||
| 31 | public function create($data,$filePath=false,$matrixPointSize=10,$errorCorrectionLevel='L'){ |
||
| 32 | if (trim($data) == ''){ |
||
| 33 | throw new Exception('data 不能为空!'); |
||
| 34 | } |
||
| 35 | |||
| 36 | if (!in_array($errorCorrectionLevel, array('L','M','Q','H'))){ |
||
| 37 | $errorCorrectionLevel = 'L'; |
||
| 38 | } |
||
| 39 | if (!empty($matrixPointSize)){ |
||
| 40 | $matrixPointSize = min(max((int)$matrixPointSize, 1), 10); |
||
| 41 | } |
||
| 42 | |||
| 43 | if($filePath){ |
||
| 44 | if($filePath === true){ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 45 | $filePath = saveFilePath; |
||
| 46 | }else{ |
||
| 47 | if(substr($filePath,-1) != DIRECTORY_SEPARATOR){ |
||
| 48 | $filePath .= DIRECTORY_SEPARATOR; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | /** 文件是否存在 */ |
||
| 52 | if (!is_dir($filePath)) { |
||
| 53 | mkdir($filePath, 0777, true); |
||
| 54 | } |
||
| 55 | |||
| 56 | if(is_int($data)){ |
||
| 57 | $file_name = 'qr'.$data.'.png'; |
||
| 58 | }else{ |
||
| 59 | $file_name = 'qr'.date('YmdHis').rand(1111,9999).'.png'; |
||
| 60 | } |
||
| 61 | $filename = $filePath.$file_name; |
||
| 62 | QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2); |
||
| 63 | return $filename; |
||
| 64 | }else{ |
||
| 65 | QRcode::png($data, false, $errorCorrectionLevel, $matrixPointSize, 2); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 |