PHP script to implement Markdown articles uploaded to Qiniu Image Bed

藏色散人
Release: 2023-04-08 12:34:02
forward
2587 people have browsed it

After using Markdown to write articles, we often need to publish them to different platforms. Here we will encounter a problem. The pictures of the article need to be uploaded manually, which is very inconvenient to manage. Therefore, it is strongly recommended to upload the pictures to the picture. In this way, an article can be easily synchronized to major platforms. Next, use PHP to implement this function, select Qiniu Cloud as the image bed

Create and enter the project

$ mkdir markdown-images-to-qiniu $ cd markdown-images-to-qiniu
Copy after login

Install Qiniu official extension

$ composer require qiniu/php-sdk
Copy after login

The implementation idea is very simple

● Read the makrdown file

● Regularly match all the pictures

● Upload the pictures in sequence

● Replace the address of the article picture with the image bed Address

● Save the replaced article

The following is the specific implementation. First, create the script index.php in the project directory,

uploadToken($bucket); $uploadMgr = new UploadManager(); $content = $orginalContent; foreach ($mdImageArr as $image) { $start = mb_strpos($image, '](') + 2; $localPath = mb_substr($image, $start, -1); $extension = pathinfo($localPath)['extension']; $uploadPath = uniqid(). ".". $extension; list($ret, $error) = $uploadMgr->putFile($token, $uploadPath, $localPath); if(! $error ){ // 4. 将文章图片的地址替换为图床地址 $content = str_replace($localPath, $url.$uploadPath, $content); echo "{$uploadPath} 上传成功。\n"; } else { echo "{$uploadPath} 上传失败。\n"; } } // 5. 保存替换后的文章 file_put_contents($file, $content);
Copy after login

Use

$ php index.php test.md
Copy after login

The above is the detailed content of PHP script to implement Markdown articles uploaded to Qiniu Image Bed. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!