nignx实现图片截图与缓存 一

一,使用Nginx插件 image_filter

  1. 在nginx.conf配置如下
    # 我使用16进制数的方式给图片重命名
    location~*/(.*)\/([0-9a-f]+)_(\d+)x(\d+)\.(jpg|png|jpeg|gif)${
       # 如果存在文件就终止规则
       if(-f $request_filename){
           break;
       }
       
       # 设定一些参数
       set$filepath $1;
       set$filename"$2.$5";
       set$thumb    "$2_$3x$4.$5";
       set$width    $3;
       set$height   $4;
       
       # 如果原文件不存在可以直接返回404
       if(!-f $document_root/$filepath/$filename){
           return404;
       }
       # 重写URL
       rewrite/(.*)\/([0-9a-f]+)_([0-9x]+)\.(jpg|png|jpeg|gif)/$1/$2.$4break;
       # 执行图片缩放
       image_filter test;
       image_filter resize $width $height;
       image_filter_jpeg_quality75;
    }

     

  2. 重启nginx ,然后就可以访问了 : http://localhost:8080/mglsoft/1_600x600.jpg

  3. 在公司重新整理了一下

      location ~* /(.*)\/([0-9a-f]+)\.(jpg|png|jpeg|gif)@(\d+)x(\d+)Q_([rc])$ {
                if (-f $request_filename) {
                    break;
                }
                set $filepath $1;
                set $filename "$2.$3";
                set $thumb    "$2_$4x$5.$3";
                set $width    $4;
                set $height   $5;
                set $type    $6;
                if ( $type = 'r' ){
                    set  $type   'image-resize';   #旋转
                }
            
                if ( $type = 'c' ){
                  set $type  'image-crop'; #剪切
               }
            
                if (!-f $document_root/$filepath/$filename) {
                    return 404;
                }
                rewrite /(.*)\/([0-9a-fx_]+)\.(.*) /imgcache/$2.$4;
                if (!-f $request_filename) {
                    proxy_pass $scheme://127.0.0.1:$server_port/$type/$filepath/$filename?width=$width&height=$height;
                    break;
                }
                proxy_store          $document_root/imgcache/$thumb;
                proxy_store_access   user:rw  group:rw  all:r;
                proxy_set_header     Host $host;
                expires  10d; # 设置图片过期时间10天
            }
            location /image-resize {
                rewrite /(image-resize)/(.*) /$2 break;
                image_filter resize $arg_width $arg_height;
                image_filter_jpeg_quality 75;
                #allow 127.0.0.0/8;
                #deny all;
            }
            location /image-crop {
                rewrite /(image-crop)/(.*) /$2 break;
                image_filter crop $arg_width $arg_height;
                image_filter_jpeg_quality 75;
                #allow 127.0.0.0/8;
                #deny all;
            }
        }
    1. 通过以下路径访问: http://aidemofang.com/test/a.jpg@240x210Q_r

 

参考:

   http://0x1.im/blog/nginx/use-nginx-image-filter-module.html

   https://blog.wangjunfeng.com/archives/669

 

来源:http://hihubs.com/article/268

转载于:https://my.oschina.net/273579540/blog/847590

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值