久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      PHP腳本實(shí)現(xiàn)Markdown文章上傳到七牛圖床

      在使用 Markdown 編寫文章之后,經(jīng)常需要發(fā)布到不同的平臺(tái),這里會(huì)遇到一個(gè)問題,文章的圖片需要手動(dòng)的進(jìn)行上傳,管理起來非常不方便,因此,強(qiáng)烈建議將圖片統(tǒng)一上傳到圖床中,這樣的話一篇文章就可以輕松的同步到各大平臺(tái)上面了。下面,用 PHP 來實(shí)現(xiàn)該功能,選用 七牛云 作為圖床

      創(chuàng)建并進(jìn)入項(xiàng)目

      $ mkdir markdown-images-to-qiniu $ cd markdown-images-to-qiniu

      安裝七牛官方的擴(kuò)展

      $ composer require qiniu/php-sdk

      實(shí)現(xiàn)思路很簡(jiǎn)單

      ● 讀取 makrdown 文件

      ● 正則匹配出所有的圖片

      ● 依次上傳圖片

      ● 將文章圖片的地址替換為圖床地址

      ● 保存替換后的文章

      以下是具體的實(shí)現(xiàn),首先在項(xiàng)目目錄下創(chuàng)建腳本 index.php,

      <?php require 'vendor/autoload.php'; use QiniuAuth; use QiniuStorageUploadManager; // 1. 讀取 `makrdown` 文件 $file = $argv[1]; if(! file_exists($file) ){     return "找不到文件{$file}"; } $orginalContent = file_get_contents($file); // 2. 正則匹配出所有的圖片 preg_match_all(     '/![.*](.+)/',     $orginalContent,     $matches,     PREG_PATTERN_ORDER ); $mdImageArr = $matches[0]; if(! count($mdImageArr) ){     return "無需上傳圖片"; } // 3. 依次上傳圖片 $accessKey = '你的 AccessKey'; $secretKey = '你的 SecretKey'; $bucket = '你的七??臻g名'; // eg. mindgeek $url = "空間所綁定的域名";  // eg. http://qiniu.site.com $auth = new Auth($accessKey, $secretKey); $token = $auth->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);

      使用

      $ php index.php test.md

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)