nginx 編譯的時候增加 ./configure --with-http_image_filter_module
配置如下
server
{
listen 80;
server_name 192.168.0.156;
index index.html index.htm index.php;
root /opt/htdocs;
access_log /opt/local/nginx/logs/access.log main;
location ~* (.*)/(\d+)\.(jpg|png|gif)$
{
set $h $arg_h;
set $w $arg_w;
#image_filter crop $h $w;
image_filter resize $h $w;
}
location ~* /(\d+)_(\d+)x(\d+)\.(jpg|png|gif)$
{
if ( -e $document_root/$1.$4 )
{
rewrite /(\d+)_(\d+)x(\d+)\.(jpg|png|gif)$ /$1.$4?h=$2&w=$3 last;
}
return 404;
}
}
縮圖使用.. 圖片儲存在 /opt/htdocs/upload/ 目錄下面.. 如 0.jpg
原圖訪問 http://192.168.0.156/upload/0.jpg
縮略100x100 訪問 http://192.168.0.156/upload/0_100x100.jpg
縮略300x300 訪問 http://192.168.0.156/upload/0_300x300.jpg
如果原圖不存在 將會返回 404 !!
生成縮圖到本地硬碟中
server
{
listen 80;
server_name 192.168.0.156;
index index.html index.htm index.php;
root /opt/htdocs;
access_log /opt/local/nginx/logs/www.xxx.com.log main;
location ~* ^/resize
{
set $width 100;
set $height 100;
set $dimens "";
if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" )
{
set $width $1;
set $height $2;
set $image_path $3;
set $demins "_$1x$2";
}
if ($uri ~* "^/resize/(.*)" )
{
set $image_path $1;
}
set $image_uri image_resize/$image_path?width=$width&height=$height;
if (!-f $request_filename) {
proxy_pass http://192.168.0.156/$image_uri;
break;
}
proxy_store /opt/htdocs/upload/resize$demins/$image_path;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
expires 30d;
access_log off;
}
location /image_resize {
alias /opt/htdocs/;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
access_log off;
}
}
縮圖使用.. 圖片儲存在 /opt/htdocs/upload/ 目錄下面.. 如 0.jpg
原圖訪問 http://192.168.0.156/upload/0.jpg
訪問 http://192.168.0.56/resize/upload/0.jpg
location ~* ^/resize
{
set $width 100;
set $height 100;
set $dimens "";
縮略為定義的 100 x 100
當縮圖存在時間
訪問 http://192.168.0.156/resize_100x100/upload/0.jpg
if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" )
{
set $width $1;
set $height $2;
set $image_path $3;
set $demins "_$1x$2";
}
if ($uri ~* "^/resize/(.*)" )
{
set $image_path $1;
}
縮略為 resize_$width x $height 縮略定義大小
當縮圖不存在時,使用直接縮略 圖片質量為 75%
set $image_uri image_resize/$image_path?width=$width&height=$height;
location /image_resize {
alias /opt/htdocs/;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
access_log off;
訪問 http://192.168.0.156/image_resize/upload/0.jpg?width=100&height=100
並利用 Nginx proxy_store 快取縮略的圖片到本地
if (!-f $request_filename) {
proxy_pass http://192.168.0.156/$image_uri;
break;
}
proxy_store /opt/htdocs/upload/resize$demins/$image_path;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
expires 30d;
access_log off;
}
訪問 http://192.168.0.156/resize_100x100/upload/0.jpg
生成圖片的路徑為 /opt/htdocs/upload/resize_100x100/upload/0.jpg