成都创新互联网站制作重庆分公司

使用nginx动态转换图片大小生成缩略图

Nginx的ngx_http_image_filter_module 模块(nginx版本为0.7.54+) 可用来动态转换JPEG, GIF, PNG, 和WebP格式的图片大小。

创新互联公司专业为企业提供信阳网站建设、信阳做网站、信阳网站设计、信阳网站制作等企业网站建设、网页设计与制作、信阳企业网站模板建站服务,10多年信阳做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

该模块默认没有构建,需要通过 --with-http_image_filter_module 配置参数启用。

如果图片访问量不大,可以使用该模块。

该模块使用了libgd库。 推荐使用该库的最新版本。

以下为在已经安装了nginx的情况下添加该模块的步骤。

1 安装依赖
yum -y install gd-develyum -y install libxslt-develyum -y install perl-devel perl-ExtUtils-Embed2 查看原nginx参数
​ nginx -V

输出结果如下:

​ nginx version: nginx/1.12.2
​ configure arguments:

查看是否有--with-http_image_filter_module,如果有则说明已经安装过,否则继续安装。

3 编译

重新编译时需要下载和原来相同版本的nginx源码,解压。

添加--with-http_image_filter_module=dynamic及原来的参数(不需要执行make install命令重新安装)

./configure --with-http_image_filter_module=dynamicmake4 备份原nginx
cp -r /usr/local/nginx /usr/local/nginxbak5 覆盖nginx
cp objs/nginx /usr/local/nginx/sbin/mkdir /usr/local/nginx/modulecp objs/ngx_http_image_filter_module.so /usr/local/nginx/module/ngx_http_image_filter_module.so6 修改配置文件

vim /usr/local/nginx/conf/nginx.conf

load_module /usr/local/nginx/module/ngx_http_image_filter_module.so;location ~* /images/(.+)_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ { set $w $2; set $h $3; image_filter resize $w $h; image_filter_buffer 30M; #设置图片缓冲区的最大大小,大小超过设定值,服务器将返回错误415 image_filter_interlace on; image_filter_jpeg_quality 80; try_files /$1.$4 /notfound.jpg; expires 1d; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; alias /home/publisherupload/ ; #root 能够让访问 /images/ 路径时,访问到 /User/XX/Desktop/images,如果将 root 替换为 alias,则访问的是 /User/XX/Desktop/;其中 /User/XX/Desktop/ 是本机中的绝对路径。 }location /images { alias /home/publisherupload/ ; #root 能够让访问 /images/ 路径时,访问到 /User/XX/Desktop/images,如果将 root 替换为 alias,则访问的是 /User/XX/Desktop/;其中 /User/XX/Desktop/ 是本机中的绝对路径。 autoindex on; #打开目录浏览功能}7 重载nginx配置
/usr/local/nginx/sbin/nginx -s8 测试

最终的图片访问地址为在原图片地址后面加入_宽度X高度参数

http://110.87.103.58:12345/images/2021-01/202101250944272_500x400.png

参考

到此这篇关于使用nginx动态转换图片大小生成缩略图的文章就介绍到这了,更多相关nginx动态转换图片大小内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!


分享标题:使用nginx动态转换图片大小生成缩略图
网站地址:http://cxhlcq.com/article/cghsoj.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部