---
title: "使用nginx为图片进行水印操作"
author: "roc08"
date: "2015-12-04T11:58:04Z"
tags: []
categories: ["编程语言"]
slug: "nginx"
draft: false
lastmod: "2015-12-04T11:58:04Z"
id: 2261667
url: "https://roc08.iteye.com/blog/2261667"
---

打水印版nginx安装过程(centos)
1.	安装依赖组件
sudo yum install gd-devel pcre-devel libcurl-devel
2.	下载nginx
(1)	下载 wget http://nginx.org/download/nginx-1.9.6.tar.gz
(2)解压 tar -zxvf nginx-1.9.6.tar.gz
(3) 进入目录 cd nginx-1.9.6
3.	下载水印功能源代码
https://github.com/intaro/nginx-image-filter-watermark
点击download zip下载
4.	覆盖nginx中的代码
将下载的zip解压，将其中的ngx_http_image_filter_module.c文件覆盖到nginx源码中（/src/http/modules/http_image_filter_module.c）
5.	编译安装nginx（假设nginx源码在usr/local/nginx/nginx-1.9.6中）
./configure --with-http_image_filter_module
 --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module  --prefix=/usr/local/nginx/nginx-1.9.6
make
make install

6.	配置
Example Usage
Base Usage:
    location /img/ {
        image_filter watermark;#开启水印
        image_filter_watermark "PATH_TO_FILE";#水印文件位置
        image_filter_watermark_position center-center;#水印位置
		image_filter_jpeg_quality   95;#图片质量
        image_filter_buffer         20M;#缓存
		image_filter_watermark_width_from 400;   # 打水印的图片最小宽带，只有大于这个宽带的才会打水印
       image_filter_watermark_height_from 400;  #打水印的图片最小高度，只有大于这个高度的才会打水印


    }
Usage with resize and crop:
   location ~ ^/r/(\d+|-)x(\d+|-)/c/(\d+|-)x(\d+|-)/(.+) {
       set                         $resize_width  $1;
       set                         $resize_height $2;
       set                         $crop_width  $3;
       set                         $crop_height $4;

       alias                       /Users/goshan/Sites/Zot/Zot/web/$5;
       try_files                   "" @404;

       image_filter                resize $resize_width $resize_height;
       image_filter                crop   $crop_width $crop_height;

       image_filter_jpeg_quality   95;
       image_filter_buffer         2M;

       image_filter_watermark_width_from 400;   # Minimal width (after resize) of when to use watermark
       image_filter_watermark_height_from 400;  # Minimal height (after resize) of when to use watermark

       image_filter_watermark "PATH_TO_FILE";
       image_filter_watermark_position center-center;
   }
7.	注意事项
(1)	水印文件必须是背景透明的文件
(2)	如果要打水印的图片文件会很大，需将image_filter_buffer设置大一些，比如20M