GraphicsMagick 安裝:
javascript
a.下載GraphicsMagick-1.3.20.tar.gz文件,自行官網css
b.解壓文件html
tar zxvf GraphicsMagick-1.3.20.tar.gz
c.編譯java
./configure
d.安裝node
make make install
e.設置環境變量nginx
GM_HOME=/usr/local/GraphicsMagick; PATH=$GM_HOME/bin:$PATH; export PATH export GM_HOME
f.測試web
source /etc/profile gm
若是出來東西,說明安裝成功。注:確保安裝的組件都已經裝好,最後經過gm convert 以後,在查看選項中是否有-thumbnail。由於有一次源碼裝完,發現這個組件沒有被安裝上去,所以沒法使用圖片壓縮shell
2.Lua 安裝app
這安裝方法和上面的差很少。那我就不寫了。也能夠本身搜下百度
less
3.Nginx的相關配置
a.nginx.conf
server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 128k; sendfile on; tcp_nopush on; keepalive_timeout 60; open_file_cache max=102400 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #APP服務 include /usr/local/nginx/conf/wa-app.conf; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for $request_body'; access_log /var/log/nginx/access.log access; #這裏值的時日誌輸出的地方 }
b.wa-app.conf
#APP服務 upstream wa-app { server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=10s; server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=10s; } server { listen 80; server_name localhost; # add_header X-Frame-Options "SAMEORIGIN"; location / { proxy_pass #這裏的wa-app 指向的是upstream的wa-app proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 32m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } location ~* ^.+.(ico|map|less|gif|ttf|woff|svg|eot|bmp|jpg|jpeg|png|null|temp)$ { root /var/www/wa-app/web/ROOT; #指向項目跟目錄 #valid_referers none blocked server_names *.wa.com; #if ($invalid_referer) #{ # return 403; #} set $image_root /var/www/wa-app/web/ROOT; #設置圖片跟目錄 set $file "$image_root$uri"; # $image_root 指向/var/www/wa-app/web/R#OOT $uri 指向訪問的路徑的後半段好比: 指向的是 /upload/image#/2015/...xx.jpg if (!-f $file) { #lua_code_cache off; content_by_lua_file "/usr/local/nginx/conf/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; #proxy_cache cache_one; #proxy_cache_valid 200 302 1h; #proxy_cache_valid 301 1d; #proxy_cache_valid any 1m; index index.html index.htm; expires 7d; } location /nginx-status/ { stub_status on; access_log off; } }
c.Image.lua
if index then originalUri = string.sub(ngx.var.uri, 0, index-2); area = string.sub(ngx.var.uri, index); index = string.find(area, "([.])"); area = string.sub(area, 0, index-1); local index = string.find(originalFile, "([0-9]+)x([0-9]+)"); originalFile = string.sub(originalFile, 0, index-2) end -- check original file #檢查源文件 if not file_exists(originalFile) then #先判斷文件是否存在 local fileid = string.sub(originalUri, 2); -- main -- local fastdfs = require('restyfastdfs') -- local fdfs = fastdfs:new() -- fdfs:set_tracker("172.16.16.201", 22122) -- fdfs:set_timeout(1000) -- fdfs:set_tracker_keepalive(0, 100) -- fdfs:set_storage_keepalive(0, 100) -- local data = fdfs:do_download(fileid) -- if data then -- check image dir if not is_dir(ngx.var.image_dir) then os.execute("mkdir -p " .. ngx.var.image_dir) end writefile(originalFile, data) -- end end -- 建立縮略圖 -- local image_sizes = {"80x80", "800x600", "40x40", "60x60"}; local image_sizes = {"100x100","160x160","200x200","450x450","490x490","620x620","640x640","90x90"}; function table.contains(table, element) #這裏判斷填寫的尺寸是不是規定的。這裏註釋掉了 --for _, value in pairs(table) do -- if value == element then -- return true -- end --end --return false return true end if area and table.contains(image_sizes, area) then #先判斷是否符合尺寸要求,符合,生成圖片 local command = "gm convert -quality 92 -thumbnail " .. area .. " " .. originalFile .. " " .. ngx.var.file; #執行Graphics命令 os.execute(command); end; if file_exists(ngx.var.file) then #再次判斷文件是否存在,不存在返回404,存在繼續執行 --ngx.req.set_uri(ngx.var.uri, true); ngx.exec(ngx.var.uri) else ngx.exit(404) end
測試:www.wa.com/upload/image/2015/20150210/FC12AB22D1DE477EAE60D1214F7951F1.jpg_150x150.jpg
若是沒有成功,請自行查看nginx的錯誤日誌。