解決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") };-
break;
-
.....................
-
}
-
static u_char *
-
ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,
-
int *size)
-
{
-
.....................
-
case NGX_HTTP_IMAGE_BMP:
-
out = gdImageBmpPtr(img, size, -1);
-
failed = "gdImageBmpPtr() failed";
-
break;
-
.....................
-
}
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