Begin知更鸟主题接入又拍云缩略图服务

 5年前     6.4K  

文章目录

Begin知更鸟主题接入又拍云缩略图服务

知更鸟主题默认是thumbnail缩略图,还有七牛缩略图,但是因为七牛https不免费,一为使用的是又拍的云服务,所以想顺便使用又拍的截图服务。

Begin知更鸟主题接入又拍云缩略图服务

你还没用过又拍?那点击注册一个。

方法一

1、主题设置选择默认缩略图

2、添加一下代码到主题下的 functions.php 中即可。

/*
==================================================
begin知更鸟主题接入又拍云截图服务 By 一为
原文地址:https://www.iowen.cn/begin-theme-access-upyun-picture-service
==================================================
*/
function upyunCDN(){
    function Rewrite_URI($html){
      	$host = 'https://www.iowen.cn';
      	$CDNurl = 'https://cdn.iowen.cn';
      	$pattern = '/'. str_replace('/', '\/', $host) .'(.*)thumbnail\.php\?src='. str_replace('/', '\/', $host) .'\/wp-([^"\']*?)\.(jpg|gif|png|jpeg)\&w=([0-9]*?)\&h=([0-9]*?)\&a=([0-9]*?)\&zc=(\\d+)/i';
      	$replacement = $CDNurl.'/wp-$2.$3!/both/$4x$5';
      	$html = preg_replace($pattern, $replacement,$html);
  return $html;
  }
    if(!is_admin()){
        ob_start("Rewrite_URI");
    }
}
add_action('init', 'upyunCDN');

 

这个方法很简单,注意替换自己的链接地址。

参数说明

/both/<w>x<h> 宽x高,如 300×200 固定宽度和高度,宽高不足时居中裁剪再缩放。特别地,配合 /force/true 使用时,宽高不足时只缩放,不裁剪

其他参数请访问:又拍云

方法二

因为方法一无法替换ajax小工具的地址,所以有了这个方法二,方法简单粗暴ヾ(≧▽≦*)o 直接替换七牛缩略图的代码。

推荐使用方法二,覆盖全,效率也高,图方便用方法一也行

上码

/*
==================================================
begin知更鸟主题接入又拍云截图服务 By 一为 
原文地址:https://www.iowen.cn/begin-theme-access-upyun-picture-service 
注意:替换代码666和668行处的链接地址为自己的地址
==================================================
*/
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
// 标准缩略图
function zm_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){ 
      echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function zm_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a></span>';
    }
  }
}

// 分类宽缩略图
function zm_long_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'long_thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'long_thumbnail', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_k_w'),'height'=>zm_get_option('img_k_h'))).'" alt="'.$post->post_title .'" /></a>';
    } else { 
      echo '<a class="random-img" href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/long.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function zm_long_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'long_thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'long_thumbnail', true);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/long.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/long.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_k_w'),'height'=>zm_get_option('img_k_h'))).'" alt="'.$post->post_title .'" /></a></span>';
    } else { 
      echo '<span class="load"><a class="random-img" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/long.png" data-original="'.get_template_directory_uri().'/img/random/long.jpg" alt="'.$post->post_title .'" /></a></span>';
    }
  }
}

// 图片缩略图
function img_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_i_w'),'height'=>zm_get_option('img_i_h'))).'" alt="'.$post->post_title .'" /></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function img_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_i_w'),'height'=>zm_get_option('img_i_h'))).'" alt="'.$post->post_title .'" /></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a></span>';
    }
  }
}

// 视频缩略图
function videos_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'small', true) ) {
    $image = get_post_meta($post->ID, 'small', true);
    echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
  } 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){
      echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_v_w'),'height'=>zm_get_option('img_v_h'))).'" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
    }
  }
}

function videos_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'small', true) ) {
    $image = get_post_meta($post->ID, 'small', true);
    echo '<a class="videos" href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
  } 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){
      echo '<a class="videos" href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_v_w'),'height'=>zm_get_option('img_v_h'))).'" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a class="videos" href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
    }
  }
}

