Nginx generates thumbnails in real time
缩略图的生成有多种方式,如使用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; }
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教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
