OpenResty+lua+GraphicsMagick生成缩略图

简介: 1、安装GraphicsMagick 下载地址:http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.19/GraphicsMagick-1.

1、安装GraphicsMagick

下载地址:http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.19/GraphicsMagick-1.3.19.tar.gz/download?use_mirror=ncu

tar zxvf GraphicsMagick-1.3.19.tar.gz
cd GraphicsMagick-1.3.19
make
make install

安装依赖包Ghostscript,不安装的话加水印会找不到字体

yum install -y ghostscript

 2、配置nginx.conf

location /down/PersonImg {
            set $image_root /usr/local/openresty/nginx/html;
            set $file "$image_root$uri";
            rewrite_by_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 = {"140x140", "800x800", "90x90"};

                function table.contains(table, element)
                    for _, value in pairs(table) do
                        if value == element then
                            return true
                        end
                    end
                    return false
                end
                if table.contains(image_sizes, area) then
                    local command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.image_root ..  originalUri  .. " -thumbnail " .. area .. " -gravity center -extent " .. area .. " " .. ngx.var.file;
                    os.execute(command);
                else
                    ngx.exit(401)
                end
            ';
            alias /usr/local/openresty/nginx/html/down/PersonImg/;

        }

3、访问
http://10.10.3.221/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A6.jpg.90x90.jpg

4、要加水印的话命令如下

/usr/local/GraphicsMagick/bin/gm convert /usr/local/openresty/nginx/html/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A6.jpg -font ArialBold -pointsize 45 -fill red -draw "text 10,10 dsideal" /usr/local/openresty/nginx/html/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A61111.jpg

 

 

 

相关文章
|
8月前
浅谈基于openresty(nginx+lua)开发轻量级,按流量控制的灰度模块(下)
浅谈基于openresty(nginx+lua)开发轻量级,按流量控制的灰度模块
87 0
|
测试技术 Shell 应用服务中间件
OpenResty 扩展库之(一)——lua-resty-shell 库
介绍   当您需要执行子进程(或shell命令)时,这是一个打算与OpenResty应用程序一起使用的小型库。 它类似于os.execute和io.popen,除了它是完全非阻塞的,因此即使对于需要很长时间完成的命令也是安全的。
2972 0
|
9月前
|
消息中间件 NoSQL 关系型数据库
Linux安装 OpenResty、Nginx、PHP、Mysql、Redis、Lua、Node、Golang、MongoDB、Kafka等
Linux安装 OpenResty、Nginx、PHP、Mysql、Redis、Lua、Node、Golang、MongoDB、Kafka等
111 0
|
8月前
|
负载均衡 应用服务中间件 测试技术
浅谈基于openresty(nginx+lua)开发轻量级,按流量控制的灰度模块(上)
浅谈基于openresty(nginx+lua)开发轻量级,按流量控制的灰度模块
334 0
|
关系型数据库 MySQL 应用服务中间件
Centos7安装OpenResty以及整合Lua简单的使用
Centos7安装OpenResty以及整合Lua简单的使用
945 0
Centos7安装OpenResty以及整合Lua简单的使用
|
NoSQL 应用服务中间件 API
【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一、 目标   使用Redis做分布式缓存;使用lua API来访问redis缓存;使用nginx向客户端提供服务,ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。
3616 0
|
NoSQL 关系型数据库 MySQL
OpenResty与Lua实现高并发请求处理
OpenResty与Lua实现高并发请求处理
433 0
OpenResty与Lua实现高并发请求处理
|
缓存 JSON NoSQL
Lua+OpenResty+nginx+redis+canal实现缓存策略
Lua+OpenResty+nginx+redis+canal实现缓存策略
703 0
Lua+OpenResty+nginx+redis+canal实现缓存策略
|
开发框架 JSON NoSQL
收藏的博客 -- Nginx+Lua,OpenResty相关资源
收藏的博客 -- Nginx+Lua,OpenResty相关资源
190 0