美文网首页
Nginx之http_image_filter_module模块

Nginx之http_image_filter_module模块

作者: yichen_china | 来源:发表于2021-07-31 16:19 被阅读0次

一、安装

yum install gd-devel #安装gd库

检查是否已安装过,我是用的宝塔编译默认安装的nginx1.21,默认就已经安装了

nginx -V
企业微信截图_16277194081145.png

有这个http_image_filter_module就是装过了,直接可以用了

没装过需要手动编译安装

./configure --prefix=/usr/local/nginx     \
--with-debug                            \
--with-http_stub_status_module          \
--with-http_ssl_module                  \
--with-http_realip_module               \
--with-http_image_filter_module         \
--with-pcre=../pcre-8.21                \
--add-module=../ngx_devel_kit-0.2.19    \
--add-module=../lua-nginx-module-0.9.8  \
--add-module=../echo-nginx-module       \
--add-module=../redis2-nginx-module     \
--add-module=../set-misc-nginx-module

二、配置

    location ~* (.*\.(jpg|gif|png))!(.*)!(.*)$ {
        set $width   $3;
        set $height  $4;
        rewrite "(.*\.(jpg|gif|png))(.*)$" $1;
    }

    location ~* /image/.*\.(jpg|gif|png)$ {
        root   /home/jfy/web/;
        #image_filter off;
        #image_filter test;
        #image_filter size;
        #image_filter rotate 90;
        image_filter resize $width $height;
        #image_filter crop 300 200;
        image_filter_buffer 10M;
        image_filter_interlace on;
        image_filter_jpeg_quality 95;
        image_filter_sharpen 100;
        image_filter_transparency on;
    }

功能配置参数参考

image_filter off;
#关闭模块
 
image_filter test;
#确保图片是jpeg gif png否则返415错误
 
image_filter size;
#输出有关图像的json格式:例如以下显示{ "img" : { "width": 100, "height": 100, "type": "gif" } } 出错显示:{}
 
image_filter rotate 90|180|270;
#旋转指定度数的图像,參数能够包括变量,单独或一起与resize crop一起使用。
 
image_filter resize width height;
#按比例降低图像到指定大小,公降低一个能够还有一个用"-"来表示,出错415,參数值可包括变量,能够与rotate一起使用,则两个一起生效。
 
image_filter crop width height;
#按比例降低图像比較大的側面积和还有一側多余的载翦边缘,其他和rotate一样。没太理解
 
image_filter_buffer 10M;
#设置读取图像缓冲的最大大小,超过则415错误。
 
image_filter_interlace on;
#假设启用,终于的图像将被交错。对于JPEG,终于的图像将在“渐进式JPEG”格式。
 
image_filter_jpeg_quality 95;
#设置变换的JPEG图像的期望质量。可接受的值是从1到100的范围内。较小的值通常意味着既降低图像质量,降低数据传输,推荐的最大值为95。參数值能够包括变量。
 
image_filter_sharpen 100;
#添加了终于图像的清晰度。锐度百分比能够超过100。零值将禁用锐化。參数值能够包括变量。
 
image_filter_transparency on;
#定义是否应该透明转换的GIF图像或PNG图像与调色板中指定的颜色时,能够保留。透明度的损失将导致更好的图像质量。在PNG的Alpha通道总是保留透明度。

三、几个规则,可能实用。
匹配全站全部的结尾图片


        location ~* \.(jpg|gif|png)$ {
            image_filter resize 500 500;
        }

匹配某个文件夹全部图片

        location ~* /image/.*\.(jpg|gif|png)$ {
            image_filter resize 500 500;
        }

再比方用url来指定

        location ~* (.*\.(jpg|gif|png))!(.*)!(.*)$ {
            set $width      $3;
            set $height     $4;
            rewrite "(.*\.(jpg|gif|png))(.*)$" $1;
        }
 
        location ~* /image/.*\.(jpg|gif|png)$ {
            image_filter resize $width $height;
        }

http://172.16.18.114/image/girl.jpg!300!200
自己主动将原图缩放为300*200的尺寸

更多配置方案

nginx_image_filter 

http_image_filter_module

配置


第一种:

//官方配置
location /img/ {
    proxy_pass   http://backend;
    image_filter resize 150 100;
    image_filter rotate 90;
    error_page   415 = /empty;
}

location = /empty {
    empty_gif;
}

http://nginx.org/en/docs/http/ngx_http_image_filter_module.html#image_filter_webp_quality

第二种:

//裁剪图片,不存储硬盘
访问 http://xxx.com/fanfan_11.jpg@100w_100h_75Q_r
http://xxx.com/fanfan.jpg@150w_100h_75Q_r
http://xxx.com/img/fanfan.jpg@11w_11_h_80Q_r

