關於安裝GraphicsMagic與配置環境變量參見 服務器搭建:3.一、openresty圖片壓縮之GraphicsMagickphp
一、如何在linux中直接使用壓縮呢,看下面的命令:html
#gm convert ./abd.jpg -thumbnail 100x100 .abd.100x100.jpg
注意,在一張圖是原圖,第二張圖是壓縮後的圖,另外 中間部分的 100x100是壓縮的尺寸。linux
二、用lua怎麼作呢,下面是個人test.lua。nginx
local source="/opt/local/test/adb.jpg" local arae="300x300" local target="/opt/local/test/dick."..arae..".png" local pok=io.open('pok.txt','w') print(pok) local af=assert(pok) print(af) local mdx="gm convert "..source.." -thumbnail "..arae.." "..target pok:write(mdx) pok:close() os.execute(mdx)
將文件名改爲本身的文件路徑 能夠跑一下。shell
三、怎麼在nginx中整合使用?服務器
3.1 在nginx的 ./conf/nginx.conf 文件最後添加 include vhost/*.conf 意思是引入conf文件夾下的全部vhost中的.conf全部文件。測試
3.2 在vhost中建立一個 img.chimywin.com.conf 文件,並在其中插入代碼以下:lua
server { listen 80; server_name img.hdxw.com; root /data/htdocs/source; index index.html index.htm index.php;
location / { # 這裏是把全部不存在的文件和目錄,全都轉到 index.php 處理 #try_files $uri $uri/ /index.php?__q=$uri&$args; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { root /data/htdocs/source; set $image_root /data/htdocs/source; set $file "$image_root$uri"; if (!-f $file) { content_by_lua_file lua/image.lua; # 請注意這裏,直接去調用lua文件處理見後面 } proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; index index.html index.htm; expires 7d; } }
# 日誌輸出格式 log_format access.img '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';
access_log /data/management/logs/img.access.log access.img; /opt/openresty/nginx/conf/vhost/img.chimywin.com.conf
3.3 在nginx目錄下建立一個lua/image.luaspa
local file_exists = function(name) local f=io.open(name,"r") if f~=nil then io.close(f) return true else return false end end
-- 訪問圖片的Uri,是一個相對路徑 local newFileUri = ngx.var.uri; -- 訪問圖片的絕對路徑 local target = ngx.var.file; -- 是否是有符合要求的圖片請求路徑 local index = string.find(target, "([0-9]+)x([0-9]+)"); -- local pok=io.open('/var/html/pok.txt','w') -- 支持的size格式 這裏沒有使用 local image_sizes = {"40x40", "60x60", "80x80", "100x100","120x120","140x140", "160x160", "180x180", "200x200", "220x220","240x240", "260x260", "280x280", "300x300","320x320","340x340", "360x360", "380x380", "400x400", "640x640", "600x600"}; -- 記錄Lua壓縮圖日誌 -- pok:write("newFileUri="..newFileUri..";"); -- pok:write("target="..target..";"); -- pok:write("index="..index..";");
-- 若是有 if index then -- local af=assert(pok) -- pok:write("開始分析 ;"); -- pok:write("newFileUri="..newFileUri..";"); -- pok:write("target="..target..";"); -- 截取 字符串 xxx/xxx/kl.jpg.100x100.jpg中 100x100之前的位置 : xxx/xxx/kl.jpg local source = string.sub(target, 0, index-2); -- pok:write("source="..source..";"); -- 二次截取得到 圖片的規格 local area = string.sub(target, index); -- pok:write("area="..area..";"); index = string.find(area, "([.])"); if(index ~= nil ) then area = string.sub(area, 0, index-1); end -- pok:write("area="..area..";"); -- 拼接壓縮圖命令 # 注意這裏的命令,後面解釋 local cmdexec="/usr/local/GraphicsMagick/bin/gm convert "..source.." -thumbnail "..area.." "..target os.execute(cmdexec); -- pok:write("cmdexec="..cmdexec..";"); -- pok:write("end") -- pok:close() ngx.exec(ngx.var.uri) -- end -- if file_exists(ngx.var.file) then -- ngx.exec(ngx.var.uri) else ngx.exit(404) end
說明一下:.net
i、--註釋掉的部分是我爲了測試寫的日誌文件。
ii、 local cmdexec="/usr/local/GraphicsMagick/bin/gm convert "..source.." -thumbnail "..area.." "..target 這裏由於我安裝後經過nginx沒法調用 gm命令,暫時沒有找到緣由 ,因此這裏直接用了絕對路徑 來執行它。