Nginx generates thumbnails in real time

Source: Internet
Author: User
: This article mainly introduces how to generate thumbnails in Nginx in real time. For more information about PHP tutorials, see. Thumbnails are generated in a variety of ways, such as using java to generate thumbnails, you can also use nginx + lua implementation, we will explain the use of nginx built-in module to generate a thumbnail, Module:-with-http_image_filter_module.

1. install nginx

: Http://nginx.org/download/

1. use root to install dependencies

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

2. download nginx and install

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

II. configuration

Scaling is a cpu-consuming operation. if the site has a large access volume, you 'd better consider using a program to generate a thumbnail on the hard disk, or adding a cache on the front end or using CDN. So the following configuration will save the generated thumbnail to the hard disk for next access

Create an image directory: 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; # The Command generates the corresponding thumbnail image_filter_jpeg_quality 75 according to the height and width parameters; image_filter_buffer 10 m; access_log off ;}
Generating thumbnails is only one of the image_filter functions. it supports four parameters:

Test: whether the returned result is an image.
Size: returns the size of the image in json format.
Corp: captures a part of an image starting from the upper left corner. if the size is smaller, the image will be cut.
Resize: scales the image, and scales proportionally.


The process for generating a thumbnail is as follows:
1. the source image is in/home/slim/img_site/localhost/images/a.png. I need a 100x100 thumbnail.
2. request http: // 192.168.36.54: 8080/resize_100x100/image/1.jpg.
3. the request enters the location ~ * ^/Resize, and then determine whether the image exists in the image_path Directory. If the image exists, it is directly returned to the user,
4. if it does not exist, the page will jump to http: // 192.168.36.54: 8080/image_resize/image/1.jpg? Width = 100 & height = 100;
5. location/image_resize performs the scaling function based on the input width and height, and sets the image quality to 75.
6. Generate the file/home/slim/img_site/localhost/resize_100x100/images/a.png to the hard disk.

III. start and test

Create the image directory images under/home/slim/img_site/localhost/images and copy several images.

Start nginx:

./Nginx/sbin/nginx

Access http: // 192.168.36.54: 8080/resize_100x100/images/a.png.

References:

1. Explore http_image_filter_module in the Nginx Image cropping module

The preceding section describes how to generate thumbnails in Nginx in real time, including some content. if you are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.