# 等比缩放图片
location ~ (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_r$ {
    set $w $3; #宽
    set $h $4; #高
    set $q $5; #图片质量
    image_filter resize $w $h;
    image_filter_jpeg_quality $q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}
# 裁剪图片
location ~ (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_c$ {
    set $w $3; #宽
    set $h $4; #高
    set $q $5; #图片质量
    image_filter crop $w $h;
    image_filter_jpeg_quality $q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}

第三种:

//保存在磁盘

访问 
http://xxx.com/image_filter/222.jpg@120w_120h_75Q_r
代理到
xxx.com/image-resize/image_filter/222.jpg?w=200&h=200&q=75

location ~ (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ {
    
    # 方便调试
    error_log  /usr/local/var/logs/nginx/xxx.com.imagefilter.error.log  debug;

    # 限制referer,防盗链
    # valid_referers xxx.com;#domain modify
    # if ($invalid_referer) {return 404;}
 
    set $w $3; #宽
    set $h $4; #高
    set $q $5; #图片质量
    set $type $6;
    set $image_path  $1.$2; #真实图片地址
    set $cache_path  $1_$3w_$4h_$5Q_$6.$2;  #临时文件地址
    if ($type = 'r') {
        set $type 'image-resize';
    }
    if ($type = 'c') {
        set $type 'image-crop';
    }
    set $image_uri  /$type$image_path?w=$w&h=$h&q=$q;
    if (-f $document_root$cache_path) {
        rewrite (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
        break;
    }
    if (!-f $document_root$cache_path) {
        # proxy_pass http://$server_name.$image_uri;
        # 必须填写ip,填写域名的话,nginx在启动的时候会检测域名,解析DNS,启动后,在修改域名的解析是不生效的
        # 实际上,本机填写域名报500不生效,估计是DNS设置不对,会在server下添加
        # resolver 127.0.0.1 valid=300s;

        proxy_pass http://127.0.0.1$image_uri;
        break;
    }
    proxy_store $document_root$cache_path;
    proxy_store_access user:rw group:rw all:r;
    proxy_temp_path  /tmp/images;
    proxy_set_header Host $host;
    expires  10d; # 设置图片过期时间10天
}
location ~ /image-resize(.+)\.(jpg|gif|png) {
    rewrite /image-resize(.+)\.(jpg|gif|png)$ $1.$2 break;
    image_filter resize $arg_w $arg_h;
    image_filter_jpeg_quality $arg_q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}
location ~ /image-crop(.+)\.(jpg|gif|png) {
    rewrite /image-crop(.+)\.(jpg|gif|png)$ $1.$2 break;
    image_filter crop $arg_w $arg_h;
    image_filter_jpeg_quality $arg_q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}

相关文章

  • [LNMP]缩略图网关:Nginx的http_image_fil

    http_image_filter_module是nginx官方提供的图片处理模块,支持jpg、gif、png格式...

  • nginx http_image_filter_module安装

    简单介绍 http_image_filter_module是nginx提供的集成图片处理模块,在网站访问量不是很高...

  • Nginx之http_image_filter_module模块

    一、安装 检查是否已安装过,我是用的宝塔编译默认安装的nginx1.21,默认就已经安装了 有这个http_ima...

  • 6.Nginx模块学习方法

    Nginx模块 Nginx模块分为 Nginx官方模块 和 第三方模块 , 这里我们拿Nginx官方模块来介绍一下...

  • Nginx (4)

    Nginx之负载均衡 Nginx 通过Upstream 模块进行负载均衡。 upstream 支持的负载均衡算法N...

  • Nginx核心模块以及指令介绍

    Nginx模块概览 Nginx核心模块以及指令介绍 注意:Nginx的核心模块包含主模块和事件模块,即上图的cor...

  • 应用运维面试核心

    面试题 Nginx模块 你以前用过哪些Nginx模块? upstream 是Nginx负载均衡模块 image ...

  • nginx内核原理

    Nginx的模块 Nginx由内核和模块组成。 Nginx的模块从结构上分为核心模块、基础模块和第三方模块: 核心...

  • nginx 源代码分析 (二)

    1. nginx模块 nginx的功能分布在nginx模块中,一个模块为一个功能单元。每个nginx模块都专注于自...

  • Nginx模块之ssl模块

    ngx_http_ssl_module配置 ssl on | off是否启用ssl listen 443 ssl在...

网友评论

      本文标题:Nginx之http_image_filter_module模块

      本文链接:https://www.haomeiwen.com/subject/hfhkvltx.html