Nginx 配置实现缩略图及图片裁切
2019-04-25 13:55:08
11
0
0

admin
# http://host/crop/300x300/25733822_112042031030_2.jpg
#需要注意 proxy_pass 的路径为图片的真实访问路径
#配置图片的真实访问路径的时候使用image_filter即可实现图片的缩放
location ~* ^/crop {
root /data/site_cache/$server_name;
set $width 150;
set $height 100;
set $dimens "";
set $crop 0;
if ( $uri ~* "^/crop/(200x200|300x300|400x400|500x500|600x600)" ) {
set $crop 1;
}
if ($crop = 0) {
return 403;
}
if ($uri ~* "^/crop/(.*)" ) {
set $image_path $1;
}
if ($uri ~* "^/crop/(\d+)x(\d+)/(.*)" ) {
set $width $1;
set $height $2;
set $image_path $3;
set $demins "_$1x$2";
}
set $image_uri /crop_image/$image_path?width=$width&height=$height;
if (!-f $request_filename) {
proxy_pass http://host$image_uri;
break;
}
proxy_store /data/site_cache/$server_name/crop$demins/$image_path;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
expires 30d;
access_log off;
}
location ^~ /crop_image/ {
alias /vhosts/hosts/travelzs/public/uploads/;
image_filter size;
image_filter crop $arg_width $arg_height;
image_filter_jpeg_quality 75;
image_filter_buffer 2M;
#access_log off;
}
# http://host/resize/300x300/25733822_112042031030_2.jpg
#需要注意 proxy_pass 的路径为图片的真实访问路径
#配置图片的真实访问路径的时候使用image_filter即可实现图片的缩放
location ~* ^/resize {
root /data/site_cache/$server_name;
set $width 150;
set $height 100;
set $dimens "";
set $resize 0;
if ( $uri ~* "^/resize/(200x200|300x300|400x400|500x500|600x600)" ) {
set $resize 1;
}
if ($resize = 0) {
return 403;
}
if ($uri ~* "^/resize/(.*)" ) {
set $image_path $1;
}
if ($uri ~* "^/resize/(\d+)x(\d+)/(.*)" ) {
set $width $1;
set $height $2;
set $image_path $3;
set $demins "_$1x$2";
}
set $image_uri /uploads/$image_path?width=$width&height=$height;
if (!-f $request_filename) {
proxy_pass http://host$image_uri;
break;
}
proxy_store /data/site_cache/$server_name/resize$demins/$image_path;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
expires 30d;
access_log off;
}
location ~* ^/uploads/ {
image_filter size;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
image_filter_buffer 2M;
access_log off;
}
没有帐号? 现在注册.