解决nginx利用image_filter动态生成缩略图不支持bmp

location ~* /(.+)_(\d+)x(\d+)\.(jpg|gif|png|bmp)$ {
            set $h $2;
            set $w $3;
            if ($h = "0") {
                rewrite /(.+)_(\d+)x(\d+)\.(jpg|gif|png|bmp)$ /img/$1.$4 last;
            }
            if ($w = "0") {
                rewrite /(.+)_(\d+)x(\d+)\.(jpg|gif|png|bmp)$ /img/$1.$4 last;
            }

            #根据给定的长宽生成缩略图
            image_filter resize $h $w;
            #原图最大2M,要裁剪的图片超过2M返回415错误,需要调节参数image_filter_buffer
            image_filter_buffer 2M;

            #error_page  415              /img/notfound.jpg;
            try_files /$1.$4  /notfound.jpg;
        }

这个配置只支持jpg、png、gif。就是不支持bmp


解决办法如下:

添加宏类型定义
      #define NGX_HTTP_IMAGE_BMP 4   
添加mime类型
           static  ngx_str_t ngx_http_image_types[] = {  
          ngx_string( "image/jpeg" ),  
          ngx_string( "image/gif" ),  
          ngx_string( "image/png" ), 
          ngx_string( "image/bmp" )  
          };

添加bmp返回类型
      static  ngx_uint_t  
          ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in)  
          {  
          .....................  
           else   if  (p[0] ==  'B'  && p[1] ==  'M' )  
          {  
           /* BMP */   
           return  NGX_HTTP_IMAGE_BMP;  
          }      
          .....................  
          }  

     static  ngx_int_t 
     ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)  
          {  
          .....................  
           case  NGX_HTTP_IMAGE_BMP:  
                if  (ctx->length < 26) {  
                     return  NGX_DECLINED; 
               }  
          width = p[19] * 256 + p[18]; 
          height = p[23] * 256 + p[22];  
           break ;
          .....................  
          } 
bmp文件格式中的数据是以小端类型存储(低地址存低字节)。虽然bmp文件格式用四字节存储图片的宽度和高度,但两字节已能够存储非常大的值,因此取值时取低两字节即可。取值时 “* 256” 表示向左移8位。
      static  gdImagePtr  
          ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)  
          {  
          ..................... 
           case  NGX_HTTP_IMAGE_BMP:  
               img = gdImageCreateFromBmpPtr(ctx->length, ctx->image);  
               failed =  "gdImageCreateFromBmpPtr() failed" ;  
  1.         break;  

  2. .....................  

  3. }  

  4.   

  5.   

  6. static u_char *  

  7. ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,  

  8.     int *size)  

  9. {  

  10. .....................  

  11.     case NGX_HTTP_IMAGE_BMP:  

  12.         out = gdImageBmpPtr(img, size, -1);  

  13.         failed = "gdImageBmpPtr() failed";  

  14.         break;  

  15. .....................  

更改nginx源码后会编译报错
cc1: warnings being treated as errors
src/http/modules/ngx_http_image_filter_module.c: In function ‘ngx_http_image_source’:
src/http/modules/ngx_http_image_filter_module.c:1082: error: implicit declaration of function ‘gdImageCreateFromBmpPtr’
src/http/modules/ngx_http_image_filter_module.c:1082: error: assignment makes pointer from integer without a cast
src/http/modules/ngx_http_image_filter_module.c: In function ‘ngx_http_image_out’:
src/http/modules/ngx_http_image_filter_module.c:1163: error: implicit declaration of function ‘gdImageBmpPtr’
src/http/modules/ngx_http_image_filter_module.c:1163: error: assignment makes pointer from integer without a cast
make[1]: *** [objs/src/http/modules/ngx_http_image_filter_module.o] Error 1
make[1]: Leaving directory `/usr/local/src/nginx-1.4.7'
make: *** [build] Error 2

查看nginx源码中的src下面的整个目录发现没有gdImageCreateFromBmpPtr这个函数,发现是从gd里面调取,gd2.0不支持bmp。于是重新编译了libgd2.1.0,
编译好后从新编译了更改过的nginx,没有报错,

我的nginx默认是去 /usr/lib64/下面去找libgd.so,编译安装的时候默认的gd路径是/usr/local/lib/下面,所以nginx平滑升级的时候还是出问题了,还好知道问题在哪


编译gd步奏如下

function gd(){

    tar zxf zlib-1.2.8.tar.gz

    cd zlib-1.2.8

    ./configure --prefix=/usr/local/webserver/zlib

    make && make install

    cd ../


    tar zxf jpegsrc.v9a.tar.gz

    cd jpeg-9a/

    ./configure --prefix=/usr/local/webserver/jpeg9 -enable-shared -enable-static

    make && make install

    cd ../

    

    tar zxf libpng-1.6.12.tar.gz

    cd libpng-1.6.12

    ./configure --prefix=/usr/local/webserver/png

    make && make install

    cd ../


    tar zxf freetype-2.5.3.tar.gz

    cd freetype-2.5.3

    ./configure --prefix=/usr/local/webserver/freetype

    make && make install

    cd ../


    tar zxf libgd-2.1.0.tar.gz

    cd libgd-2.1.0

    ./configure --with-jpeg=/usr/local/webserver/jpeg9/ --with-png=/usr/local/webserver/png/ --with-zlib=/usr/local/webserver/zlib/ --with-freetype=/usr/local/webserver/freetype/

    make && make install

    cd ../

    echo "========successfully install gd==========="

}


源文出处http://www.91cto.cn/post/133.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值