Website construction

WordPress integrates PHP thumbnails and enables Nginx caching

Jager · June 25 · 2015 · · · · 4759 times read

Zhang Ge's blog shared a tutorial on opening Nginx thumbnails for WordPress before, which is really good! But I always feel that the clarity is not flattering. Even if the cutting quality is adjusted to 90, the distortion is still serious, so I want to find another way.

When I remembered helping a webmaster with CC defense, I found that even if his website was purely static, the CPU was still soaring when attacked. Finally, the analysis of the request log shows that all the pressure comes from the PHP thumbnail function of the website. Although the PHP thumbnail can cache the real-time generated image into a file, PHP still needs to make some simple judgments when requested for the second time, such as whether the thumbnail is cached, whether the cache file is expired, etc. With massive IP requests, these simple PHP dynamic judgments have become a major procrastinator!

It is not difficult to understand why the php cache mode of WP Super Cache is slower than the Mod_Rewrite mode! So, Static cache In the end, we must completely abandon any simple calculation to be considered as incisive!

 WordPress integrates PHP thumbnails and enables Nginx caching

Well, it's a bit far. Although this webmaster classmate later abandoned the PHP thumbnail function, Zhang Ge has a deep memory. When the Nginx thumbnail failed, I thought of it the first time.

This is a big procrastinator when there are too many visitors, but what if I try to get rid of PHP dynamic judgment? Naturally, it can be brought into full play!

Let's briefly share Zhang Ge's approach.

1、 Accelerate thinking

I followed the path (the former webmaster friend used the Wdone theme of Advocate), and naturally found the way to use this PHP thumbnail in Advocate:

 <img src=" http://timthumb Directory/timthumb.php? Src=picture address&h=picture height&w=picture width&zc=1 "class=" thumb "/>

It can be seen that there must be dynamic judgment for this parameter transfer, so to make it completely static, I first need to modify the thumbnail form.

It's very simple. Continue the Nginx thumbnail idea shared before, change the above url to the mode with parameters at the end of the image address, and then rewrite it to the above form in a pseudo static manner. Finally, realize pure Nginx Static cache

2、 Deployment method

① PHP code

After downloading, unzip to get thumb  Folder, edit timthumb-config.php , modify according to the note (optional).

Then upload the entire folder to the root directory of the website url  The form must be able to see the thumbnail.

② Nginx rules

Step ① After you can see the thumbnail effect normally, we will deploy Nginx rules.

Insert the following rules into the original Nginx rules of the website:

 #Nginx Rewrite PHP Thumbnail URL Rules By Zhang Ge Blog location ~ .*\. (gif|jpg|jpeg|png|bmp)$ { set $width ''; set $height ''; set $width $arg_w; set $height $arg_h; #As long as the picture has a width (? W=) or height (? H=) parameter, the access will be rewritten to/thumb/? Src=* on the interface generated by this dynamic thumbnail if ( $width != '' ) { rewrite ^(.*)$ /thumb/? src=http://$host/$1 last; } if ( $height != '' ) { rewrite ^(.*)$ /thumb/? src=http://$host/$1 last; } expires  max; }

This only rewrites the URL form of thumbnails. If you need to enable caching, you need to use Nginx's fastcgi cache. For unfamiliar friends, please refer to the sharing before Zhang Ge's blog:

Nginx enables fastcgi_cache cache acceleration and supports html pseudo static pages

Deploy according to the previous article fastcgi  After caching rules, this thumbnail will be Nginx  Cached (HIT from yourdomain. com can be seen by viewing the network header information in F12 developer mode), and the loading speed will naturally come up!

3、 Theme modification

After the above operations, we only have this thumbnail function, but when it is applied to blog posts, we can't manually input the size of pictures one by one, can we? And there are a bunch of old articles that can't be manually revised, right?

The actual solution is simple, please refer to the following code:

 add_filter ('the_content', 'content_thumbnail'); function content_thumbnail($content) { global $post; $pattern1 ="/<img(.*?)src=('|\")(.*?). (bmp|gif|jpeg|jpg|png)('|\") alt=\"(.*?)\"(.*?)>/i"; $pattern2 ="/<img(.*?)src=('|\")(.*?)-300x(\d+?). (bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i"; $pattern3 ="/<img(.*?)src=('|\")(.*?)-(\d+?)x300.(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i"; $replacement = '<img$1src=$2$3.$5$6$7>'; $replacement2 = '<img$1src=$2$3.$4? w=480$5 alt="'.$post->post_title.'" tilte="'.$post->post_title.'"$7>'; $content = preg_replace($pattern2, $replacement, $content); $content = preg_replace($pattern3, $replacement, $content); $content = preg_replace($pattern1, $replacement2, $content); $content = preg_replace('/<img(.*?)width="(.*?)" height="(.*?)"(.*?)>/i', '<img$1$4>', $content); return $content; }

