Auto crop images using Nginx

Source: Internet
Author: User
Tags crop image perl script

Use Nginx auto crop image auto crop using Nginx Image Auto crop conform to the specifications of the image method: Method 1: nginx built-in module HttpImageFilterModule: http://wiki.nginx.org/httpimagefiltermodulein compilation to take parameters -- with-http_image_filter_module configuration nginx: # vi nginx. conflocation ~ * ^/Img/pro /(. *) _ RsT _ (\ d *) x (\ d *)\. jpg $ {root/data/store/newvideo/video/ZhongXun/Web_images; rewrite/img/pro /(. *) _ RsT _ (\ d *) x (\ d *)\. jpg/$1 break; image_filter resize $2 $3;} when using method 1, I do not know whether it is my character problem or what is going on, as long as the image to be cropped exceeds 1 M, no matter how large the image_filter_buffer parameter is, the Error 415 is always reported. no solution is found on the Internet. You can only use method 2 to solve the problem. Method 2: Embedded perl script to implement NginxEmbeddedPerlImageResize: yum install ImageMagick-perl in nginx compilation to bring parameters -- with-http_perl_module configuration nginx: http {perl_modules perl/lib; perl_require resize. pm; server {............. location ~ * ^/Img/pro /(. *) _ RsT _ (\ d *) x (\ d *)\. jpg $ {root/data/store/images; rewrite/img/pro /(. *) _ RsT _ (\ d *) x (\ d *)\. jpg/$1 break; image_filter resize $2 $3; error_page 415 =/FIG rst_.20.2x1_3.jpg;} location/pic {root/data/store/images/; if (! -F $ request_filename) {rewrite ^ (. * optional (.jpg |. JPG |. gif |. GIF |. png |. PNG) $/resize $1 $2 last ;}} location/resize {perl resize: handler ;}} resize. the pm content is as follows: package resize; use nginx; use Image: Magick; our $ base_dir = "/data/store/images"; # note, if nginx is started by another user, the user must use the write permission our $ image; sub handler {my $ r = shift; return DECLINED unless $ r-> uri = ~ M/\. jpg_RsT _ \. \ d {1 ,}? X \ d {1 ,}? \./; My $ uri = $ r-> uri; $ uri = ~ S! ^/Resize !!; My $ dest_file = "$ base_dir/$ uri"; my @ path_tokens = split ("/", $ uri); my $ filename = pop @ path_tokens; my @ filename_tokens = split ('\. ', $ filename); # We know the last part is the extension; # We know the one before that is the dimensions # We know that the one before that is the resize_to string my $ ext = pop @ filename_tokens; my $ dimensions = pop @ filename_tokens; pop @ filename_tokens; $ filename = join ('. ', @ filename_tokens, $ ext); my $ real_file_path = join ("/", $ base_dir, @ path_tokens, $ filename); return DECLINED unless-f $ real_file_path; my ($ width, $ height) = split ("x", $ dimensions); if ($ height <1) {$ dimensions = $ width;} $ image = new Image:: Magick; $ image-> Read ($ real_file_path); $ image-> Scale ($ dimensions); $ image-> Write ($ dest_file ); $ r-> sendfile ($ dest_file); return OK ;}

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.