{"id":3165,"date":"2014-10-19T18:15:27","date_gmt":"2014-10-19T23:15:27","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=3165"},"modified":"2014-10-19T18:21:04","modified_gmt":"2014-10-19T23:21:04","slug":"nginx-wordpress-images-with-ngx_http_image_filter_module","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/nginx-wordpress-images-with-ngx_http_image_filter_module\/","title":{"rendered":"Dynamically resizing WordPress images on nginx with ngx_http_image_filter_module"},"content":{"rendered":"<p>Ever notice that image galleries on WordPress.com load faster than those on your own? WordPress.com can serve dynamically-resized images on-the-fly to improve page load speeds and create pretty-looking image galleries. You can achieve this on your own server too, with a few simple tricks in your nginx.conf, taking advantage of URL parameters WordPress appends to image uploads and the nginx libgd-based ngx_http_image_filter_module.<\/p>\n<p>WordPress&#8217; dynamic image sizes serve two main purposes: aside from resizing the source images to a smaller size, it also lets you crop rectangular images to a thumbnail without completely skewing the aspect ratio and butchering the result.<\/p>\n<p><!--more--><\/p>\n<p>Let&#8217;s take an example WordPress gallery, created using the JetPack galleries plugin. We&#8217;re taking a mix of square (logos) and rectangular (photos) media, and framing them in circles via CSS. On a properly-configured web server, it should show up like this:<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/Correct-Gallery.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3171 colorbox-3165\" src=\"\/blog\/wp-content\/uploads\/Correct-Gallery.png\" alt=\"Correct Gallery\" width=\"300\" height=\"286\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Correct-Gallery.png 916w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Correct-Gallery-315x300.png 315w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>But without dynamic image resizing, the original aspect ratios can be completely skewed, and you&#8217;ll get something like this instead:<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/Skewed-Gallery.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3172 colorbox-3165\" src=\"\/blog\/wp-content\/uploads\/Skewed-Gallery.png\" alt=\"Skewed Gallery\" width=\"300\" height=\"296\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Skewed-Gallery.png 874w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Skewed-Gallery-304x300.png 304w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s take a look and see what is happening. When WordPress generates the blog post, the images are being created with query string arguments appended&#8230; &#8220;What&#8217;s that,&#8221; you say? &#8220;Query string arguments for static images?!&#8221; Indeed:<\/p>\n<pre>\/blog\/wp-content\/uploads\/Attachment-1.png?w=202&amp;h=202&amp;crop=1<\/pre>\n<p>As you can see, WordPress is requesting a cropped\u00a0version of the originally-uploaded file. On a normally-configured webserver, query arguments are ignored for attachments like this. But on WordPress.com&#8217;s servers, they&#8217;re interpreted by an image processor, and resized before being served. We can see proof of this by checking the headers for a request to a resized image on a WordPress.com blog:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/mqudsi\/dd5cf1c8aa99579beae0.js\"><\/script><\/p>\n<p>There&#8217;s a lot going on, but just focus on the <span class=\"code\">X-Orig-Src: 0_imageresize<\/span> bit to validate our assumption.<\/p>\n<p>With nginx&#8217;s\u00a0ngx_http_image_filter_module, you can make nginx serve up resized images too. First things first, you&#8217;ll need to make sure that nginx is compiled with this module (it&#8217;s not by default). If it&#8217;s not you&#8217;ll need to recompile nginx from source, and specify\u00a0&#8211;with-http_image_filter_module:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/mqudsi\/112dded260f675d80df8.js\"><\/script><\/p>\n<p>Then we need to configure nginx.conf to tell it what parameters to use for the width, height, and crop arguments. It&#8217;s a little tricky since WordPress can issue either ?w or ?h or both, with or without ?crop=1. Also, nginx can&#8217;t resize images inside an if block, so we&#8217;re going to need to use a specific location (and a rewrite within an if) to get around that limitation:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/mqudsi\/7510d2d0a2cd12b4b899.js\"><\/script><\/p>\n<p>As you can see, we default $width and $height to &#8211; (which nginx specifies as the required value for unused parameters, otherwise you&#8217;ll get a HTTP 415 Unsupported Media Type error), then overwrite one or both depending on whether ?w= or ?h= were specified. This detection is outside the location blocks, and then we have a location block for our images and a fake location block (marked as internal for security\/sanity reasons) for our cropped content that will serve the same content, only use the crop instead of resize argument to image_filter.<\/p>\n<p>Et voil\u00e0!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever notice that image galleries on WordPress.com load faster than those on your own? WordPress.com can serve dynamically-resized images on-the-fly to improve page load speeds and create pretty-looking image galleries. You can achieve this on your own server too, with &hellip; <a href=\"https:\/\/neosmart.net\/blog\/nginx-wordpress-images-with-ngx_http_image_filter_module\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[9,889,54],"class_list":["post-3165","post","type-post","status-publish","format-standard","hentry","category-software","tag-guides","tag-nginx","tag-wordpress"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-P3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/3165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=3165"}],"version-history":[{"count":11,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions"}],"predecessor-version":[{"id":3179,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions\/3179"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}