majiameng /
QrCode-php
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Class Bar |
||
| 4 | * @author Tinymeng <[email protected]> |
||
| 5 | * @date: 2019/9/25 18:51 |
||
| 6 | */ |
||
| 7 | namespace tinymeng\code\Gateways; |
||
| 8 | use tinymeng\code\Connector\Gateway; |
||
| 9 | use tinymeng\code\Gateways\barcode\BCGColor; |
||
| 10 | use tinymeng\code\Gateways\barcode\BCGcode128; |
||
| 11 | use tinymeng\code\Gateways\barcode\BCGDrawing; |
||
| 12 | |||
| 13 | class Bar extends Gateway{ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Function Name: create |
||
| 17 | * @param $data |
||
| 18 | * @param bool $showfront 是否显示数字 |
||
| 19 | * @param bool $filePath 保存文件路径 |
||
| 20 | * @param int $size 大小 |
||
| 21 | * @param int $height 高度 |
||
| 22 | * @throws barcode\BCGArgumentException |
||
| 23 | * @throws barcode\BCGDrawException |
||
| 24 | * @author Tinymeng <[email protected]> |
||
| 25 | * @date: 2019/9/27 11:28 |
||
| 26 | */ |
||
| 27 | public function create($data,$showfront=false,$filePath=false,$size=2,$height=20){ |
||
| 28 | if (trim($data) == ''){ |
||
| 29 | throw new Exception('data 不能为空!'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | } |
||
| 31 | |||
| 32 | $colorFront = new BCGColor(0, 0, 0); |
||
| 33 | $colorBack = new BCGColor(255, 255, 255); |
||
| 34 | $code = new BCGcode128(); |
||
| 35 | $code->setScale($size);//大小 |
||
| 36 | $code->setThickness($height);//高度 |
||
| 37 | if($showfront === false){ |
||
| 38 | $code->setFont(0);//是否显示数字 |
||
| 39 | } |
||
| 40 | $code->setColor($colorFront, $colorBack); |
||
| 41 | $code->parse($data); |
||
| 42 | |||
| 43 | if($filePath){ |
||
| 44 | if($filePath === true){ |
||
|
0 ignored issues
–
show
|
|||
| 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 = 'bar'.$data.'.png'; |
||
| 58 | }else{ |
||
| 59 | $file_name = 'bar'.date('YmdHis').rand(1111,9999).'.png'; |
||
| 60 | } |
||
| 61 | $filename = $filePath.$file_name.'.png'; |
||
| 62 | $drawing = new BCGDrawing($filename, $colorBack); |
||
| 63 | $drawing->setBarcode($code); |
||
| 64 | $drawing->draw(); |
||
| 65 | $drawing->finish(BCGDrawing::IMG_FORMAT_PNG); |
||
| 66 | return $filename; |
||
| 67 | }else{ |
||
| 68 | $drawing = new BCGDrawing(false, $colorBack); |
||
| 69 | $drawing->setBarcode($code); |
||
| 70 | $drawing->draw(); |
||
| 71 | header('Content-Type: image/png'); |
||
| 72 | $drawing->finish(BCGDrawing::IMG_FORMAT_PNG); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 |