Skip to content

Commit

Permalink
迁移qrcode插件 #2028
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Nov 20, 2016
0 parents commit abc520c
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
35 changes: 35 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
language: php

php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- hhvm
- nightly

matrix:
fast_finish: true
allow_failures:
- php: hhvm
- php: nightly

services:
- memcached

before_install:
- phpenv config-add tests/travis.ini

install:
- composer install --no-interaction

script:
- phpunit --verbose --stderr --coverage-clover build/logs/clover.xml

after_success:
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
- travis_retry php coveralls.phar -v --exclude-no-stmt

notifications:
webhooks:
- http://build.miaoxingyun.com/travis-notifications
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 二维码

[![Build Status](https://img.shields.io/travis/miaoxing/qrcode/master.svg?style=flat-square)](https://travis-ci.org/miaoxing/qrcode)
[![Coverage Status](https://img.shields.io/coveralls/miaoxing/qrcode.svg?style=flat-square)](https://coveralls.io/r/miaoxing/qrcode?branch=master)
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://www.opensource.org/licenses/MIT)
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "miaoxing/qrcode",
"description": "Miaoxing QR Code Plugin",
"keywords": [
"miaoxing", "qrcode"
],
"license": "MIT",
"require": {
"miaoxing/app": "dev-master@dev",
"t0k4rt/phpqrcode": "dev-master@dev"
},
"autoload": {
"psr-4": {
"Miaoxing\\Qrcode\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"MiaoxingTest\\Qrcode\\": "tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": [
{
"type": "package",
"package": {
"name": "t0k4rt/phpqrcode",
"version": "dev-master",
"source": {
"url": "https://github.com/t0k4rt/phpqrcode",
"type": "git",
"reference": "origin/master"
},
"autoload": {
"classmap": ["phpqrcode.php"]
}
}
}
]
}
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>

<phpunit backupGlobals="false"
colors="true"
bootstrap="vendor/miaoxing/plugin/tests/bootstrap.php"
>

<testsuites>
<testsuite name="Default Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
<directory>resource</directory>
</whitelist>
</filter>
</phpunit>
105 changes: 105 additions & 0 deletions src/Controller/Qrcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Miaoxing\Qrcode\Controller;

class Qrcode extends \miaoxing\plugin\BaseController
{
protected $guestPages = ['qrcode/show'];

public function showAction($req)
{
$validator = wei()->validate([
'data' => $req,
'rules' => [
'text' => [

]
],
'names' => [
'text' => '文本内容'
]
]);
if (!$validator->isValid()) {
return $this->err($validator->getFirstMessage());
}

// 文本内容
$text = $req['text'] ?: '';

// 相对大小,内容越多,图片越大
$size = $req['size'] ?: 3;

// 精度越高,图片越大,识别率越高,取值为0 - 4
$level = $req['level'] ?: 3;

$logo = $req['logo'];

$logoSize = $req['logoSize'] ?: 30;

// 生成二维码图片资源对象
$enc = \QRencode::factory($level, $size, 0);
$tab = $enc->encode($text);
$maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)));
$image = \QRimage::image($tab, min($size, $maxSize), 0);

// TODO 支持远程图片
// 将LOGO放置到图片中间
/*if ($logo && substr($logo, 0, 4) != 'http') {
if (!file_exists($logo)) {
return $this->err('图片无效或者不存在');
}
$logoImg = null;
switch (strtolower(substr($logo, strrpos($logo, '.') + 1))) {
case 'jpg':
$logoImg = imagecreatefromjpeg($logo);
break;
case 'png':
$logoImg = imagecreatefrompng($logo);
break;
case 'gif':
$logoImg = imagecreatefromgif($logo);
break;
default:
return $this->err('不支持该图片类型');
}
$bgX = imagesx($image);
$bgY = imagesy($image);
$logoX = imagesx($logoImg);
$logoY = imagesy($logoImg);
// 缩放logo
$tmpImg = imagecreatetruecolor($logoSize, $logoSize);
imagecopyresampled($tmpImg, $logoImg, 0, 0, 0, 0, $logoSize, $logoSize, $logoX, $logoY);
imagecopyresized($image, $tmpImg, ($bgX - $logoSize) / 2, ($bgY - $logoSize) / 2, 0, 0, $logoSize, $logoSize, $logoSize, $logoSize);
imagedestroy($logoImg);
imagedestroy($tmpImg);
}*/

// 生成图片内容
ob_start();
imagepng($image);
$content = ob_get_clean();
imagedestroy($image);

if (!$req['download']) {
// 展示图片
$this->response->setHeader('Content-type', 'image/png');
} else {
// 下载图片
$this->response->setHeader(array(
'Content-Description' => 'File Transfer',
'Content-Type' => 'application/x-download',
'Content-Disposition' => 'attachment;filename=qrcode.png',
'Content-Transfer-Encoding' => 'binary',
'Expires' => '0',
'Cache-Control' => 'must-revalidate',
'Pragma' => 'public',
'Content-Length' => strlen($content),
));
}

return $content;
}
}
10 changes: 10 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Miaoxing\Qrcode;

class Plugin extends \miaoxing\plugin\BasePlugin
{
public $name = '二维码';

public $description = '通过URL参数生成二维码';
}
9 changes: 9 additions & 0 deletions tests/Controller/QrcodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace MiaoxingTest\Qrcode\Controller;

use Miaoxing\Plugin\Test\BaseControllerTestCase;

class QrcodeTest extends BaseControllerTestCase
{
}
2 changes: 2 additions & 0 deletions tests/travis.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
date.timezone = "Asia/Shanghai"
extension="memcached.so"

0 comments on commit abc520c

Please sign in to comment.