首先检查自己的nginx是否有--with-http_image_filter_module扩展
如果没安装可以使用rpm或者yum在线安装的方式 yum -y install gd-devel 进行安装
安装成功后在配置文件中添加代码
只支持jpg格式
location ~* /images/thumbnail/(.*)_(\d+)x(\d+)\.jpg$ {
root /home/yangyongwei/wwwroot/blog/public;
set $s $1;
set $w $2;
set $h $3;
image_filter resize $w $h;
image_filter_buffer 10M;
rewrite ^/images/thumbnail/(.*)$ /images/thumbnail/$s.jpg break;
}
支持jpg png gif 格式
location ~* /images/thumbnail/(.*)_(\d+)x(\d+)\.(jpg|gif|png)$ {
# root /home/yangyongwei/wwwroot/blog/public;
set $h $2;
set $w $3;
if ($h = '0') {
rewrite /images/thumbnail/(.*)_(\d+)x(\d+)\.(jpg|gif|png)$ /images/thumbnail/$1.$4 last;
}
if ($w = '0') {
rewrite /images/thumbnail/(.*)_(\d+)x(\d+)\.(jpg|gif|png)$ /images/thumbnail/$1.$4 last;
}
#根据给定的长宽生成缩略图
image_filter resize $h $w;
#原图最大2M,要裁剪的图片超过2M返回415错误,需要调节参数image_filter_buffer
image_filter_buffer 10M;
#如果报错会出现默认的错误图片;
try_files /images/thumbnail/$1.$4 /images/thumbnail/notfound.jpg;
}
images/thumbnail/a_150x150.jpg 访问格式
参数:
image on/off 是否开启缩略图功能,默认关闭
image_backend on/off 是否开启镜像服务,当开启该功能时,请求目录不存在的图片(判断原图),将自动从镜像服务器地址下载原图
image_backend_server 镜像服务器地址
image_output on/off 是否不生成图片而直接处理后输出 默认off
image_jpeg_quality 75 生成JPEG图片的质量 默认值75
image_water on/off 是否开启水印功能
image_water_type 0/1 水印类型 0:图片水印 1:文字水印
image_water_min 300 300 图片宽度 300 高度 300 的情况才添加水印
image_water_pos 0-9 水印位置 默认值9 0为随机位置,1为顶端居左,2为顶端居中,3为顶端居右,4为中部居左,5为中部居中,6为中部居右,7为底端居左,8为底端居中,9为底端居右
image_water_file 水印文件(jpg/png/gif),绝对路径或者相对路径的水印图片
image_water_transparent 水印透明度,默认20
image_water_text 水印文字 "Power By Vampire"
image_water_font_size 水印大小 默认 5
image_water_font 文字水印字体文件路径
image_water_color 水印文字颜色,默认 #000000