Modification principle:

① . Change the image with size in the old article to the full image path. I used to use 300 size image thumbnails, so here we need to remove all 300 height or width images and change them to full size image paths;

② Finally, all the image paths in the article should be marked with the size rules suitable for sharing in this article. For example, what is the above code? W=480, that is, the size of the picture thumbnail is uniformly changed to 480px.

Well, just share so much. If you need it, please refer to the above code. You can add it to the topic functions.php after modifying it according to the actual situation to perfectly realize the article thumbnail.

40 responses
  1. Software Sea 2015-6-26 · 1:51

    First of all, I support it. In addition, the feature image I uploaded is uploaded according to the size of automatic clipping. Will the system generate thumbnail images again?

    •  avatar
      Jager 2015-6-26 · 8:18

      The purpose of this article is to prevent an image upload from being cut into multiple sizes, which not only takes up space, but also needs different thumbnail sizes once the theme is changed, which is very disgusting.

      • Xibei Blog 2015-6-28 · 9:23

        This is true. I suffered a lot in the past..

  2. Xibei Blog 2015-6-28 · 9:22

    Will this thumbnail conflict with the featured image?

    •  avatar
      Jager 2015-6-29 · 8:16

      can't

  3. Andrew 2015-6-28 · 11:29

    Learned

  4. Carefree pleasure 2015-6-29 · 22:12

    Is there any conflict between this method and 7N image storage

    •  avatar
      Jager 2015-6-29 · 23:52

      It is the same path with parameters, and there should be conflicts.
      It is estimated that the big picture and the small picture are indistinct.

  5. af 2015-6-30 · 11:52

    Thanks for sharing. It has been downloaded~

  6. Huang Yong's Blog 2015-6-30 · 14:15

    I have been looking for excuses for my laziness, and always work with impetuous and arrogant attitude. It is ability to show off when things are done well, and it is bad luck to excuse when things are done badly.

  7. Hot Word Network 2015-6-30 · 17:02

    Sign in successfully! Sign in time: 5:01:01 p.m., sign in every day, and life is more exciting~

  8. Sieve treasure 2015-7-1 · 9:24

    It will be faster after caching.

  9. Nhzy Infomaster 2015-7-1 · 12:45

    Thanks for the low traffic, no need to bother

    •  avatar
      Jager 2015-7-1 · 19:32

      You can't learn without tossing

  10. Disadvantages of diatom mud 2015-7-1 · 15:13

    : wink: like passing by

  11. Must share 2015-7-1 · 15:57

    pretty good.

  12. Black and white mushrooms 2015-7-11 · 13:52

    Look at the topic of this blog. Have you shared it, Master Zhang

    •  avatar
      Jager 2015-7-11 · 14:40

      Payment subject

      • Black and white mushrooms 2015-7-11 · 14:41

        How much must be in Chinese?

        •  avatar
          Jager 2015-7-11 · 15:22

          Baidu search began theme.

  13. greatly 2015-7-29 · 18:27

    How to configure the Apache server

  14. lock 2015-8-17 · 17:37

    Thanks for thinking about this recently, but I can't write nginx rules. I see this blog.
    360CDN can be cached normally after the rules are modified. Baidu website acceleration can force caching with custom rules before the rules are modified, which is also good.

    • lock 2015-8-28 · 19:51

      I tested it with the blogger's, but it seems to have no effect... Why: sad:

    • Qa 2017-1-7 · 22:02

      I understand, because I am the picture of the outer chain.
      The following is available for external links.

       set $url	''; set $url	$arg_url; rewrite ^/thumb/w(\d*)_h(\d*)/$ /timthumb.php? src=$arg_url&amp; w=$1&amp; h=$2&amp; zc=1&amp; q=85&amp; ct=1 last;
  15. ha-ha. It was a flash of inspiration last night, and this problem was finally solved grin:

  16. Brother Jager, I found a problem:
    Modify the URL form of the thumbnail, and then rewrite it back to the timthumb form.
    Use zhang. ge/*/xxx.jpg? The access mode of w=xxx&h=xxx can be clipped normally.
    Then use ob_start to replace zhang. ge in the URL with res.zgboke.com
    The URL becomes res.zgboke. com/*/xxx.jpg? W=xxx&h=xxx, there is a problem that the picture will not be cut to the specified size, so that the whole picture will be scaled, and the thumbnail will have different heights. See the screenshot for details:
    https://sunxyu.cn/image/zg.png

    •  avatar
      Jager 2017-5-7 · 22:17

      I found it before, but I didn't have time to study and solve it

    •  avatar
      Jager 2017-5-9 · 22:13

      The reason has been found. I wrote the rule of jpg incorrectly by nginx, which is written as jgp. As a result, the access of jpg is directly requested to the source image file, without clipping.
      https://res.zgboke.com/wp-content/uploads/2016/01/cdn.jpg?w=200&h=151

      •  avatar
        Jager 2017-5-10 · 15:48

        Click the green icon in the lower right corner to refresh the comments on this page.....

  17. AFR 2017-6-15 · 22:44

    Ask Big Jager
    What are the rules of Nginx cache Timthumb? I'm still not skilled and can't figure out

  18. Old Ghost 2017-9-13 · 19:37

    Chang Meng's timthumb. php? Src=&h=x&w=x&zc=1 is a parameter with&, and the blogger's is a parameter with? Yes, but I just want to bring it? The parameters of are reported incorrectly. I think it's normal for bloggers. How does Jager solve this problem.

    • Old Ghost 2017-9-13 · 20:59

      Solved the problem of Nginx configuration order. Redirection takes effect when it is put in front

  19. YF Blog 2020-2-8 · 22:24

    Hello, the last code seems to be wrong. See if this is correct

     function content_thumbnail($content) { global $post; $pattern1 ="//i"; $pattern2="//i"; $pattern3 ="//i"; $replacement = ''; $replacement2 = 'post_title.' " tilte="'.$ post-&gt; post_title.' "$7&gt;'; $content = preg_replace($pattern2, $replacement, $content); $content = preg_replace($pattern3, $replacement, $content); $content = preg_replace($pattern1, $replacement2, $content); $content = preg_replace('//i', '', $content); return $content; }

    It seems to be right. I don't know if it is because of the wrong theme

    •  avatar
      Jager 2020-2-8 · 23:31

      Do you mean my article code is wrong or your article code is wrong? Your code is obviously bad. Did you submit comments and lose content? Submit code comments and highlight them with code... What do you think of direct posting

      • YF Blog 2020-2-9 · 19:41

        Um I remember posting code with highlighted And lost some content
        Just now, I tried again. I don't know whether the highlighted code of the comment has a problem or I can't use it Go straight to the picture

        First of all, I am Code Xiaobai Directly use the code in the article The content of the blog will be swallowed , return $content; Just put it on "}"

        •  avatar
          Jager 2020-2-9 · 20:20

          return $content; It's on}...
          Highlight the code under test

           add_filter ('the_content', 'content_thumbnail'); function content_thumbnail($content) { global $post; $pattern1 ="/<img(.*?)src=('|\")(.*?). (bmp|gif|jpeg|jpg|png)('|\") alt=\"(.*?)\"(.*?)>/i"; $ pattern2="/<img(.*?)src=('|\")(.*?)-300x(\d+?). (bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i"; $pattern3 ="/<img(.*?)src=('|\")(.*?)-(\d+?)x300.(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i"; $replacement = '<img$1src=$2$3.$5$6$7>'; $replacement2 = '<img$1src=$2$3.$4? w=480$5 alt="'.$post->post_title.'" tilte="'.$post->post_title.'"$7>'; $content = preg_replace($pattern2, $replacement, $content); $content = preg_replace($pattern3, $replacement, $content); $content = preg_replace($pattern1, $replacement2, $content); $content = preg_replace('/<img(.*?)width="(.*?)" height="(.*?)"(.*?)>/i', '<img$1$4>', $content); return $content; }
          • YF Blog 2020-2-9 · 20:23

            The code is still not highlighted The code above has one more} at the end

            •  avatar
              Jager 2020-2-9 · 20:53

              Fixed it. Thanks for your feedback.

          • YF Blog 2020-2-9 · 20:25

            The code is still not highlighted There is one more} at the end of the code in the article...