WordPress For SAE整合七牛CDN

后端 Tumi 827浏览 0评论

1. 根目录下config.yaml文件
添加内容

- rewrite: if (path ~ "upload/(.+\.(jpg|jpeg|gif|png))$") goto "uploads/index.php?id=$1"
- rewrite: if (path ~ "qiniu/\d+/image/(.+)$") goto "uploads/image.php?id=$1"

2. 根目录下新建文件夹 uploads , uploads下新建文件 index.php , image.php
index.php

$stor = new SaeStorage ();
$f = $_GET [''id''];
if ($stor->getUrl ( ''wordpress'', ''uploads/'' . $f )) {
    $type = pathinfo ( $f, PATHINFO_EXTENSION );
    switch ($type) {
      case ''jpg'' :
        header ( "Content-Type:image/jpg" );
        break;
      case ''jpeg'' :
            header ( "Content-Type:image/jpeg" );
            break;
      case ''gif'' :
        header ( "Content-Type:image/gif" );
        break;
      case ''png'' :
        header ( "Content-Type:image/png" );
        break;
      default :
        header ( "HTTP/1.1 404 Not Found" );
        header ( "Status: 404 Not Found" );
        exit ();
    }
    
    echo file_get_contents ( ''http://你的Storage地址/uploads/'' . $f );
} else {
    header ( "HTTP/1.1 404 Not Found" );
    header ( "Status: 404 Not Found" );
}

image.php

$stor = new SaeStorage ();
$f = base64_decode ( $_GET [''id''] );
$type = pathinfo ( $f, PATHINFO_EXTENSION );
switch ($type) {
    case ''jpg'' :
      header ( "Content-Type:image/jpg" );
      break;
    case ''jpeg'' :
      header ( "Content-Type:image/jpeg" );
      break;
    case ''gif'' :
      header ( "Content-Type:image/gif" );
      break;
    case ''png'' :
      header ( "Content-Type:image/png" );
      break;
    default :
      header ( "HTTP/1.1 404 Not Found" );
      header ( "Status: 404 Not Found" );
      exit ();
}
echo @file_get_contents ( $f );

3. 根目录下wp-config.php
更改内容line 71

define(''SAE_URL'', ''http://你的七牛CDN地址/upload'');

4.主题文件夹下functions.php
添加内容

//七牛
defined ( ''CDN_HOST'' ) or define ( ''CDN_HOST'', ''http://你的七牛CDN地址'' );
//SAE
defined ( ''LOCAL_HOST'' ) or define ( ''LOCAL_HOST'', home_url () );
//文章缩略图
if ( ! function_exists( ''deel_thumbnail'' ) ) :
function deel_thumbnail($size = null) {
      global $post;
      if (has_post_thumbnail ()) {
        $domsxe = simplexml_load_string ( get_the_post_thumbnail () );
        $thumbnailsrc = $domsxe->attributes ()->src;
        echo ''<img src="'' . $thumbnailsrc . $size . ''" alt="'' . trim ( strip_tags ( $post->post_title ) ) . ''" />'';
      } else {
        $content = $post->post_content;
        preg_match_all ( ''/<img.*?(?: |\\t|\\r|\\n)?src=[\''"]?(.+?)[\''"]?(?\"\"?: |\\t|\\r|\\n)+.*?)?>/sim'', $content, $strResult, PREG_PATTERN_ORDER );
        $n = count ( $strResult [1] );
        if ($n > 0) {
            $base = base64_encode ( $strResult [1] [0] );
            echo ''<img src="'' . CDN_HOST . ''/qiniu/'' . get_the_ID () . ''/image/'' . $base . $size . ''" alt="'' . trim ( strip_tags ( $post->post_title ) ) . ''" />'';
        } else {
            echo ''<img src="'' . get_bloginfo ( ''template_url'' ) . ''/img/thumbnail.png'' . $size . ''" alt="'' . trim ( strip_tags ( $post->post_title ) ) . ''" />'';
        }
      }
    }
endif;
//七牛镜像存储
if (! is_admin ()) {
    //页面加载
    add_action ( "wp_loaded", ''qiniucdn_start_ob_cache'' );
    //文章内容
    add_filter ( ''the_content'', ''qiniucdn_content'', 1 );
}
function qiniucdn_start_ob_cache() {
    ob_start ( ''qiniucdn_replace'' );
}
//替换域名
function qiniucdn_replace($html) {
    $cdn_exts = ''js|css|png|jpg|jpeg|gif|ico'';
    $cdn_dirs = str_replace ( ''-'', ''\-'', ''wp-content|wp-includes'' );
    $regex = ''/'' . str_replace ( ''/'', ''\/'', LOCAL_HOST ) . ''\/(('' . $cdn_dirs . '')\/[^\s\?\\\''\"\;\>\<]{1,}.('' . $cdn_exts . ''))([\"\\\''\s\?]{1})/'';
    $html = preg_replace ( $regex, CDN_HOST . ''/$1$4'', $html );
    return $html;
}
//替换文章图片
function qiniucdn_content($content){
    return preg_replace_callback(''|<img.*?src=[\''"](.*?)[\''"].*?>|i'',''qiniucdn_replace_remote_image'',do_shortcode($content));
}
function qiniucdn_replace_remote_image($matches) {
    $image_url = $matches [1];
    
    if (emptyempty ( $image_url ))
      return;
    
    if (strpos ( $image_url, LOCAL_HOST ) === false && strpos ( $image_url, CDN_HOST ) === false && strpos ( $image_url, ''.gif'' ) === false) {
      $base = base64_encode ( $image_url );
      return str_replace ( $image_url, CDN_HOST . ''/qiniu/'' . get_the_ID () . ''/image/'' . $base, $matches [0] );
    }
    
    return $matches [0];
}

deel_thumbnail使用方法:
deel_thumbnail(”?imageView/1/w/100/h/70”);

转载请注明:岁月回音 » WordPress For SAE整合七牛CDN

与本文相关的文章

您必须 登录 才能发表评论!