基于 OpenResty 的二维码生成方案,openresty生成方案


做一个简单二维码生成API,先找找可用的轮子 lua-resty-QRcode 还有 qrencode 。 下面是安装和测试,第二个方案安装和使用更为简单一些,所以使用 qrencode 方案,可以给有兴趣的朋友一些启发。

这两种方案都依赖 libqrencode 还有 libpng , 大概的使用流程是 安装这两个依赖,然后编译上面2个库中的c文件成动态库,openresty中lua调用动态库完成操作。

假设openresty都已经安装好了,我这里是mac上 1.9.7 的环境

安装依赖

libqrencode, libpng 可以源码安装。

ubuntu

sudo apt-get install libqrencode-dev libpng12-dev

CentOS

  • https://github.com/harrisj/qrencoder/wiki/qrencode-Dependency-installation-on-CentOS-(RHEL) 很不幸,这里提到了repo已经不在维护了。
yum install libpng-devel

#centos7 找个一个包
wget http://ftp.riken.jp/Linux/centos/7/os/x86_64/Packages/qrencode-devel-3.4.1-3.el7.x86_64.rpm
rpm -ivh qrencode-devel-3.4.1-3.el7.x86_64.rpm 

MacOS

brew install libqrencode

自动安装 libpng ,很简单呀

安装lua拓展

!!为了openresty中使用方面一些从新写了一个makefile,放到 https://github.com/orangle/lua-resty-qrencode 上了,可以用这个方案,不是mac系统记得修改 Makefile

git clone https://github.com/vincascm/qrencode.git

## 它自己的makefile是使用luarocks安装,这里想自己编译成lua拓展
gcc -bundle -undefined dynamic_lookup -lpng -lqrencode -I/usr/local/openresty/luajit/include/luajit-2.1/ qrencode.c -o qrencode.so
cp test/test.lua ./
/usr/local/openresty/luajit/bin/luajit test.lua

运行test.lua 可以看到有数据输出

openresty 接口编写

其实test.lua 中已经给出来使用方法了,这里就简单的贴出来代码片段和测试情况。

把lua拓展移动到 openresty lib目录下,当然可以手动指定 lua path 了,这是自己玩怎么方面怎么做了。

cp qrencode.so  /usr/local/openresty/lualib/

nginx conf 中的 lua代码

location /qrcode {
        content_by_lua_block {
            local qr = require("qrencode")
            local args = ngx.req.get_uri_args()
            local text = args.text

            if text == nil or text== "" then
                ngx.say('need a text param')
                ngx.exit(404)
            end

            ngx.say(qr {
                text=text,
                level="L",
                kanji=false,
                ansi=true,
                size=4,
                margin=2,
                symversion=0,
                dpi=78,
                casesensitive=true,
                foreground="48AF6D",
                background="3FAF6F"
            })
        }
    }

使用url测试

curl 'http://127.0.0.1:8008/qrcode?text=http://orangleliu.info'

在终端中也可以得到二维码,测试成功。 如果正式使用还需要很多细节和打磨了。

!为了openresty中使用方面一些从新写了一个makefile,放到 https://github.com/orangle/lua-resty-qrencode 上了

—更新—-

上面的代码在命令行显示是可以,但是在浏览器显示就不好使了

location /qrcode {
        content_by_lua_block {
            local qr = require("qrencode")
            local args = ngx.req.get_uri_args()
            local text = args.text

            if text == nil or text== "" then
                ngx.say('need a text param')
                ngx.exit(404)
            end

            ngx.say(qr {
                    text=text,
                    level="L",
                    kanji=false,
                    ansi=false,
                    size=4,
                    margin=2,
                    symversion=0,
                    dpi=78,
                    casesensitive=true,
                    foreground="000000",
                    background="FFFFFF"
            })
        }

        default_type image/png;
        add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";
        add_header Pragma "no-cache";
        add_header Cache-Control "no-cache, max-age=0, must-revalidate";
    }

几个点

  • ansi=false 配置为false
  • foreground,background 注意色差,黑白最容易显示
  • 还有mime以及no cache的设置

相关内容

    暂无相关文章