使用nginx+lua+GraphicsMagick实现图片自动 裁剪

在做网站尤其是以内容为主的过程中,常常会遇到一张图片各种地方都要引用,且每个引用的地方要求的图片尺寸都不一样的。
一般中大型的网站都会对这一类的图片做自动裁剪功能。本文介绍在centos6操作系统上,采用nginx、lua和GraphicsMagick工具简单实现图片的自动裁剪功能。其中nginx负责展示图片和调度lua脚本,GraphicsMagick负责对原图进行裁剪。

一、基础软件包安装
groupadd www
useradd -g www www -s /bin/false
yum -y install epel-release git gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel readline-devel ncurses-devel wget

二、下载相关软件
其中nginx-http-concat和echo-nginx-module模块非必须
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 zxvf lua-5.3.1.tar.gz
cd lua-5.3.1
make linux && make install
 
cd ..
tar zxvf 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日志目录
mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs

创建Nginx配置文件
在/usr/local/nginx/conf/目录中创建nginx.conf文件:
>/usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf

  输入以下内容:
#####################################################################################################
user  www www;

worker_processes 8;

error_log  /data/logs/nginx_error.log  crit;

pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

#charset  gb2312;
      
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
      
  sendfile on;
  tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

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;

#limit_zone  crawler  $binary_remote_addr  10m;
  include vhosts/*.conf;
}
#####################################################################################################

创建lua目录和lua脚本
mkdir /usr/local/nginx/lua
vi /usr/local/nginx/lua/cropSize.lua
#####################################################################################################
-- 根据输入长和宽的尺寸裁切图片

-- 检测路径是否目录
local function is_dir(sPath)
    if type(sPath) ~= "string" then return false end

local response = os.execute("cd " .. sPath)
    if response == 0 then
        return true
    end
    return false
end

-- 文件是否存在
function file_exists(name)
    local f = io.open(name, "r")
    if f ~= nil then io.close(f) return true else return false end
end

-- 获取文件路径
function getFileDir(filename)
    return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
end

-- 获取文件名
function strippath(filename)
    return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
end

--去除扩展名
function stripextension(filename)
    local idx = filename:match(".+()%.%w+$")
    if (idx) then
        return filename:sub(1, idx - 1)
    else
        return filename
    end
end

--获取扩展名
function getExtension(filename)
    return filename:match(".+%.(%w+)$")
end

-- 开始执行
-- ngx.log(ngx.ERR, getFileDir(ngx.var.img_file));

local gm_path = ‘/usr/local/GraphicsMagick/bin/gm‘

-- check image dir
if not is_dir(getFileDir(ngx.var.img_file)) then
    os.execute("mkdir -p " .. getFileDir(ngx.var.img_file))
end

--  ngx.log(ngx.ERR,ngx.var.img_file);
--  ngx.log(ngx.ERR,ngx.var.request_filepath);

-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
-- gm convert cropSize.jpg -thumbnail 300x300^ -gravity center -extent 300x300 -quality 100 +profile "*" cropSize.jpg_300x300.jpg
if (file_exists(ngx.var.request_filepath)) then
    local cmd = gm_path .. ‘ convert  -auto-orient -strip ‘ .. ngx.var.request_filepath
    cmd = cmd .. " -thumbnail " .. ngx.var.img_width .. "x" .. ngx.var.img_height .. "^"
    cmd = cmd .. " -gravity center -extent " .. ngx.var.img_width .. "x" .. ngx.var.img_height

-- 由于压缩后比较模糊,默认图片质量为100,请根据自己情况修改quality
    cmd = cmd .. " -quality " .. ngx.var.img_fill
    cmd = cmd .. " +profile \"*\" " .. ngx.var.img_file;
--  ngx.log(ngx.ERR, cmd);
    os.execute(cmd);
    ngx.exec(ngx.var.uri);
else
    ngx.exit(ngx.HTTP_NOT_FOUND);
end
#####################################################################################################

创建image.conf
touch /usr/local/nginx/conf/image.conf
vi /usr/local/nginx/conf/image.conf
###########################################################################################
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
        expires      30d;
}
###########################################################################################

创建js.conf
touch /usr/local/nginx/conf/js.conf
vi /usr/local/nginx/conf/js.conf
###########################################################################################
location ~ .*\.(js|css)?$
{
        expires      1d;
}
###########################################################################################

创建nginxcrop.conf
vi /usr/local/nginx/conf/nginxcrop.conf
###########################################################################################
    location /lua1 {
        default_type ‘text/plain‘;
        content_by_lua ‘ngx.say("hello, lua")‘;
    }
    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_100x100.jpg
    location ~* ^(.+\.(jpg|jpeg|gif|png))_+w(\d+)+h(\d+)+f(\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_fill $5;    # height
                    set $img_ext $2;    # file ext
                    content_by_lua_file lua/cropSize.lua;    # load lua
            }
    }

###########################################################################################

创建vhosts/a.conf
mkdir /usr/local/nginx/conf/vhosts
vi /usr/local/nginx/conf/vhosts/a.conf
###########################################################################################
server
{
    listen       80;
    charset  utf-8;
    server_name  192.168.1.50;
    index index.html index.htm index.php;
    root  /data/www/blog;  
    #设置图片路径 建议与root相同
    set $upload_path /data/www/blog;  
    #limit_conn   crawler  20;                             
    include       image.conf;
    include       js.conf;
    include       nginxcrop.conf;
    log_format  access  ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" $http_x_forwarded_for‘;
    access_log  /data/logs/access.log  access;
}
###########################################################################################

最后启动nginx
/usr/local/nginx/sbin/nginx

测试:
真实图片地址:http://192.168.0.25/images/xxx/xxx/a.jpg
访问裁剪地址:http://192.168.0.25/images/xxx/xxx/a.jpg_w200h150f70.jpg

原文地址:https://www.cnblogs.com/cnsong/p/9093785.html

时间: 2024-08-27 22:45:38

使用nginx+lua+GraphicsMagick实现图片自动 裁剪的相关文章

nginx+lua+GraphicsMagick生成实时缩略图-CentOS7

背景 大多数的系统都会涉及缩略图的处理,比如新闻系统和电商系统,特别是电商系统,每个商品大图都会对应一系列尺寸的缩略图用于不同业务场景的使用.部分系统也会生成不同尺寸的缩略图以供PC.手机端.ipad端使用. 解决方案探索: 直接加载原图,使用css样式表来控制图片的宽高.显然不太合适,大家也尽量不要这样做. web程序在上传成功后,同时生成相应缩略图.这种做法效率较低,如果遇到批量导入的业务时严重影响性能.并且同步生成缩略图会占用一定量的存储空间,如果能按需生成岂不更好? 使用七牛.阿里云提供

OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能

http://www.hopesoft.org/blog/?p=1188 2.用法 原始图片是input.jpg,尺寸:160×120 1)只缩小不放大 1 gm convert input.jpg -resize "500x500>" output_1.jpg 加了>,表示只有当图片的宽与高,大于给定的宽与高时,才进行“缩小”操作.生成的图片大小是:160×120,未进行操作如果不加>,会导致图片被比等放大. 2)等比缩图 (缺点:产生白边) 1 gm conver

.net图片自动裁剪白边函数案例

1.项目要求上传白底的图片要进行裁剪白边,于是同事谢了个函数感觉很好用. 2. #region 剪切白边 /// <summary> /// 剪切白边 /// </summary> /// <param name="p"></param> /// <returns></returns> public static Image Crop(Image p) { int x, y ;//for use of X,Y Co

Nginx利用lua剪辑FastDFS图片

Nginx利用lua剪辑FastDFS中的图片 我们经常用FastDFS来做图片服务器,通过nginx来上传或者获取图片.本文要实现的功能是,当客户点要获取不同尺寸的图片是,lua根据url中的尺寸大小调用GraphicsMagick 的gm命令来剪辑图片. 1.软件准备: GraphicsMagick-1.3.21.tar.gz LuaJIT-2.0.2.tar.gz nginx-1.4.2.tar.gz ngx_devel_kit-0.2.18.tar.gz v0.8.6.tar.gz(lu

nginx+lua+image实现上传图片裁剪和加水印 - nginx - 运维网 - iyunv.com

nginx+lua+image实现上传图片裁剪和加水印 - nginx - 运维网 - iyunv.com nginx+lua+image实现上传图片裁剪和加水印 - Android/[email protected] - 51CTO技术博客

通过Nginx访问FastDFS文件系统并进行图片文件裁剪的性能测试和分析

前段时间公司的分布式图片文件系统(FastDFS)做了图片裁剪和缩放功能,并把缩放计算和FastDFS做了解耦分离,前端用虚拟机作为图片文件缩放的访问代理层(Nginx Proxy),后端使用nginx直接访问FastDFS的文件系统.以下是测试和分析过程. 1测试场景 为了测试解耦后的图片读取并发和分析系统瓶颈,我们在内网中搭建了一个测试环境.以下是测试环境的网络的物理架构图: 上图中: NginxProxy:CPU解耦后的图片裁剪代理服务器 Storage:图片的存储服务器 ab:图片访问的

nginx+lua_nginx+GraphicsMagick生成实时缩略图

暂做笔记,带后续验证通过后,再补充 1.2.3 步. 一.安装 lua 二.安装 GraphicsMagick 三.安装nginx 四.配置 nginx nginx.conf http { lua_package_path '/usr/local/openresty/nginx/lua/?.lua;;'; server { listen 80; server_name img.rhythmk.org; root /home/wwwroot/static/image; #对类似_100x100.g

nginx+lua_nginx+GraphicsMagick缩略图+tfs获取原图+ngx_cache_purge

环境介绍 [email protected]:~# uname -aLinux ubuntu-1.230 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux[email protected]:~# cat /etc/issueUbuntu 12.04.1 LTS \n \l [email protected]:~# PS:以下操作我只在如上系统操作,仅供参考 1.安

使用Nginx+Lua(OpenResty)开发高性能Web应用

在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开发主要是以C/C++模块的形式进行,整体学习和开发成本偏高:如果有一种简单的语言来实现Web应用的开发,那么Nginx绝对是把好的瑞士军刀:目前Nginx团队也开始意识到这个问题,开发了nginxScript:可以在Nginx中使用JavaScript进行动态配置一些变量和动态脚本执行:而目前市面上