tutorial on configuring thumbnail functionality with modules that are nginx in the _nginx

Source: Internet
Author: User
Tags image filter

Http_image_filter_module is an integrated image processing module provided by Nginx, Support nginx-0.7.54 later version, when the website traffic is not very high disk limited do not want to generate redundant picture files under the premise can, you can use it to zoom in real time pictures, rotate pictures, verify the validity of pictures and get picture width and image type information, because it is the result of real-time calculation, so the Web site traffic, do not recommend use.
Installation
installation is still very simple, the default Httpimagefiltermodule module is not compiled into the nginx, so to be specified at configure time

./configure arguments:--prefix=/usr/local/nginx--with-http_image_filter_module

Ps:httpimagefiltermodule modules need to rely on gd-devel support and can be easily installed using Yum or Apt-get if return is not installed "/configure:error:the HTTP image Filter module requires the GD library. Error

Yum Install Gd-devel
Apt-get Install libgd2-xpm Libgd2-xpm-dev

After Make&&make install can be configured, do the simplest configuration, first let the module can run up ^ ^

Location ~/simg/.*\.jpg$ {
  #proxy_pass   http://10.11.11.11;
  #rewrite "/simg/(. *\.jpg) $"/img/$1 break;
  Image_filter  resize;
  Error_page   415  =/empty;
}

The module has two main instructions:
Directive 1:
Syntax: image_filter (Test | size | Resize width Height | crop width height)
The default is: None
The context that can occur: location
This instruction specifies the form of the image to be transformed:
(1) test-tests whether the reply is a JPEG, GIF, or PNG picture (other formats such as BMP are not supported) and returns 415 when an error occurs.
(2) Size-Returns the JSON data for a picture, such as: ( "IMG": ("width": +, "height": +, "type": "GIF"))
(3) Resize-according to the set of proportional to reduce the image, such as 100*100 picture, and the setting is 50*25, the reduced picture for 25*25. If you only want to set a dimension, you can use "-" instead. Returns 415 when an error occurs.
(4) Crop-reduces the image proportionally to the setting, and then crops the picture to the same size as the setting. For example 100*100 picture, but the setting is 50*25, the reduced picture for 50*50,nginx will choose the middle height 25 pixel, forms the 50*25 picture, therefore the picture may have the flaw. If you only want to set a dimension, you can use "-" instead. Returns 415 when an error occurs.
Directive 2:
Syntax: Image_filter_buffer size
Default value: Image_filter_buffer 1M
Where to appear: HTTP, server, location
The directive sets the maximum value for a single picture cache, and returns 415 if the filtered picture is larger than the cache size.

Usage
starting now is the focus:
With the understanding and then with Locaiont, if, image_filter can let nginx dynamic generation of thumbnails.
Suppose your picture is in the/IMG directory
Access thumbnail mode

Yun_qi_img/9gumjr7200aj0003_90x90.jpg

How to access the original artwork

Yun_qi_img/9gumjr7200aj0003_90x0.jpg
yun_qi_img/9gumjr7200aj0003_0x50.jpg
yun_qi_img/9gumjr7200aj0003_ 0x0.jpg
yun_qi_img/9gumjr7200aj0003.jpg

Add the following configuration to the server context

 Location ~*/img/(. +) _ (d+) x (d+).
      (jpg|gif|png) $ {set $h $;
      Set $w $; 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;
      #根据给定的长宽生成缩略图 image_filter resize $h $w;             
      
      #原图最大2M, to crop the picture more than 2M return 415 error, need to adjust the parameters image_filter_buffer Image_filter_buffer 2M;
      #error_page 415/img/notfound.jpg; 
    try_files/img/$1.$4/img/notfound.jpg; } location ~*/img {} location ~*/img/(. +) _ (d+) x (d+).
      (jpg|gif|png) $ {set $h $;
      Set $w $; 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;
      #根据给定的长宽生成缩略图 image_filter resize $h $w; #原图最大2M, to crop the picture more than 2M returns 415 error, need to adjust parameter image_filter_buffer imaGe_filter_buffer 2M;
      #error_page 415/img/notfound.jpg; 
    try_files/img/$1.$4/img/notfound.jpg;
 } location ~*/img {}

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.