function videoe_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'small', true) ) {
    $image = get_post_meta($post->ID, 'small', true);
    echo '<img src=';
    echo $image;
    echo ' alt="'.$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){
      echo '<img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_v_w'),'height'=>zm_get_option('img_v_h'))).'" ';
      echo ' alt="'.$post->post_title .'" />';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" ';
      echo ' alt="'.$post->post_title .'" />';
    }
  }
}

function videor_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'small', true) ) {
    $image = get_post_meta($post->ID, 'small', true);
    echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
  } 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){
      echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_v_w'),'height'=>zm_get_option('img_v_h'))).'" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
    }
  }
}

function videor_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'small', true) ) {
    $image = get_post_meta($post->ID, 'small', true);
    echo '<a class="videor" href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
  } 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){
      echo '<a class="videor" href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_v_w'),'height'=>zm_get_option('img_v_h'))).'" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a class="videor" href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" ';
      echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
    }
  }
}

// 商品缩略图
function tao_thumbnail() {
    global $post;
    $url = get_post_meta($post->ID, 'taourl', true);
    if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.esc_url( get_permalink() ).'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_t_w'),'height'=>zm_get_option('img_t_h'))).'" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function tao_thumbnail_h() {
    global $post;
    $url = get_post_meta($post->ID, 'taourl', true);
    if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<span class="load"><a href="'.esc_url( get_permalink() ).'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_t_w'),'height'=>zm_get_option('img_t_h'))).'" alt="'.$post->post_title .'" /></a></span>';
    }
  }
}

// 图像日志缩略图
function format_image_thumbnail() {
    global $post;
  $content = $post->post_content;
  preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
  echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div>';
  echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][1],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div>';
  echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][2],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div>';
  echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][3],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div>';
}

function format_image_thumbnail_h() {
    global $post;
  $content = $post->post_content;
  preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
  echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div></div>';
  echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][1],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div></div>';
  echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][2],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div></div>';
  echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][3],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div></div></div>';
}

function zm_ad_thumbnail() {
  global $post;
  $direct = get_post_meta($post->ID, 'direct', true);
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.$direct.'" target="_blank" rel="nofollow"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.$direct.'" target="_blank" rel="nofollow"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function zm_ad_thumbnail_h() {
  global $post;
  $direct = get_post_meta($post->ID, 'direct', true);
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a></span>';
      }
  }
}

// 图片布局缩略图
function zm_grid_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('grid_w'),'height'=>zm_get_option('grid_h'))).'" alt="'.$post->post_title .'" /></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function zm_grid_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('grid_w'),'height'=>zm_get_option('grid_h'))).'" alt="'.$post->post_title .'" /></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

// 宽缩略图分类
function zm_full_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'full_thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'full_thumbnail', true);
    echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span></figure>';
    echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
  } 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){
      echo '<header class="full-header"><figure class="full-thumbnail"><a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>900,'height'=>350,'str'=>'large')).'" alt="'.$post->post_title .'" /></a></figure><div class="clear"></div>';
      echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
    } else {
      the_title( sprintf( '<h2 class="entry-title-full"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
      echo '<span class="title-l"></span>';
    }
  }
}

function zm_full_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'full_thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'full_thumbnail', true);
    echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span></figure>';
    echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
  } 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){
      echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>900,'height'=>350,'str'=>'large')).'" alt="'.$post->post_title .'" /></a></span></figure><div class="clear"></div>';
      echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
    } else {
      the_title( sprintf( '<h2 class="entry-title-full"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
      echo '<span class="title-l"></span>';
    }
  }
}

// 网址缩略图
function zm_sites_thumbnail() {
  global $post;
  $sites_img_link = sites_nofollow( base64_encode(get_post_meta($post->ID, 'sites_img_link', true)));
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function zm_sites_thumbnail_h() {
  global $post;
  $sites_img_link = sites_nofollow( base64_encode(get_post_meta($post->ID, 'sites_img_link', true)));
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<div class="load"><a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a></div>';
    }
  }
}

// wd_img
function gr_wd_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'wd_img', true) ) {
    $image = get_post_meta($post->ID, 'wd_img', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } else {
    $content = $post->post_content;
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
    echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>700,'height'=>380,'str'=>'large')).'" alt="'.$post->post_title .'" /></a>';
  }
}

