Using Qiniu Cloud Storage to Host Images in Typecho without Plugins

Publish: 2016-04-19 | Modify: 2019-02-20

Typecho has a third-party plugin that uses the Qiniu SDK to automatically synchronize uploaded images to Qiniu. However, sometimes this plugin seems to have some issues, causing upload failures. I'm not sure if it's due to the Qiniu interface or the plugin itself. Here's a way to automatically replace Typecho article images with Qiniu URLs without using a plugin.

Skip the registration and real-name authentication process for Qiniu. First, create a bucket (space). I have already created a bucket named "typecho". Then, set up mirroring storage and fill in your blog address (http://faq.xiaoz.me/) as the mirror source. Finally, save the settings.

[![2016-04-19_205642](https://cdn.xiaoz.top/wp-content/uploads/2016/04/2016-04-19_205642.jpg)](https://cdn.xiaoz.top/wp-content/uploads/2016/04/2016-04-19_205642.jpg)

Find the test domain name assigned to your current Qiniu space or bind your own domain name. You will need it later. In the screenshot below, I have already bound a domain name "img.xiaoz.me" to the typecho space.

[![2016-04-19_210010](https://cdn.xiaoz.top/wp-content/uploads/2016/04/2016-04-19_210010.jpg)](https://cdn.xiaoz.top/wp-content/uploads/2016/04/2016-04-19_210010.jpg)

Find the post.php file in the theme directory of Typecho's article page. Find the following code:

<?php $this->content(); ?>

Replace it with:

<?php echo $str = str_replace("faq.xiaoz.me/usr/uploads","img.xiaoz.me/usr/uploads",$this->content); ?>

In the code above, "faq.xiaoz.me" is your blog's own domain name, and "img.xiaoz.me" is the domain name you bind to your Qiniu space or the test domain name assigned by Qiniu. This method works because Qiniu supports mirroring storage. After setting up the mirror source, when you visit the Qiniu URL, the corresponding file will be automatically fetched from the source address, without even using the SDK.


Comments