nginx + lua + image 업로드 이미지 재단 및 워 터 마크 구현
https://github.com/simpl/ngx_devel_kit
Lua JIT
http://luajit.org/download.html
configure \
...
--add-module=/path/lua-nginx-module \
--add-module=/path/ngx_devel_kit
make && make install
ImageMagick
http://www.p_w_picpathmagick.org/
Modify
nginx-server.conf
convert_bin
p_w_picpathmagick_install_path/bin/convert rewrite_by_lua_file
nginx-p_w_picpathmagick.lua save path if want to allow more p_w_picpath size, please modify the
p_w_picpath_sizes
variable in nginx-p_w_picpathmagick.lua server
{
listen 80;
server_name upload.qq.com;
index index.html index.htm index.php;
root /h1/upload.qq.com;
location ~ ^(.+\.php)(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fcgi.conf;
}
rewrite ^/p_w_upload/p_w_picpath/(.*).jpg$ /p w upload / p w picpath / $1. jpg! 0x0 last; \ # 처리 원본 그림 은 0x0 그림 으로 표 시 됩 니 다.
# rewrite ^/p_w_picpaths/(.*).jpg ^/p_w_picpaths/$1.jpg!0x0 last;
location /p_w_upload/p_w_picpath/ {
set $p_w_picpath_root "/htdoc/upload.qq.com";
set $file "$p_w_picpath_root$uri";
set $convert_bin "/usr/local/bin/convert";
if (!-f $file)
{
rewrite_by_lua_file /usr/local/webserver/nginx/conf/nginx-p_w_picpathmagick.lua;
}
expires max;
}
location ~ .*\.(js|css)?$
{
expires max;
}
}
다음은 lua 코드. nginx-p_w_picpathmagick.lua:
-- http://domain.com/111/photo/201411/05/5459e306820af926411357_320x320.jpg
-- config
local p_w_picpath_sizes = { "640x640", "320x320", "124x124", "140x140", "64x64", "60x60", "32x32", "0x0" }
-- parse uri
function parseUri(uri)
local _, _, name, ext, size = string.find(uri, "(.+)(%..+)!(%d+x%d+)")
--ngx.header.content_type = "text/plain";
--ngx.say(name,size);
if name and size and ext then
return ngx.var.p_w_picpath_root .. name .. ext, size
else
return "",""
end
end
function fileExists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
--ngx.header.content_type = "text/plain";
--ngx.say(name);
function sizeExists(size)
for _, value in pairs(p_w_picpath_sizes) do
if value == size then
return true
end
end
return false
end
--ngx.header.content_type = "text/plain";
--ngx.say(size);
function resize()
local ori_filename, size = parseUri(ngx.var.uri)
--ngx.header.content_type = "text/plain";
--ngx.say(ori_filename,size);
if fileExists(ori_filename) == false or sizeExists(size) == false then
ngx.exit(404)
end
local command = '';
if size == '0x0' then
command = table.concat({
ngx.var.convert_bin,
ori_filename,
"/usr/local/webserver/nginx/conf/logo.png",
"-gravity southeast -geometry +5+10 -composite",
ngx.var.file,
}, " ")
else
command = table.concat({
ngx.var.convert_bin,
ori_filename,
"/usr/local/webserver/nginx/conf/logo.png",
"-gravity southeast -geometry +5+10 -composite -resize",
size,
ngx.var.file,
}, " ")
end
--ngx.header.content_type = "text/plain";
--ngx.say(command);
os.execute(command)
end
--ngx.header.content_type = "text/plain";
--ngx.say(command);
resize()
다음은 nginx 의 mime 설정 입 니 다.
[root@localhost conf]# cat mime.types
types {
text/html html htm shtml;
text/css css;
text/xml xml plist;
p_w_picpath/gif gif gif!320x320 gif!64x64 gif!140x140;
p_w_picpath/jpeg jpeg jpg jpg!320x320 jpg!64x64 jpg!140x140 jpg!200x200 jpg!0x0;
결과 주소 http://upload.qq.com/p_w_picpaths/13.jpg(원본 그림)
http://upload.qq.com/p_w_picpaths/13.jpg!0x0 (미리 보기 그림)
http://upload.qq.com/p_w_picpaths/13.jpg! 64x 64 (미리 보기 그림)
http://upload.qq.com/p_w_picpaths/13.jpg!320x320 (미리 보기 그림)
사이즈 자체 정의
/ usr / local / webserver / nginx / conf / logo. png 이것 은 워 터 마크 입 니 다.
수 동 테스트 그림 처리 시스템 에서 실행
convert src.jpg logo.gif -gravity southeast -geometry +5+10 -composite dest.jpg
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간단! Certbot을 사용하여 웹 사이트를 SSL(HTTPS)화하는 방법초보자가 인프라 주위를 정돈하는 것은 매우 어렵습니다. 이번은 사이트를 간단하게 SSL화(HTTP에서 HTTPS통신)로 변경하는 방법을 소개합니다! 이번에는 소프트웨어 시스템 Nginx CentOS7 의 환경에서 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.