Nginx uses image_filter to dynamically generate thumbnails

Source: Internet
Author: User

"I need to generate thumbnails for some images now. Now I have added image_filter, but I don't know how to access the source image I uploaded"

At the beginning, I felt that it was not easy to use the program area for processing. In fact, I could use a little brains to analyze it, or I could not modify the program to dynamically generate thumbnails and access the source image.

The premise is that you need to set the image access rules.

Let's take a look at what is the nginx image filter module.

Httpimagefiltermodule is used to crop an excessively large image to a specified size. It is a built-in module of nginx and is not enabled by default.
To enable httpimagefiltermodule, You need to include the parameter -- with-http_image_filter_module at compilation

This module has two commands:
Syntax: image_filter (test | size | resize width height | crop width height)
Default Value: None
Context: Location

This command specifies the image conversion format:

Test-test whether the response is in JPEG, GIF, or PNG format (other formats such as BMP are not supported). If an error occurs, 415 is returned.
Size-return the JSON data of the image, for example: ("IMG": ("width": 100, "height": 100, "type": "GIF "))
Resize-reduce the image proportionally according to the settings, for example, the image size of 100*100 is set to 50*25, and the image size after reduction is 25*25. If you only want to set a dimension, you can use "-" instead. If an error occurs, 415 is returned.
Crop-scale down the image according to the settings, and then crop it to an image of the same size as the settings. For example, if the image is 100*100 and the image is set to 50*25, the reduced image is 50*50, and nginx selects 25 pixels in the middle to form a 50*25 image, therefore, images are missing. If you only want to set a dimension, you can use "-" instead. If an error occurs, 415 is returned.

Syntax: image_filter_buffer size
Default Value: image_filter_buffer 1 m
Available locations: HTTP, server, and location

This command sets the maximum cache size for a single image. If the size of the filtered image exceeds the cache size, an Error 415 is returned.

Focus at the beginning:

With the above knowledge, coupled with locaiont, if, and image_filter, nginx can dynamically generate thumbnails.

Assume that your image is in the/IMG directory.

Access to thumbnails

Http://www.xxx.cn/img/9GUMJR7200AJ0003_90x90.jpg

Source image access method

Http://www.xxx.cn/img/9GUMJR7200AJ0003_90x0.jpg

Http://www.xxx.cn/img/9GUMJR7200AJ0003_0x50.jpg

Http://www.xxx.cn/img/9GUMJR7200AJ0003_0x0.jpg

Http://www.xxx.cn/img/9GUMJR7200AJ0003.jpg

Add the following configuration to the server context:

Location ~ */Img /(. +) _ (\ D +) x (\ D + )\. (JPG | GIF | PNG) $ {set $ h $2; set $ W $3; if ($ H = "0") {rewrite/img /(. +) _ (\ D +) x (\ D + )\. (JPG | GIF | PNG) $/img/$1. $4 last;} if ($ W = "0") {rewrite/img /(. +) _ (\ D +) x (\ D + )\. (JPG | GIF | PNG) $/img/$1. $4 last ;}# generate the thumbnail image_filter resize $ h $ W based on the given length and width; # the maximum size of the source image is 2 MB. If the image to be cropped exceeds 2 MB, a 415 error is returned, you need to adjust the image_filter_buffer 2 m; # error_page 415/img/notfound.jpg; try_files/img/$1. $4/I Mg/notfound.jpg;} location ~ */IMG {}

Http://wiki.nginx.org/HttpImageFilterModule

Nginx uses image_filter to dynamically generate thumbnails

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.