源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Nginx服务器中用于生成缩略图的模块配置教程

  • 时间:2022-08-22 02:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Nginx服务器中用于生成缩略图的模块配置教程
[b]ngx_image_thumb模块生成缩略图[/b] ngx_image_thumb是nginx中用来生成缩略图的模块,生存缩略图的方法很多,本nginx模块主要功能是对请求的图片进行缩略/水印处理,支持文字水印和图片水印。支持自定义字体,文字大小,水印透明度,水印位置,判断原图是否是否大于指定尺寸才处理等等。 [b]1. 编译方法 [/b]编译前请确认您的系统已经安装了libcurl-dev libgd2-dev libpcre-dev 依赖库 [b]1.1 Debian / Ubuntu 系统举例 [/b]
# 如果你没有安装GCC相关环境才需要执行
$ sudo apt-get install build-essential m4 autoconf automake make
$ sudo apt-get install libgd2-noxpm-dev libcurl4-openssl-dev libpcre3-dev
[b]1.2 CentOS /RedHat / Fedora [/b]
# 请确保已经安装了gcc automake autoconf m4
$ sudo yum install gd-devel pcre-devel libcurl-devel
[b]1.3 FreeBSD / NetBSD / OpenBSD [/b]# 不多说了,自己用port 把libcurl-dev libgd2-dev libpcre-dev 装上吧 # 编译前请确保已经安装gcc automake autoconf m4 [b]1.4 Windows [/b]# 也支持的,不过要修改的代码太多了,包括Nginx本身,用VC++来编译 # 嫌麻烦可以用cygwin来编译。还是不建议你这么做了,用Unix/Linux操作系统吧。 [b]2. nginx / tengine 安装 [/b]选Nginx还是Tengine,您自己看,两者选其一 [b]2.1 下载Tengine[/b]
 # wget http://tengine.taobao.org/download/tengine-1.4.5.tar.gz
 # tar -zxvf tengine-1.4.5.tar.gz
 # cd tengine-1.4.5
[b]2.2 下载Nginx[/b]
# wget http://nginx.org/download/nginx-1.4.0.tar.gz
 # tar -zxvf nginx-1.4.0.tar.gz
 # cd nginx-1.4.0
[b]2.3 安装模块[/b]
 # wget https://github.com/3078825/nginx-image/archive/master.zip
 # unzip master.zip
 # ./configure --add-module=./nginx-image-master
 # make
 # make install
[b]3. 配置[/b]
location / {
 root html;
 #添加以下配置
 image on;
 image_output on;
 }
