OpenWrt利用luagd库生成图片验证码

OpenWrt的feeds没有luagd,需自行添加(还有luagd依赖libgd库)。软件包Makefile如下:


#
# Copyright (C) 2009-2013 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=luagd
PKG_VERSION:=2.0.33
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://github.com/ittner/lua-gd.git
PKG_SOURCE_VERSION:=e60b13b7977bb3424d7044976ccba5d42c256934
PKG_SOURCE_PROTO:=git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)

include $(INCLUDE_DIR)/package.mk

define Package/luagd
  SUBMENU:=Lua
  SECTION:=lang
  CATEGORY:=Languages
  TITLE:=LuaGD
  URL:=http://ittner.github.io/lua-gd/
  MAINTAINER:=Alexandre Erwin Ittner <alexandre@ittner.com.br>
  DEPENDS:=+lua +libgd
endef

define Package/luagd/description
  Lua-GD, the gd bindings for the Lua Programming Language.
endef

define Build/Configure
endef

define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR)/ \
		INSTALL_PATH="$(TARGET_LDFLAGS)" \
		CC="$(TARGET_CC) $(TARGET_CFLAGS) $(TARGET_CPPFLAGS) -std=gnu99" \
		LD="$(TARGET_CROSS)ld -shared" \
		all
endef


define Package/luagd/install
	$(INSTALL_DIR) $(1)/usr/lib/lua
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/gd.so $(1)/usr/lib/lua
endef

$(eval $(call BuildPackage,luagd))

luagd源码不能直接编译通过,需修改luagd源码的Makefile文件,补丁文件如下:

// 0001-mod-makefile.patch文件
--- a/Makefile	2015-01-26 14:50:03.000000000 +0800
+++ b/Makefile	2015-01-27 08:59:27.248696647 +0800
@@ -28,9 +28,6 @@
 # Lua-GD version. This one must be set.
 VERSION=2.0.33r3
 
-# Command used to run Lua code
-LUABIN=lua5.1
-
 # Path to the utility 'gdlib-config'. This may be changed to compile the
 # module with development versions of libgd.
 GDLIBCONFIG=gdlib-config
@@ -45,25 +42,15 @@
 # have these programs you must comment out these lines and uncomment and
 # change the next ones.
 
-# Name of .pc file. "lua5.1" on Debian/Ubuntu
-LUAPKG=lua5.1
 OUTFILE=gd.so
 
 CFLAGS=-O3 -Wall -fPIC $(OMITFP)
-CFLAGS+=`$(GDLIBCONFIG) --cflags` `pkg-config $(LUAPKG) --cflags`
+CFLAGS+=`$(GDLIBCONFIG) --cflags`
 CFLAGS+=-DVERSION=\"$(VERSION)\"
 
 GDFEATURES=`$(GDLIBCONFIG) --features |sed -e "s/GD_/-DGD_/g"`
 LFLAGS=-shared `$(GDLIBCONFIG) --ldflags` `$(GDLIBCONFIG) --libs` -lgd
 
-INSTALL_PATH := `$(LUABIN) -e'                          \
-    for dir in package.cpath:gmatch("(/[^?;]+)?") do    \
-        io.write(dir)                                   \
-        os.exit(0)                                      \
-    end                                                 \
-    os.exit(1)                                          \
-'`
-
 
 
 # ---------------------------------------------------------------------------
@@ -92,25 +79,22 @@
 # ---------------------------------------------------------------------------
 
 
-all: test
+all: $(OUTFILE)
 
 $(OUTFILE): gd.lo
 	$(CC) -o $(OUTFILE) gd.lo $(LFLAGS)
 
-test: $(OUTFILE)
-	$(LUABIN) test_features.lua
-
 gd.lo: luagd.c
 	$(CC) -o gd.lo -c $(GDFEATURES) $(CFLAGS) luagd.c
 
 install: $(OUTFILE)
-	install -D -s $(OUTFILE) $(DESTDIR)/$(INSTALL_PATH)/$(OUTFILE)
+	cp $(OUTFILE) $(INSTALL_PATH)/$(OUTFILE)
 
 
 # Rules for making a distribution tarball
 
 TDIR=lua-gd-$(VERSION)
-DFILES=COPYING README luagd.c lua-gd.spec Makefile test_features.lua
+DISTFILES=COPYING README luagd.c lua-gd.spec Makefile test_features.lua
 dist: $(DISTFILES)
 	rm -f $(TDIR).tar.gz
 	mkdir $(TDIR)
@@ -127,4 +111,4 @@
 	rm -rf $(TDIR) $(TDIR).tar.gz
 	rm -f demos/out.png demos/out.gif demos/counter.txt
 
-.PHONY: all test install clean dist
+.PHONY: all install clean dist




// 编译luagd软件包
./scripts/feeds update -i
./scripts/feeds install luagd
make menuconfig 在Languages-->lang-->Lua找到LuaGD选上。
make package/luagd/compile V=s

编译完成后在openwrt上安装luagd(luagd依赖libgd,libgd依赖libpng,libjpeg。。。),关于luagd的文档可以到http://ittner.github.io/lua-gd/manual.html查看。

在openwrt系统的/www/cgi-bin/下创建checkcode文件,chmod 755 checkcode 附上可执行权限。checkcode文件内容:

#!/usr/bin/lua

local gd = require("gd")
local iSeed = os.time() + os.clock() * 10000
math.randomseed(iSeed)

function CheckCode(nChars, iWidth, iHeight)
	local sTab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	local iTabLen = string.len(sTab)
	local idx, x, y, clr
	local im = gd.create(iWidth, iHeight)
	im:colorAllocate(255, 255, 255) -- background color
	for i=1, nChars do
		clr = im:colorAllocate(math.random(0, 100), math.random(0, 150), math.random(0, 200))
		idx = math.random(1, iTabLen)
		x = math.random(1, 8) + iWidth * (i-1) / 4
		y = math.random(1, iHeight / 4)
		im:string(gd.FONT_LARGE, x, y, string.sub(sTab, idx, idx), clr)
	end
	for i=1, 3 do
		clr = im:colorAllocate(math.random(0, 255), math.random(0, 255), math.random(0, 255))
		im:line(math.random(1, iWidth), math.random(1, iHeight), math.random(1, iWidth), math.random(1, iHeight), clr)
	end
	return im
end

local im = CheckCode(4, 60, 20)
print("Content-type: image/png")
print("Pragma: no-cache")
print("Expires: Thu Jan 01 00:00:00 UTC 1970")
print("")
io.write(im:pngStr())

浏览器访问 http://路由器IP/cgi-bin/checkcode即可看到效果。效果图如下:


本文章由作者:佐须之男 整理编辑,原文地址: OpenWrt利用luagd库生成图片验证码
本站的文章和资源来自互联网或者站长的原创,按照 CC BY -NC -SA 3.0 CN协议发布和共享,转载或引用本站文章应遵循相同协议。如果有侵犯版权的资 源请尽快联系站长,我们会在24h内删除有争议的资源。欢迎大家多多交流,期待共同学习进步。

相关推荐