Nginx’s ngx_HTTP_image_filter_module module (Nginx version 0.7.54+) can be used to dynamically convert JPEG, GIF, PNG, and WebP image sizes.

This module is not built by default and needs to be enabled with the –with-http_image_filter_module configuration parameter.

This module can be used if images are not heavily visited.

This module uses the libgd library. The latest version of the library is recommended.

Here are the steps to add the module if you already have Nginx installed.

1 Installation Dependencies

yum -y install gd-devel
yum -y install libxslt-devel
yum -y install perl-devel perl-ExtUtils-Embed
Copy the code

2 View the original nginx parameters

nginx -V

The following output is displayed:

Nginx version: nginx/1.12.2 Configure arguments: Check whether –with-http_image_filter_module is available.

3 to compile

To recompile, you need to download the same version of nginx source code and decompress it.

Add –with-http_image_filter_module=dynamic and the original parameters (do not need to make install to reinstall)

./configure --with-http_image_filter_module=dynamic
Copy the code

4 Back up the original Nginx

cp -r /usr/local/nginx /usr/local/nginxbak

5 covers nginx

cp objs/nginx /usr/local/nginx/sbin/
mkdir /usr/local/nginx/module
cp objs/ /usr/local/nginx/module/
Copy the code

6 Modify the configuration file

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

load_module /usr/local/nginx/module/; location ~* /images/(.+)_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ { set $w $2; set $h $3; image_filter resize $w $h; image_filter_buffer 30M; # set the maximum size of the image buffer. If the size exceeds this value, the server will return error 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/ ; /images/ alias = /User/XX/Desktop/ /User/XX/Desktop/ is the absolute path on the local computer. } location /images { alias /home/publisherupload/ ; /images/ alias = /User/XX/Desktop/ /User/XX/Desktop/ is the absolute path on the local computer. autoindex on; # enable directory browsing}Copy the code

7 Reload the nginx configuration

/usr/local/nginx/sbin/nginx -s

8 test

The final image access address is a _ width X height parameter after the original image address