[b]4. 参数 [/b] [list] [*]image on/off 是否开启缩略图功能,默认关闭[/*] [*]image_backend on/off 是否开启镜像服务,当开启该功能时,请求目录不存在的图片(判断原图),将自动从镜像服务器地址下载原图[/*] [*]image_backend_server 镜像服务器地址[/*] [*]image_output on/off 是否不生成图片而直接处理后输出 默认off[/*] [*]image_jpeg_quality 75 生成JPEG图片的质量 默认值75[/*] [*]image_water on/off 是否开启水印功能[/*] [*]image_water_type 0/1 水印类型 0:图片水印 1:文字水印[/*] [*]image_water_min 300 300 图片宽度 300 高度 300 的情况才添加水印[/*] [*]image_water_pos 0-9 水印位置 默认值9 0为随机位置,1为顶端居左,2为顶端居中,3为顶端居右,4为中部居左,5为中部居中,6为中部居右,7为底端居左,8为底端居中,9为底端居右[/*] [*]image_water_file 水印文件(jpg/png/gif),绝对路径或者相对路径的水印图片[/*] [*]image_water_transparent 水印透明度,默认20[/*] [*]image_water_text 水印文字 "Power By Vampire"[/*] [*]image_water_font_size 水印大小 默认 5[/*] [*]image_water_font 文字水印字体文件路径[/*] [*]image_water_color 水印文字颜色,默认 #000000[/*] [/list] [b]4.1调用说明[/b] 这里假设你的nginx 访问地址为 http://127.0.0.1/ 并在nginx网站根目录存在一个 test.jpg 的图片 通过访问 http://127.0.0.1/test.jpg!c300x200.jpg 将会 生成/输出 test.jpg 300x200 的缩略图 其中 c 是生成图片缩略图的参数, 300 是生成缩略图的 宽度 200 是生成缩略图的 高度 一共可以生成四种不同类型的缩略图。 支持 jpeg / png / gif (Gif生成后变成静态图片) C 参数按请求宽高比例从图片高度 10% 处开始截取图片,然后缩放/放大到指定尺寸( 图片缩略图大小等于请求的宽高 ) M 参数按请求宽高比例居中截图图片,然后缩放/放大到指定尺寸( 图片缩略图大小等于请求的宽高 ) T 参数按请求宽高比例按比例缩放/放大到指定尺寸( 图片缩略图大小可能小于请求的宽高 ) W 参数按请求宽高比例缩放/放大到指定尺寸,空白处填充白色背景颜色( 图片缩略图大小等于请求的宽高 ) [b]5. 调用举例 [/b]
http://127.0.0.1/test.jpg!c300x300.jpg
http://127.0.0.1/test.jpg!t300x300.jpg
http://127.0.0.1/test.jpg!m300x300.jpg
http://127.0.0.1/test.jpg!w300x300.jpg
http://127.0.0.1/test.c300x300.jpg
http://127.0.0.1/test.t300x300.jpg
http://127.0.0.1/test.m300x300.jpg
http://127.0.0.1/test.w300x300.jpg
[b]nginx实时生成缩略图到硬盘上 [/b]现在随着各终端的出现(手机,ipad等平板),以及各种终端的手机分辨率和尺寸都不同,现在手机用户流量都是宝,网上出现了各种各样的生成缩略图功能的架构,有使用php实时生成缩略图的,也有用nginx + lua实现的,上面我也讲到了使用nginx生成缩略图,但是用户每次访问都需要生成一次,会给cpu和硬盘带来比较大的压力,今天带来了另外一种方式,这次使用nginx将原图生成缩略图到硬盘上.看我的配置 [b]1. 首先建好cache目录[/b]
# mkdir /data/site_cache/
[b]2. 修改nginx配置[/b]
 location ~* ^/resize {
  root /data/site_cache/$server_name;
  set $width 150;
  set $height 100;
  set $dimens "";
 
  if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) {
   set $width $1;
   set $height $2;
   set $image_path $3;
   set $demins "_$1x$2";
  }
 
  if ($uri ~* "^/resize/(.*)" ) {
   set $image_path $1;
  }
 
  set $image_uri image_resize/$image_path?width=$width&height=$height;
 
  if (!-f $request_filename) {
   proxy_pass http://127.0.0.1/$image_uri;
   break;
  }
  proxy_store /data/site_cache/$server_name/resize$demins/$image_path;
  proxy_store_access user:rw group:rw all:r;
  proxy_set_header Host $host;
  expires 30d;
  access_log off;
 }
 
 location /image_resize {
  alias /data/site/$server_name/;
  image_filter resize $arg_width $arg_height;
  image_filter_jpeg_quality 75;
  access_log off;
 }
生成缩略图流程如下: (1)、原图在www.1sucai.cn/image/1.jpg。我需要一份100x100的缩略图。 (2)、请求www.1sucai.cn/resize_100x100/image/1.jpg. (3)、这个请求进入了location ~* ^/resize,接着判断image_path这个目录下是否存在这张图片,如果存在直接放回给用户, (4)、不存在那么跳转到http://www.1sucai.cn/image_resize/image/1.jpg?width=100&height=100; (5)、location /image_resize根据传入的width和height执行缩略功能,并且设置图像质量为75 (6)、接着生成文件到/data/site_cache/www.1sucai.cn/resize_100x100/image/1.jpg (7)、并且返回图片给用户 (8)、nginx生成缩略图到硬盘上的功能到这里就结束了
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部