Many people use WordPress to build image-based websites, which usually require loading many uploaded attachment images on the page. Although wordpress's space can specify the attachment as an absolute URL address, it is far from enough to solve the limitations of the browser download thread mechanism. Below is my solution for wordpress.
Before talking about the method, let’s first understand what the browser’s download thread is. To put it simply, when you open a web page with a browser, if all the images, styles, js, etc. on the page are placed under the same domain name, then the browser must request a downloaded file when it requests the server. Request the next one (or you can request several downloads at the same time and then continue to request, different browsers vary). If the pictures, styles, js and other files under this page are all placed in a domain name and there are too many, the waiting queue will be long when opening the web page. (For example, your 2M broadband is not much faster than your 4M broadband)
The solution is to download more data at the same time. Limited to the restriction problem under the same domain name, we can use multiple domain names to solve it.
If your space allows multiple subdirectories to be bound, you can set multiple subdomain names to be bound to the WordPress attachment wp-content/uploads/.
Mine is binding my subdomains http://pic0.tsov.net/ and http://pic1.tsov.net/ to wp-content/uploads/.
At the same time, specify the subdomain name you set in the "Settings" and "Media" in the background (if your blog already has data, you can use phpmyadmin to execute the statement and convert the attachments at once),
For example, my settings are: (Full URL address of the file: http://pic.tsov.net )
Then add the following code to the functions.php file under your theme.
<?php
function setting_pic_tsov_net($content) {
$pattern="/<img******************/"; (So the complete code cannot be displayed here, please click to download the complete code)
preg_match_all($pattern,$content,$match);
$iNumberOfPics = count($match[0]);
for ( $i=0; $i < $iNumberOfPics ; $i++ ){
$tsovnet= str_replace('pic.tsov.net','pic'.rand(0,1).'.tsov.net',$match[1][$i]);
$content=str_replace($match[1][$i],$tsovnet,$content);
};
return $content;
}
add_filter('the_content','setting_pic_tsov_net',12);
?>
The function of the above code is to randomly replace the image paths in your log.
All are bound to wp-content/uploads/.
If you don’t have a lot of pictures on one page. Just don't have too many subdomain names (domain name resolution also takes time). Generally, 2-3 is enough. You can roughly calculate it this way: a subdomain can be downloaded 5 times at the same time, so how many do you need to download at the same time? Divide by 5 to get the number of subdomains you want to use.
The above is just my initial implementation method. Later I found that the path of the image using random numbers rand (0, 1) often changes, which may not be good for search engines. It is best to fix it, so I use it like this:
$tsovnet= str_replace('pic.tsov.net','pic'.substr(decbin(md5($match[1][$i])),-1).'.tsov.net',$match[1 ][$i]);
I won’t explain this sentence, it’s too cumbersome. But I finally achieved my ultimate goal.
Of course, the above involves many other optimization issues, so I won’t go into details.
In addition: For example, if you point the subdomain name to a different space, every time you publish a log with an attachment, the attachment will be synchronized to other spaces. (For this synchronization, you can write a program to download it with 404 and other weird methods)
Having said so much, everyone must be confused when watching it. Then let’s experience the effect after implementation.
Original text: http://tsov.net/category/album/
Thanks to simonsu for your contribution