1. 程式人生 > >解決nginx利用image_filter動態生成縮圖不支援bmp

解決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)             {             .....................   elseif (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. static u_char *  

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

  6.     int *size)  

  7. {  

  8. .....................  

  9.     case NGX_HTTP_IMAGE_BMP:  

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

  11.         failed = "gdImageBmpPtr() failed";  

  12.         break;  

  13. .....................  

更改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