NGX_IMAGE_THUMB module generates thumbnails
Ngx_image_thumb is a nginx used to generate thumbnails of the module, the existence of thumbnails of many methods, the main function of the Nginx module is to request the image of the thumbnail/watermark processing, support text watermark and image watermark. Support custom font, text size, watermark transparency, watermark location, judge whether the original image is larger than the specified size to deal with and so on.
1. Compiling method
before compiling, verify that your system has a Libcurl-dev Libgd2-dev Libpcre-dev dependent library installed
1.1 Debian/ubuntu System Example
# If you do not install the GCC-related environment to perform
$ sudo apt-get install build-essential M4 autoconf automake make
$ sudo apt-get install D2-noxpm-dev Libcurl4-openssl-dev Libpcre3-dev
1.2 Centos/redhat/fedora
# Please make sure you have installed GCC automake autoconf M4
$ sudo yum install gd-devel pcre-devel libcurl-devel
1.3 Freebsd/netbsd/openbsd
# Not much said, I use port to Libcurl-dev Libgd2-dev Libpcre-dev installed on it
# before compiling please ensure that GCC Automake autoconf is installed M4
1.4 Windows
# also supported, but to modify the code too much, including Nginx itself, with VC + + to compile
# Too much trouble can be compiled with Cygwin. It is still not recommended that you do so, use the Unix/linux operating system.
2. Nginx/tengine Installation
Choose Nginx or Tengine, see for yourself, choose one of the two
2.1 Download Tengine
# wget http://tengine.taobao.org/download/tengine-1.4.5.tar.gz
# TAR-ZXVF tengine-1.4.5.tar.gz
# CD tengine-1.4.5
2.2 Download Nginx
# wget http://nginx.org/download/nginx-1.4.0.tar.gz
# TAR-ZXVF nginx-1.4.0.tar.gz
# CD nginx-1.4.0
2.3 Installation Module
# wget Https://github.com/3078825/nginx-image/archive/master.zip
# unzip Master.zip
#/configure- Add-module=./nginx-image-master
# make
# make install
3. Configure
Location/{
root html;
#添加以下配置
image on;
Image_output on;
}
4. Parameter
- Image On/off whether to turn on the thumbnail feature, by default
- Image_backend On/off Whether the mirroring service is turned on, when the function is turned on, the request directory does not exist in the picture (judge the original image), will automatically download from the mirror server address
- Image_backend_server Mirror server address
- Image_output On/off If the picture is not generated and is processed directly after the output default off
- Image_jpeg_quality 75 The quality default value of the JPEG picture generated 75
- Image_water On/off Whether the watermark function is turned on
- Image_water_type 0/1 Watermark Type 0: Image watermark 1: Text watermark
- Image_water_min 300 300 Picture width 300 height 300 to add watermark
- Image_water_pos 0-9 Watermark Position Default value 9 0 is random position, 1 for the top left, 2 for the top center, 3 for the top right, 4 for the middle of the left, 5 for the Middle center, 6 for the middle of the right, 7 for the bottom of the left, 8 for the end of the center, 9 for the bottom right
- Image_water_file watermark File (jpg/png/gif), absolute path, or relative path watermark picture
- image_water_transparent watermark Transparency, default 20
- Image_water_text watermark Text "Power by Vampire"
- image_water_font_size Watermark Size Default 5
- Image_water_font text watermark font file path
- Image_water_color watermark text color, default #000000
4.1 Invocation Description
This assumes that your nginx access address is HTTP://127.0.0.1/
And there is a test.jpg picture in the Nginx site root directory
By accessing
Yun_qi_img/test.jpg!c300x200.jpg will generate/output thumbnails of test.jpg 300x200
Where c is the parameter that generates the thumbnail of the picture, 300 is the width of the thumbnail that is generated 200 is the height of the thumbnail
Four different types of thumbnails can be generated altogether.
Support Jpeg/png/gif (gif to static picture after generation)
The C parameter intercepts the picture at the height of the request at 10%, then zooms/enlarges to the specified size (picture thumbnail size equals requested width)
The M parameter centers the screenshot picture by the request wide height, then scales/enlarges to the specified size (picture thumbnail size equals request width height)
The T parameter is scaled/enlarged proportionally to the specified size by the requested width and height (picture thumbnail size may be smaller than the requested width)
The W parameter is scaled/enlarged to the specified size in the requested width and height, padding white background color (picture thumbnail size equals request width height)
5. Call examples
Yun_qi_img/test.jpg!c300x300.jpg
yun_qi_img/test.jpg!t300x300.jpg
yun_qi_img/test.jpg!m300x300.jpg
yun_qi_img/test.jpg!w300x300.jpg
yun_qi_img/test.c300x300.jpg
yun_qi_img/test.t300x300.jpg
Yun_qi_img/test.m300x300.jpg
yun_qi_img/test.w300x300.jpg
Nginx real-time thumbnail to hard drive
now with the advent of each terminal (cell phone, ipad and other tablets, as well as the various terminals of the mobile phone resolution and size are different, and now the mobile phone user traffic is treasure, the network has a variety of generated thumbnail function of the architecture, with PHP real-time generation of thumbnails, but also useful nginx + LUA implementation , I also talked about the use of nginx to generate thumbnails, but the user needs to generate one time per visit, the CPU and hard disk will bring more pressure, today brings another way, this time using Nginx to generate thumbnails to the hard disk. Look at my configuration.
1. First of all, build cache directory
2. Modify Nginx Configuration
location ~* ^/resize {root/data/site_cache/$server _name;
Set $width 150;
Set $height 100;
Set $dimens "";
if ($uri ~* "^/resize_ (\d+) x (\d+)/(. *)") {set $width $;
Set $height $;
Set $image _path $;
Set $demins "_$1x$2";
} if ($uri ~* "^/resize/(. *)") {set $image _path $;
The set $image _uri image_resize/$image _path?width= $width &height= $height;
if (!-f $request _filename) {proxy_pass http://127.0.0.1/$image _uri;
Break
proxy_store/data/site_cache/$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/data/site/$server _name/;
Image_filter Resize $arg _width $arg _height;
Image_filter_jpeg_quality 75;
Access_log off; }
The
Generate thumbnail flow is as follows:
(1), the original image in Www.jb51.net/image/1.jpg. I need a thumbnail of the 100x100.
(2), request www.jb51.net/resize_100x100/image/1.jpg.
(3), this request entered the location ~* ^/resize, and then Judge Image_path This directory is the existence of this picture, if there is a direct return to the user,
(4), does not exist then jump to the http://www.jb51.net/ image_resize/image/1.jpg?width=100&height=100;
(5), Location/image_resize performs the thumbnail function according to the incoming width and height, and sets the image quality to
(6), and then generates the file to/data/site_cache/www.jb51.net/ Resize_100x100/image/1.jpg
(7), and return the picture to the user
(8), nginx to generate thumbnails on the hard disk The function is over here.