nginx的配置
server {
listen 80;
server_name xxx;
root /home/websites/xxx/htodcs;
default_type image/jpeg;
#include /etc/nginx/http_user_agent.conf;
location ~ /img(1|2|3)/M00 {
default_type image/jpeg;
include proxy_params;
proxy_pass http://192.168.0.101;
#access_log /home/logs/nginx/img1.biz72img.com/access.log access;
}
location /get {
content_by_lua_file /home/websites/xxxx/lua/get_img.lua;
access_log /home/logs/nginx/xxxx/access.log access;
}
error_log /home/logs/nginx/xxx/error.log;
}
----------------------------------------------------------------------------------------
lua代码:
--ngx.header["Content-Type"] = 'image/jpeg'
local args = ngx.req.get_uri_args()
res = ngx.location.capture(args["f"])
local src = "">
--图片是坏的就输出默认图片
if type(src) == "nil" then
res = ngx.location.capture('/default.jpg')
src = ""
end
--没有定义宽和高 输出原图
if type(args["w"]) == "nil" and type(args["h"]) == "nil" then
ngx.say(res.body)
ngx.exit(200)
elseif type(args["h"]) == "nil" then --如果没有定义高度 那么根据宽生成图片
--如果宽大于原图的宽 就输出原图
if tonumber(args["w"]) > src:sizeX() then
ngx.say(res.body)
ngx.exit(200)
else --否则 按原图比例缩放
args["h"] = (src:sizeY() * args["w"])/src:sizeX()
end
elseif type(args["w"]) == "nil" then --如果没有定义宽度 那么根据高度生成图片
--如果高大于原图的高 就输出原图
if tonumber(args["h"]) > src:sizeY() then
ngx.say(res.body)
ngx.exit(200)
else --否则 按原图比例缩放
args["w"] = (src:sizeX() * args["h"])/src:sizeY()
end
end
local im = gd.createTrueColor(args["w"], args["h"])
gd.copyResized(im, src , 0, 0, 0, 0, args["w"], args["h"], src:sizeX(), src:sizeY())
ngx.say(im:jpegStr(100))
ngx.exit(200)
On Thursday, October 18, 2012 11:54:46 PM UTC+8, liseen wrote:
Hi, all
先用curl等工具测试, 看请求是否有返回?
如果返回正确, 设置输出的的 Content-Type Header 试一试?
另外, 请把更详细的配置贴上来, 现在都不知道你的这段lua是在那个阶段执行的, 并且你是如何输出图片的
liseen
2012/10/18 kobeng
<benw...@gmail.com>
大家好:
我想问问,我现在从磁盘拿出a图片是可以通过nginx显示,但是通过lua gd库加载这张图片,会出现加载不了,我想问问是什么原因?
lua gd 库 :http://luaforge.net/projects/lua-gd/
lua加载图片程序为
local args = ngx.req.get_uri_args()
res = ngx.location.capture(args["f"])
local src = "">
--图片是坏的就输出默认图片
if type(src) == "nil" then
res = ngx.location.capture('/default.jpg')
src = "">
end
------------------------------------------------------------------
1.有些图片是没有问题,type(src) 不等于nil
2.有些图片type==nil,但是nginx从磁盘直接读取原图是可以显示(不经过lua gd)。
我想问问,是哪里的问题呢?