热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

Nginx服务器ngx_http_image_filter_module模块使用讲解

原创文章,转载请指明出处并保留原文url地址本文主要针对nginx的ngx_http_image_filter_module模块做简单介绍,本文具体包括如下指image_filter,image_filter_buffer,image_filter_jpeg_quality,image_filter_sharpen,i
原创文章,转载请指明出处并保留原文url地址
本文主要针对nginx的ngx_http_image_filter_module 模块做简单介绍,本文具体包括如下指image_filter,image_filter_buffer,image_filter_jpeg_quality,image_filter_sharpen,image_filter_transparency
ngx_http_image_filter_module模块是一个用于在JPEG, GIF, 和PNG 图片格式之间进行转换的过滤模块(nginx的一个术语)。
这个模块不是nginx内建模块,需要通过?with-http_image_filter_module指令进行进行开启相关功能。
这个模块使用libgd库, 推荐采用最新版本的库。
配置示例如下:
    location /img/ {
        proxy_pass   http://backend;
        image_filter resize 150 100;
        image_filter rotate 90;
        error_page   415 = /empty;
    }
    location = /empty {
        empty_gif;
    }
Nginx原文:
The ngx_http_image_filter_module module (0.7.54+) is a filter that transforms images in JPEG, GIF, and PNG formats.
This module is not built by default, it should be enabled with the ?with-http_image_filter_module configuration parameter.
This module utilizes the libgd library. It is recommended to use the latest available version of the library; it is version 2.0.35 as of this writing.
Example Configuration
    location /img/ {
        proxy_pass   http://backend;
        image_filter resize 150 100;
        image_filter rotate 90;
        error_page   415 = /empty;
    }
    location = /empty {
        empty_gif;
    }
1. image_filter
syntax:
image_filter  off;
image_filter  test;
image_filter  size;
image_filter  rotate 90 | 180 | 270;
image_filter  resize width height;
image_filter  crop width height;
default:
image_filter off;
context:
location
设置图像变换的类型
Off: 关闭一个访问类的图片变换处理功能(一般是一个nginx配置的location)
Test:确认在返回给用户的相应图片是JPEG, GIF, or PNG 格式的, 否则报415 错误(不支持的媒体文件类型)
Size: 采用json格式输出信息,例如:
    { "img" : { "width": 100, "height": 100, "type": "gif" } }
如果发生错误则输出如下
    {}
rotate 90|180|270
逆时针旋转图像到设定的度数,这个值可以包括变量(配置时是个变量, 到执行具体这个条指令是, 变量需要实例化成具体的值),指令可以单独使用,也可以同resize 和crop 变换结合使用
调整高度和宽度(resize width height)
保持比例(原意是按照比例, 但是根据推测,这里的图片缩小等一定会保持原始图片的显示比例, 然后根据设定的宽度或者高度进行相关的缩放, 否则一定带来图片的比例失真,用户体验会比较差)的缩减图像到指定的尺寸。通常为了缩减等, 可以仅仅使用一个维度的设置(例如仅仅指定,宽度或者高度), 被省略的维度可以用“-”符号替代,在这种情况下, 若是发生错误,则给用户反馈一个415的错误代码(不支持的媒体类型)。这个指令的值可以是一个变量。当使用时伴随一个rotate参数,则缩放完成后进行旋转操作。
裁剪图像到指定的宽高(crop width height)
按照比例将图片调整到合适大小(最后一个维度达到设定值后停止变换, 然后裁剪掉超出的部分。 同上面指令区别是,上面是第一维度达到设定值后,停止变换, 然后将其他地方用背景或者设定的留白填充(我自己推测的, 没去测试))。通常为了缩减等操作, 可以仅仅使用一个维度的设置(例如仅仅指定,宽度或者高度), 被省略的维度可以用“-”符号替代,在这种情况下, 若是发生错误,则给用户反馈一个415的错误代码(不支持的媒体类型)。这个指令的值可以是一个变量。当使用时伴随一个rotate参数,则缩放完成后进行旋转操作。


Nginx原文:
Sets the type of transformation to perform on images:
off
    turns off module processing in a surrounding location.
test
    ensures that responses are images in either JPEG, GIF, or PNG format. Otherwise, the error 415 (Unsupported Media Type) is returned.
size
    outputs information about images in a JSON format, e.g.:
        { "img" : { "width": 100, "height": 100, "type": "gif" } }
    In case of an error, the following is output:
        {}
rotate 90|180|270
    rotates images counter-clockwise by the specified number of degrees. Value of the parameter can contain variables. Can be used either alone, or along with the resize and crop transformations.
