Get the App
SLTechnology News&Howtos  ›  Servers  › 

Build nginx with lua module and call ImageMagick tool to realize the function of real-time cutting and zooming pictures

Shulou Source: shulou.com Published: 2022-06-03 04:27:31 10月04日 Update

First, install LuaJIT.

Wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz

Tar xf LuaJIT-2.0.5.tar.gz

Cd LuaJIT-2.0.5

Make

Make install

Set the environment variable, do not switch the console after setting, otherwise it will be invalid.

Export LUAJIT_LIB=/usr/local/lib

Export LUAJIT_INC=/usr/local/include/luajit-2.0

Make a soft link and load the module

Ln-s / usr/local/lib/libluajit-5.1.so.2 / usr/lib/libluajit-5.1.so.2

Ldconfig

Second, install ImageMagick tools.

Just use yum to install it, it's very convenient.

Yum install-y ImageMagick

Download nginx and ngx_devel_kit and nginx_lua_module modules, and extract them to the specified path. If nginx is already installed on this machine, please ignore downloading and installing nginx.

Wget http://nginx.org/download/nginx-1.12.0.tar.gz

Wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

Wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc5.tar.gz

Tar xf nginx-1.12.0.tar.gz & & tar xf v0.3.0.tar.gz & & tar xf v0.10.9rc5.tar.gz

Fourth, compile and install nginx, if the machine already has nginx installed, please ignore the installation, but need to recompile, then backup the old nginx files and get the new one over.

. / configure-- prefix=/usr/local/nginx\

-- add-module= (your decompression path) / ngx_devel_kit-0.3.0\

-- add-module= (your decompression path) / lua-nginx-module-0.10.9rc5

(if nginx is already installed in this machine, add the parameters that your nginx has compiled.)

Make

There is no nginx installed, it is installed by make install directly. Refer to https://cache.yisu.com/upload/information/20200309/32/42291.com$' {

Root / opt/qikanjpg

}

Location ~'/ pdgpath/ ([0-9a murzz] +)\ @ ([0-9] +) x ([0-9] +)\. (gif | jpg | png | jpeg) $'{

Root / opt/qikanjpg

Set $p_w_picpath_root'/ opt/qikanjpg/pdgpath'

Set $fileName $1

Set $width $2

Set $height $3

Set $origin $p_w_picpath_root/$fileName.jpg

Set $file_1 $paired picpathically rootpaced ${fileName} @ ${width} x ${height} .jpg

If (!-f $file_1) {

Rewrite_by_lua'

Local comd = "convert-sample".. ngx.var.width.x ".ngx.var.height.." .ngx.var.width.. ".. ngx.var.file_1

Os.execute (comd)

'

}

}

Location ~'/ pdgpath/ ([0-9a murzz] +)\ @ ([0-9] +) x ([0-9] +) _ f\. (gif | jpg | png | jpeg) $'{

Root / opt/qikanjpg

Set $p_w_picpath_root'/ opt/qikanjpg/pdgpath'

Set $fileName $1

Set $width $2

Set $height $3

Set $origin $p_w_picpath_root/$fileName.jpg

Set $file $paired picpathic root f.jpg ${fileName} @ ${width} x ${height} _ f.jpg

If (!-f $file) {

Rewrite_by_lua'

Local command = "convert-strip-quality 80%-resize".. ngx.var.width.x ".ngx.var.width.." ^-gravity center-crop ".ngx.var.width.x" .ngx.var.width.. "x" .ngx.var.width.. "+ zero" .ngx.var.width.. "" .ngx.var.file ".

Os.execute (command)

'

}

}

7. Visit the pictures and test them.

With the configuration file above, put a 1.jpg in / opt/qikanjpg/pdgpath/.

Original http://(your_server_ip):(your_server_port)/pdgpath/1.jpg

Scale the original http://(your_server_ip):(your_server_port)/pdgpath/1@ width x height equal to the smaller values of width and height. JPG

Scale the original image equally according to the larger values of width and height, and then cut the width and height of the original image again from the middle. One area http://(your_server_ip):(your_server_port)/pdgpath/1@ width x height _ f.jpg

For more cutting functions, please check convert-- help to figure it out and add it to local command =.

Tags: Original path native compilation module file function picture tool larger middle region parameter variable backup more environment link facet switch Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Microsoft Docker MariaDB