function gr_wd_thumbnail_h() {
  global $post;
  if ( get_post_meta($post->ID, 'wd_img', true) ) {
    $image = get_post_meta($post->ID, 'wd_img', true);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } else {
    $content = $post->post_content;
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.getCDNImage($strResult[1][0],array('width'=>700,'height'=>380,'str'=>'large')).'" alt="'.$post->post_title .'" /></a></span>';
  }
}

// slider_widge
function zm_widge_thumbnail() {
  global $post;
  if ( get_post_meta($post->ID, 'widge_img', true) ) {
    $image = get_post_meta($post->ID, 'widge_img', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_s_w'),'height'=>zm_get_option('img_s_h'))).'" alt="'.$post->post_title .'" /></a>';
    }
  }
}


// slider_scrolling
function zm_thumbnail_scrolling() {
  global $post;
  if ( get_post_meta($post->ID, 'thumbnail', true) ) {
    $image = get_post_meta($post->ID, 'thumbnail', true);
    echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src="'.getCDNImage($strResult[1][0],array('width'=>zm_get_option('img_w'),'height'=>zm_get_option('img_h'))).'" alt="'.$post->post_title .'" /></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

// waterfall
function zm_waterfall_img() {
  global $post;
  if ( get_post_meta($post->ID, 'fall_img', true) ) {
    $image = get_post_meta($post->ID, 'fall_img', true);
    echo '<a href="'.get_permalink().'"><img src=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a>';
  } 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){
      echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'" alt="'.$post->post_title .'" /></a>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a>';
    }
  }
}

function zm_waterfall_img_h() {
  global $post;
  if ( get_post_meta($post->ID, 'fall_img', true) ) {
    $image = get_post_meta($post->ID, 'fall_img', true);
    echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
    echo $image;
    echo ' alt="'.$post->post_title .'" /></a></span>';
  } 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){
      echo '<span class="load"><a href="'.get_permalink().'"><img src="'.$strResult[1][0].'" alt="'.$post->post_title .'" /></a></span>';
    } else { 
      if ( zm_get_option('rand_img_n') ) {
        $random = mt_rand(1, zm_get_option('rand_img_n'));
      } else { 
        $random = mt_rand(1, 5);
      }
      echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/img/random/'. $random .'.jpg" alt="'.$post->post_title .'" /></a></span>';
    }
  }
}

  
/**
 * 获取又拍裁剪的图片链接
 *
 *
 * @param   string  $url   原始图片链接
 * @param   string | array $size    图片尺寸
 * @return  string
 */
function getCDNImage($url, $size = 'thumbnail'){
    // 去掉url中多余的符号 "
  	if(strrpos($url, '"')){
    	$url = substr($url,0, strrpos($url, '"'));
  	}
  	$width = array_key_exists('width', $size) ? $size['width'] : 225;
  $height = array_key_exists('height', $size) ? $size['height'] : 150;
  $mark = array_key_exists('str', $size) ? $size['str'] : '';
  	$imgurl=Rewrite_CDN_URI($url . '!' . $mark . '/both/'. $width . 'x' . $height);
    return $imgurl;
}
  
/**
 * 转化为又拍链接
 *
 */
function Rewrite_CDN_URI($html){
    /* 前面是需要用到又拍的域名,后面是需要加速的静态文件类型,使用分隔符 | 隔开即可 */
    $pattern ='/https:\/\/(www\.|)iowen\.cn\/wp-([^"\']*?)\.(jpg|gif|png|jpeg)/i';
    /* 又拍CDN空间地址,请自行替换成实际空间地址 */
    $replacement = 'https://cdn.iowen.cn/wp-$2.$3';
  $html = preg_replace($pattern, $replacement,$html);
  return $html;
}

 

将上面的代码替换知更鸟主题inc\thumbnail-qiniu.php文件内的内容,然后在主题设置里面选择七牛缩略图(不要理会后面的‘需要安装七牛云存储插件’)。

注意:替换代码666和668行处的链接地址为自己的地址

版权声明:一为 发表于 5年前,共 30561 字。
转载请注明:Begin知更鸟主题接入又拍云缩略图服务 | 一为忆

您可能感兴趣的

