nginx的Http Image Filter Module可以动态处理JPEG,GIF,PNG格式的图片,它在0.7.54+版本后引入nginx,要使用它需要在./configure时添加 –with-http_image_filter_module 打开它,它需要libgd的支持,所以要使用它我们先要在机器上安装libgd,centos和debian的用户可以使用它们的包管理器去安装,如果要手动安装,对于64位的系统需要在编译时添加相应参数(使生成64位的二进制文件),这里只有32位的linux系统安装的例子。
1、需要的程序文件
jpegsrc.v6b.tar.gz
libpng-1.5.12.tar.gz
pierrejoye-gd-libgd-5551f61978e3.tar.gz
freetype-2.3.5.tar.gz
pcre-8.31.tar.gz
nginx-1.3.4.tar.gz
cd jpeg-6b ./configure --enable-shared --enable-static make && make install # 报错,提示找不到文件 /usr/local/man/man1/ # mkdir -p /usr/local/man/man1/ 然后再执行 make install cd libpng-1.5.12 ./configure make && make install # 报错,提示找不到文件夹 /usr/local/include/freetype2/freetype/ # mkdir -p /usr/local/include/freetype2/freetype/ 然后再 make install cd freetype-2.3.5 ./configure make make install # 报错,提示找不到文件夹 /usr/local/include/freetype2/freetype/internal # 执行 mkdir -p /usr/local/include/freetype2/freetype/internal 然后再 make install cd pierrejoye-gd-libgd-5551f61978e3/src ./configure --with-jpeg=/usr/local/jpeg8 --with-png -with-zlib -with-freetype=/usr/local/freetype make make install cd pcre-8.31 ./configure make && make intall groupadd nginx useradd -g nginx nginx cd nginx-1.3.4 ./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module # 本地如果要打开debug日志,需要添加--with-debug make && make install
3、定制
模块的crop是从中间去截取,并且对于宽图,只能得到中间的一片图像,不符合大多数的情况,我们需要从顶部截取并且当图宽时,把宽缩放到规定的大小,然后再截取(原始逻辑是把高缩放到指定大小再截取),没有直接在原crop基础上修改。
代码位置:src/http/modules/ngx_http_image_filter_module.c
新增加指令crop_guoguo
主要修改的代码
static ngx_buf_t * ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) { ...... } else if (conf->filter == NGX_HTTP_IMAGE_ROTATE) { resize = 0; /* guoguo add start */ } else if (conf->filter == NGX_HTTP_IMAGE_CROP_GUOGUO) { resize = 0; if ((double) ctx->max_width / dx < 1) {// 新增加的代码段,处理宽图的缩放 dy = dy * ctx->max_width / dx; dy = dy ? dy : 1; dx = ctx->max_width; resize = 1; } } /* guoguo add end */ ...... if ((ngx_uint_t) dx > ctx->max_width) { ox = dx - ctx->max_width; } else { ox = 0; } /* guoguo add start */ /* 处理顶部截取,可以更细致的做配置,比如增加一个seed参数用于表示截线的位置 */ ox /= 2; if (conf->filter == NGX_HTTP_IMAGE_CROP_GUOGUO) { oy = 0; } else { oy /= 2; } /* guoguo add end */ if (ox || oy) { ...... if (ngx_strcmp(value[i].data, "resize") == 0) { imcf->filter = NGX_HTTP_IMAGE_RESIZE; } else if (ngx_strcmp(value[i].data, "crop") == 0) { imcf->filter = NGX_HTTP_IMAGE_CROP; /* guoguo add start */ } else if (ngx_strcmp(value[i].data, "crop_guoguo") == 0) { imcf->filter = NGX_HTTP_IMAGE_CROP_GUOGUO; /* guoguo add end */ ...... }
4、nginx配置文件
location /mblog/ { if ($uri ~* (/xxxxxx)/r_(xxxxxx\.(jpg|png|gif)) ) { set $pre_uri $1; set $file_name $2; rewrite ^(.*)$ /guoguo/stream/$pre_uri/m_$file_name last; } proxy_pass http://image_process; } location /guoguo/stream/ { proxy_pass http://image_process/; #image_filter resize 150 100; #image_filter rotate 90; # image_filter crop 50 50; #image_filter resize 200 -; #image_filter crop 200 700; #image_filter crop 200 700; image_filter crop_guoguo 200 700; }