Nginx+lua_nginx+graphicsmagick generating real-time thumbnail images

Source: Internet
Author: User

Take notes with follow-up verification, then add 1, 2, 3 steps.

First, install Lua

Second, installation GraphicsMagick

Third, install Nginx

Four, configuration Nginx

Nginx.conf

http {lua_package_path'/usr/local/openresty/nginx/lua/?. LUA;;'; server {Listen the;                server_name img.rhythmk.org; Root/home/wwwroot/Static/image; #对类似_100x100. gif/jpg/png/JPEG thumbnail processing location~* _([0-9]+) x ([0-9]+)\. (gif|jpg|png|jpeg) $ {#匹配文件名规则 root/home/wwwroot/Static/image; #站点根目录Set$image _root/home/wwwroot/Static/image; #图片目录Set$thumbnail _root/home/wwwroot/Static/thumbnail; #缩略图存放目录 #如果缩略图文件存在, return directlySet$file $thumbnail _root$uri; if(-f $file) {Rewrite^/(. *) $/thumbnail/$1Last ; } #如果缩略图文件不存在, the thumbnail module is used to processif(!-f $file) {Rewrite_by_lua_file Lua/Thumbnail.lua; }}} #include conf/*. conf;}

Lua/thumbnail.lua

Local index =string. Find (Ngx.var. Uri,"([0-9]+) x ([0-9]+)"); Local Originaluri=string. Sub (ngx.var. Uri,0, index-2); Local Area=string. Sub (ngx.var. Uri, index); Index=string. Find (area,"([.])"); Area=string. Sub (area,0, index-1); Local image_sizes= {"80x80","800x600","40x40"}; function table.contains (table, Element) for_, ValueinchPairs (table) Do            ifValue = =element Thenreturn trueEnd Endreturn falseEndiftable.contains (image_sizes, area) then local command="GM Convert".. Ngx.var. Image_root. Originaluri."-thumbnail".. Area.."-background gray-gravity center-extent".. Area.." ".. Ngx.var. File;          Os.execute (command); Ngx.req.set_uri (NGX.var. Uri,true); ElseNgx.exit (404);  End 

Nginx+lua_nginx+graphicsmagick generating real-time thumbnail images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.