14 条评论

  • avatar

    自然 ( VIP 1 )

    广东
    回复

    你有没有替换文章内图片为又拍云的代码

  • avatar

    自然 ( VIP 1 )

    广东
    回复

    我用的方法和你差不多,我直接修改了inc\thumbnail-qiniu.php文件

    • avatar

      一为

      湖南
      回复

      @自然 嗯 直接替换好点,只是替换的地方太多了

  • avatar

    今日新闻 ( VIP 1 )

    广西
    回复

    文章不错非常喜欢,支持一下

  • avatar

    isdola ( VIP 2 )

    浙江
    回复

    真的很感谢博主,我的情况几乎和你一模一样。之前一直用新浪相册,无奈马上要关了,于是想到了七牛,又无奈七牛HTTPS是收费的,只好用又拍,但主题本身没有又拍缩略图功能,所以当我使用又拍的图片地址测试时缩略图一直出不来(不过本地都是正常的,网站里出不来,不知道为什么),后来看到了你这篇文章,现在又回去测试了一下,发现缩略图正常了。再次表示感谢!

    另外,想请教博主一个问题。起初我使用又拍的图片测试缩略图时一直出不来嘛(显示的是一张纯灰的图),我一直理解不了,比如说又拍的图片和新浪相册的图片,同样都是外链的图片,当我不设置手动缩略图时,按照主题缩略图代码的话,此时应该获取正文第一张图片作为缩略图(假如正文里有图片的话),那我现在把又拍的图片放到正文里后,为什么缩略图还是出不来呢,而用新浪相册的图片放到正文里,缩略图就可以正常显示呢?这个是不是和又拍平台有关?是不是像又拍、七牛这种CDN平台和普通的图床平台不一样,他们的平台有专门一套机制去控制图片的显示、调用什么的。只有按照特定的格式把图片地址转换后才能正常显示为其他尺寸的图片(比如说原图地址是http://a.com/a.jpg,此时无法显示在缩略图里,但是通过他们特定的机制转换成比如http://a.com/a.jpg!both/100x100时才能正常给缩略图调用)

    还有,再想请教一个问题,使用方案二的代码时,需不需要额外在又拍里做什么设置啊,比如说参数跟随是不是要选择开启,图片处理那里要不要自定义什么缩略图版本,图片版本什么的啊?

    最后,期待博主能解答一二的同时也再次感谢博主的分享,对我很有用!

    • avatar

      一为

      湖南
      回复

      @isdola 1、是不是thumbnail.php里面的白名单没有填写你的又拍域名啊?
      Begin主题根目录的timthumb-config.php,在大约70行处添加外链接图片地址域名:

      // 添加外链图片域名
      if(! isset($ALLOWED_SITES)){
      $ALLOWED_SITES = array (
      'ww1.sinaimg.cn',
      'ww2.sinaimg.cn',
      );
      }
      格式类似 'www.zmingcx.com', 一行一个.

      2、又拍里面不需要设置其他东西,默认就行,知更鸟可以直接用方案2

      • avatar

        isdola ( VIP 2 )

        浙江
        回复

        @一为 我当时有添加白名单的,不过添加的是自定义二级域名,不是又拍提供的默认域名。但是一直不显示缩略图。用了你的缩略图代码后一切都正常了。对了,代码二里有一个地方默认是需要改一下的,就是那个post-loading.gif主题默认是没有的,要改成loading.png

        • avatar

          一为

          湖南
          回复

          @isdola 只要添加你访问图片用的域名就可以了,不行就F12看看是什么情况把,
          post-loading.gif是我自己加的,忘记说明了 :mrgreen: ,改下文章去。

  • avatar

    今日新闻 ( VIP 1 )

    广西
    回复

    文章不错非常喜欢

  • avatar

    红嘴鸭 ( VIP 1 )

    湖北
    回复

    感谢大佬分享

  • avatar

    唯心寒辞 ( VIP 1 )

    广东
    回复

    这个不错,不过我用的COS,等有机会试试

  • avatar

    boke112导航 ( VIP 1 )

    广西
    回复

    直接根据原有的函数为又拍云的最好,简单直接有效。

    • avatar

      一为

      湖南
      回复

      @boke112导航 嗯嗯,方法一是因为我看到原函数要改的地方太多了才整的,因为覆盖不全所以就还是改原函数。