Heim  >  Artikel  >  Backend-Entwicklung  >  Nginx 实时生成缩略图

Nginx 实时生成缩略图

WBOY
WBOYOriginal
2016-08-08 09:26:37973Durchsuche

缩略图的生成有多种方式,如使用java生成缩略图,也可以使用nginx+lua实现,下面我们讲解一下使用nginx自带的模块生成缩略图,模块:-with-http_image_filter_module。

一、安装nginx

下载地址:http://nginx.org/download/

1.使用root安装依赖

yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel gd gd-devel

2.下载nginx,安装

wget http://nginx.org/download/nginx-1.7.9.tar.gz

mkdir nginx

tar -zxvf nginx-1.7.9.tar.gz

cd nginx-1.7.9

./configure  --prefix=/home/slim/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_image_filter_module

make 

make install

二、配置

生成缩略是个消耗cpu的操作,如果访问量比较大的站点,最好考虑使用程序生成缩略图到硬盘上,或者在前端加上cache或者使用CDN。所以下面我们配置将生成的缩略图保存到硬盘供下次访问

创建图片目录:mkdir img_site

location ~* ^/resize {
                root /home/slim/img_site/$server_name;
                set $width 150;
                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.36.54:8080/$image_uri;
                                break;
                }
                proxy_store /home/slim/img_site/$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 /image_resize {
                alias /home/slim/img_site/$server_name;#原图目录
                image_filter resize $arg_width $arg_height; #指令根据height和width参数生成相应缩略图
                image_filter_jpeg_quality 75;
                image_filter_buffer 10m;
                access_log off;
        }
生成缩略图只是image_filter功能中的一个,它一共支持4种参数:

test:返回是否真的是图片
size:返回图片长短尺寸,返回json格式数据
corp:截取图片的一部分,从左上角开始截取,尺寸写小了,图片会被剪切
resize:缩放图片,等比例缩放


生成缩略图流程如下:
1、原图在/home/slim/img_site/localhost/images/a.png。我需要一份100×100的缩略图。
2、请求http://192.168.36.54:8080/resize_100x100/image/1.jpg.
3、这个请求进入了location ~* ^/resize,接着判断image_path这个目录下是否存在这张图片,如果存在直接放回给用户,
4、不存在那么跳转到http://192.168.36.54:8080/image_resize/image/1.jpg?width=100&height=100;
5、location /image_resize根据传入的width和height执行缩略功能,并且设置图像质量为75
6、接着生成文件/home/slim/img_site/localhost/resize_100x100/images/a.png缩略图到硬盘上

三、启动,测试

在/home/slim/img_site/localhost/images下新建图片目录images,copy几张图片过来。

启动nginx:

./nginx/sbin/nginx

访问http://192.168.36.54:8080/resize_100x100/images/a.png试试。

参考文章:

1.Nginx图片剪裁模块探究 http_image_filter_module

以上就介绍了Nginx 实时生成缩略图,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn