FastDFS通过nginx实现图片压缩裁剪配置

 图片压缩裁剪

利用Nginx的image_filter模块实现:

nginx_http_image_filter_module在nginx 用于对JPEG, GIF和PNG图片进行转换处理(压缩图片、裁剪图片、旋转图片)。这个模块默认不被编译,所以要在编译nginx源码的时候,加入相关配置信息

(1)安装gd,HttpImageFilterModule模块需要依赖gd-devel的支持

yum -y install gd-devel

(2)编译安装nginx

wget http://nginx.org/download/nginx-1.15.4.tar.gz #下载nginx压缩包
tar -zxvf nginx-1.15.4.tar.gz #解压
cd nginx-1.15.4/
./configure --prefix=/usr/nginx-1.15 \
--with-http_gzip_static_module \
--add-module=/usr/local/src/fastdfs-nginx-module/src/ \ #添加fastdfs-nginx-module模块
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module 添加http_image_filter_module,实现图片压缩裁剪
make && make install

(3)编辑nginx.conf文件

server {
    listen       8888;    ## 该端口为storage.conf中的http.server_port相同
    server_name  localhost;

    location ~group1/M00/(.+)_(\d+)x(\d+)\.(jpg|gif|png|jpeg){ # .表示除\n外的任意字符 +表示1-正无穷 \d表示数字
            alias /data/dfs/data/; #设置别名
             ngx_fastdfs_module;
             set $w $2; #正则表达式匹配的第二个参数,此处为第一个(\d+)
             set $h $3;
             if ($w != "0") {
                  rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png|jpeg)$ group1/M00$1.$4 break;
              }set $h $3;
             if ($h != "0") {
                  rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png|jpeg)$ group1/M00$1.$4 break;
              }
            image_filter resize $w $h;
            image_filter_buffer 50M; ##原图最大50M,要裁剪的图片超过2M返回415错误
        }

    location ~/group[0-9]/ {
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}

如下添加裁剪的参数进行裁剪

http://file.test.com:8888/group1/M00/00/00/rBIAA10ljqWAMV-zAABdrSqbHGQ439_400x200.jpg

其他参数配置

 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;
 #旋转指定度数的图像,该度数只能是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通道总是保留透明度。

参考:https://blog.csdn.net/u012965203/article/details/96651061

posted @ 2020-07-07 16:02  suminem  阅读(1400)  评论(0编辑  收藏  举报