GraphicsMagick 安装:
a.下载GraphicsMagick-1.3.20.tar.gz文件,自行官网
b.解压文件
tar zxvf GraphicsMagick-1.3.20.tar.gz
c.编译
./configure
d.安装
make make install
e.设置环境变量
GM_HOME=/usr/local/GraphicsMagick; PATH=$GM_HOME/bin:$PATH; export PATH export GM_HOME
f.测试
source /etc/profile gm
如果出来东西,说明安装成功。注:确保安装的组件都已经装好,最后通过gm convert 之后,在查看选项中是否有-thumbnail。因为有一次源码装完,发现这个组件没有被安装上去,因此无法使用图片压缩
2.Lua 安装
这安装方法和上面的差不多。那我就不写了。也可以自己搜下百度
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的错误日志。