resize width height
proportionally reduces an image to the specified sizes. To reduce by only one dimension, another dimension can be specified as “-”. In case of an error, the server will return code 415 (Unsupported Media Type). Values of parameters can contain variables. When used along with the rotate parameter, the rotation happens after reduction.
crop width height
proportionally reduces an image to the size of the largest side and crops extraneous edges by another side. To reduce by only one dimension, another dimension can be specified as “-”. In case of an error, the server will return code 415 (Unsupported Media Type). Values of parameters can contain variables. When used along with the rotate parameter, the rotation happens before reduction.
2. image_filter_buffer
syntax:
image_filter_buffer    size;
default:
image_filter_buffer   1M;
context:
http, server, location
设置用于读取图像缓冲区的最大大小。当一个大小超过设定值,服务器将返回错误415(不支持的媒体类型)。
Nginx原文:
Sets the maximum size of the buffer used for reading images. When a size is exceeded the server will return error 415 (Unsupported Media Type).
3. image_filter_jpeg_quality
syntax:
image_filter_jpeg_quality  quality;
default:
image_filter_jpeg_quality 75;
context:
http, server, location
设置转换jpeg图片的期望图片质量值(质量高,设置值大, 显示效果好, 但是图片尺寸大, 不利于传输)。可以接受的设置值是1到100之间。最大的可以推荐值是95.这个值可以包括一个变量
Nginx原文:
Sets the desired quality of the transformed JPEG images. Acceptable values are in the 1..100 range. Lesser values usually imply both lower image quality and less data to transfer. The maximum recommended value is 95. Value of the parameter can contain variables.
4. image_filter_sharpen
syntax:
image_filter_sharpen   percent;
default:
image_filter_sharpen 0;
context:
http, server, location
增加图片的锐化程度(图像相邻显示像素之间的跳跃变换程度增加, 连续程度降低, 某种程度上可以减少图像模糊感觉, 增加一定的清晰度, 但是会增加图片的颗粒化程度),锐化比例程度可以超过100%, 但是0值是被禁止的。这个在可以包括变量
Nginx原文:
Increases sharpness of the final image. The sharpness percentage can exceed 100. The value of 0 disables sharpening. Value of the parameter can contain variables. 
5. image_filter_transparency
syntax:
image_filter_transparency   on|off;
default:
image_filter_transparency on;
context:
http, server
设定当进行png(具有特定调色板的png图片)或者gif图片处理时, 是否保留透明部分。透明度的损失可以允许获取更好的图像显示效果。Alpha通道的透明性质在png图片中是被保留的。
Nginx原文:
Defines whether transparency should be preserved when transforming PNG images with colors specified by a palette, or in GIF images. The loss of transparency allows to obtain images of a better quality. The alpha channel transparency in PNG is always preserved.  

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
  • svnWebUI:一款现代化的svn服务端管理软件
    svnWebUI是一款图形化管理服务端Subversion的配置工具,适用于非程序员使用。它解决了svn用户和权限配置繁琐且不便的问题,提供了现代化的web界面,让svn服务端管理变得轻松。演示地址:http://svn.nginxwebui.cn:6060。 ... [详细]
  • Node.js学习笔记(一)package.json及cnpm
    本文介绍了Node.js中包的概念,以及如何使用包来统一管理具有相互依赖关系的模块。同时还介绍了NPM(Node Package Manager)的基本介绍和使用方法,以及如何通过NPM下载第三方模块。 ... [详细]
  • 深入解析Linux下的I/O多路转接epoll技术
    本文深入解析了Linux下的I/O多路转接epoll技术,介绍了select和poll函数的问题,以及epoll函数的设计和优点。同时讲解了epoll函数的使用方法,包括epoll_create和epoll_ctl两个系统调用。 ... [详细]
  • 本文介绍了Java后台Jsonp处理方法及其应用场景。首先解释了Jsonp是一个非官方的协议,它允许在服务器端通过Script tags返回至客户端,并通过javascript callback的形式实现跨域访问。然后介绍了JSON系统开发方法,它是一种面向数据结构的分析和设计方法,以活动为中心,将一连串的活动顺序组合成一个完整的工作进程。接着给出了一个客户端示例代码,使用了jQuery的ajax方法请求一个Jsonp数据。 ... [详细]
  • 微信官方授权及获取OpenId的方法,服务器通过SpringBoot实现
    主要步骤:前端获取到code(wx.login),传入服务器服务器通过参数AppID和AppSecret访问官方接口,获取到OpenId ... [详细]
  • 本文介绍了NetCore WebAPI开发的探索过程,包括新建项目、运行接口获取数据、跨平台部署等。同时还提供了客户端访问代码示例,包括Post函数、服务器post地址、api参数等。详细讲解了部署模式选择、框架依赖和独立部署的区别,以及在Windows和Linux平台上的部署方法。 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • Nginx Buffer 机制引发的下载故障
    Nginx ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
author-avatar
林子冰2011
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有