使用nginx+Lua+GraphicsMagick实现图片自动裁剪
在做网站尤其是以内容为主的过程中,常常会遇到一张图片各种地方都要引用,且每个引用的地方要求的图片尺寸都不一样的。对于小网站来说,这种需求通常是人工进行裁剪,然后在代码上分别引用。然而当网站图片越来越多的时候,这种办法的效率问题就凸显出来了,所以一般中大型的网站都会对这一类的图片做自动裁剪功能。本文介绍在centos6操作系统上,采用nginx、lua和GraphicsMagick工具简单实现图片的自动裁剪功能。其中nginx负责展示图片和调度lua脚本,GraphicsMagick负责对原图进行裁剪。
实现功能点如下:
1、输入原图地址,例如:http://192.168.1.19/img_6065.jpg后返回原图
2、输入图片地址,例如:http://192.168.1.19/img_6065.jpg_200x400.jpg 后,如果目录底下存在图片,则返回,如果不存在则根据需求对原图进行裁剪,其中200代表图片的宽,400代表图片的长。
3、图片的根目录为/static/p_w_picpaths,裁剪后的图片和原图放在同一级目录,例如原图地址的物理路径为:/static/p_w_picpaths/uploads/2016/07/01.jpg
按200x200裁剪后的图片物理路径为:/static/p_w_picpaths/uploads/2016/07/01_200x200.jpg
一、基础软件包安装
# groupadd www # useradd -g www www -s /bin/false # yum -y install epel-release git # yum install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel # yum install -y libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel readline-devel ncurses-devel
二、下载相关软件
其中nginx-http-concat和echo-nginx-module模块非必须
# cd /usr/local/src # wget http://nginx.org/download/nginx-1.8.0.tar.gz # wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz # wget http://zlib.net/zlib-1.2.8.tar.gz # wget http://www.lua.org/ftp/lua-5.3.1.tar.gz # wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.18.tar.gz # git clone https://github.com/alibaba/nginx-http-concat.git # git clone https://github.com/simpl/ngx_devel_kit.git # git clone https://github.com/openresty/echo-nginx-module.git # git clone https://github.com/openresty/lua-nginx-module.git
三、编译安装nginx、GraphicsMagick
# tar -zxf nginx-1.8.0.tar.gz # tar -zxf LuaJIT-2.0.4.tar.gz # tar -zxf zlib-1.2.8.tar.gz # cd LuaJIT-2.0.4 # make # make install # export LUAJIT_LIB=/usr/local/lib # export LUAJIT_INC=/usr/local/include/luajit-2.0 # ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 # cd .. # tar -zxvpf lua-5.3.1.tar.gz # cd lua-5.3.1 # make linux && make install # cd .. # tar -zxvpf GraphicsMagick-1.3.18.tar.gz # cd GraphicsMagick-1.3.18 # ./configure --prefix=/usr/local/GraphicsMagick --enable-shared # make && make install # cd .. # cd nginx-1.8.0 # ./configure --prefix=/usr/local/nginx \ --user=www \ --group=www \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_sub_module \ --with-http_flv_module \ --with-http_dav_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_addition_module \ --with-http_spdy_module \ --with-pcre \ --with-zlib=../zlib-1.2.8 \ --add-module=../nginx-http-concat \ --add-module=../lua-nginx-module \ --add-module=../ngx_devel_kit \ --add-module=../echo-nginx-module \ --with-ld-opt=-Wl,-rpath,$LUAJIT_LIB # make && make install
四、修改nginx配置文件
server { listen 80; server_name 192.168.1.19; root /static/p_w_picpath; location /lua1 { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ { root /static/p_w_picpath; if (!-f $request_filename) { # 如果文件不存在时才需要裁剪 add_header X-Powered-By 'Lua GraphicsMagick'; # 此 HTTP Header 无实际意义,用于测试 add_header file-path $request_filename; # 此 HTTP Header 无实际意义,用于测试 #lua_code_cache off; # 在编写外部 Lua 脚本时,设置为 off Nginx 不会缓存 Lua,方便调试 set $request_filepath /static/p_w_picpath/$1; # 设置原始图片路径,如:/document_root/1.gif set $width $3; # 设置裁剪/缩放的宽度 set $height $4; # 设置裁剪/缩放的高度 set $ext $5; # 图片文件格式后缀 content_by_lua_file lua/ImageResizer.lua; # 加载外部 Lua 文件 } } }
五、准备lua脚本
# cat /usr/local/nginx/lua/ImageResizer.lua local command = "/usr/local/GraphicsMagick/bin/gm convert -auto-orient -strip " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext; os.execute(command); ngx.exec(ngx.var.request_uri);
六、测试
# cd /static/p_w_picpath/ # ls # cd uploads/2016/07/21/ # ls
如果要做固定高宽模式裁切图片处理,例如:http://192.168.1.19/uploads/2016/07/21/1.jpg_-200.jpg 或http://192.168.1.19/uploads/2016/07/21/1.jpg_200-.jpg 这种需求,首先修改nginx配置文件如下
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; server { listen 80; server_name 192.168.1.19; root /static/p_w_picpath; location /lua1 { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } set $upload_path /static/p_w_picpath; set $img_original_root $upload_path;# original root; set $img_thumbnail_root $upload_path/cache/thumb; set $img_file $img_thumbnail_root$uri; # like:/xx/xx/xx.jpg_100-.jpg or /xx/xx/xx.jpg_-100.jpg location ~* ^(.+\.(jpg|jpeg|gif|png))_((\d+\-)|(\-\d+))\.(jpg|jpeg|gif|png)$ { root $img_thumbnail_root; # root path for croped img set $img_size $3; if (!-f $img_file) { # if file not exists add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test add_header file-path $request_filename; # header for test set $request_filepath $img_original_root$1; # origin_img full path:/document_root/1.gif set $img_size $3; # img width or height size depends on uri set $img_ext $2; # file ext content_by_lua_file lua/autoSize.lua; # load lua } } # like: /xx/xx/xx.jpg_100x100.jpg location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png)$ { root $img_thumbnail_root; # root path for croped img if (!-f $img_file) { # if file not exists add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test add_header file-path $request_filename; # header for test set $request_filepath $img_original_root$1; # origin_img file path set $img_width $3; # img width set $img_height $4; # height set $img_ext $5; # file ext content_by_lua_file lua/cropSize.lua; # load lua } } } }
其次准备两个lua脚本,脚本包含在附件中。
测试效果如下:
后记:
采用这种方式简单实现了需求,但是url中的图片高度和宽度没有限制,这个需要注意,还有长时间不用的缩略图如何清理也需要考虑。
参考文档,感谢作者分享!
http://my.oschina.net/eduosi/blog/169606
http://blog.ddtet.org/2014/03/graphicsmagick.html
https://github.com/yanue/nginx-lua-GraphicsMagick
http://www.111cn.net/sys/CentOS/55070.htm

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
本地ASP.NET开发页面使用AzureAD(AAD)验证登录
本地ASP.NET开发页面使用AzureAD(AAD)验证登录 Azure和Office365已不是一个热门话题了,因为所谓的云时代已经走进了技术大师们的内心,大家多少有一定了解了,所以就不多介绍了,我的Blog中之前也写了很多关于Azure及Office365的相关文章,如果有兴趣的同学可以参考一下。一般企业内部如果使用了Azure或者Office365产品的话,都会跟本地的域进行验证绑定,然后可以使用相关工具(Dirsync & Azure AD Connect)将本地的域用户同步到AAD里面,然后配置ADFS使用本地的域用户格式进行登录及管理Azure相关服务等,当然也有其他的一些做法,具体就不介绍了,我们首先要知道,Azure 和Office365使用的是同一个AD服务,所以注册的时候自定义的域名称也就是唯一标识了,比如,当注册一个oa@ixmsoft.onmicrosoft.com(portal.office.com)的office365顶订阅的账户的时候,系统也会保留一个oa@imxmsoft.onmicrosoft.com;(manage.windowsazure...
- 下一篇
MariaDB10自动化安装部署
去MariaDB官网下载MariaDB本文用的是MariaDB 10.1.16 https://downloads.mariadb.org 选择二进制版本,下载到/root目录下 mariadb-10.1.16-linux-x86_64.tar.gz 开始安装 [root@HE3 ~]# cat mariadb_auto_install.sh [root@HE3 ~]# cat mariadb_auto_install.sh [root@HE3 ~]# cat mariadb_auto_install.sh ######二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可############### ######数据库目录/usr/local/mysql############ ######数据目录/data/mysql############ ######日志目录/log/mysql############ ######端口号默认3306其余参数按需自行修改############ ################## #author:rrhele...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS关闭SELinux安全模块
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Docker使用Oracle官方镜像安装(12C,18C,19C)