rest api for random images

This commit is contained in:
spirit 2019-11-27 17:17:54 +08:00
parent 79ea6a20e3
commit 5197e50c2e
5 changed files with 46 additions and 24 deletions

View File

@ -1565,29 +1565,15 @@ function html_tag_parser($content) {
if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content,$matches)){
$i=0;
if ($i<sizeof($matches)) {
$content=str_replace($matches[$i],preg_replace(
'/!\{([^\{\}]+)*\}\('.$url_regex.'\)\['.$url_regex.'\]/i',
'<a data-fancybox="gallery"
data-caption="$1"
class="fancybox"
href="$2"
alt="$1"
title="$1"><img src="$7" target="_blank" rel="nofollow" class="fancybox"></a>',
$matches[$i]),
$content);
$content=str_replace(
$matches[$i],
preg_replace('/!\{([^\{\}]+)*\}\('.$url_regex.'\)\['.$url_regex.'\]/i','<a href="$2"><img src="$7" alt="$1" title="$1"></a>',$matches[$i]),
$content
);
$i++;
}
}
$content=preg_replace(
'/!\{([^\{\}]+)*\}\('.$url_regex.'\)/i',
'<a data-fancybox="gallery"
data-caption="$1"
class="fancybox"
href="$2"
alt="$1"
title="$1"><img src="$2" target="_blank" rel="nofollow" class="fancybox"></a>',
$content
);
$content=preg_replace('/!\{([^\{\}]+)*\}\('.$url_regex.'\)/i','<a href="$2"><img src="$2" alt="$1" title="$1"></a>',$content);
//Github cards
$content=preg_replace(
@ -1646,7 +1632,7 @@ function change_avatar($avatar){
// default feature image
function DEFAULT_FEATURE_IMAGE() {
if ( empty( akina_option('default_feature_image' )) ) {
return get_template_directory_uri().'/feature/index.php?'.rand(1,1000);
return rest_url('sakura/v1/image/feature').'?'.rand(1,1000);
//return 'https://api.mashiro.top/feature/?'.rand(1,1000);
} else {
return akina_option('default_feature_image').'?'.rand(1,1000);

View File

@ -12,6 +12,14 @@ add_action('rest_api_init', function () {
'methods' => 'GET',
'callback' => 'cache_search_json',
));
register_rest_route('sakura/v1', '/image/cover', array(
'methods' => 'GET',
'callback' => 'cover_gallery',
));
register_rest_route('sakura/v1', '/image/feature', array(
'methods' => 'GET',
'callback' => 'feature_gallery',
));
});
/**
@ -262,3 +270,31 @@ EOS;
return $result;
}
/*
* 随机封面图 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/cover
*/
function cover_gallery() {
$img_array = glob(get_wp_root_path(). "/themes/Sakura/cover/gallery/*.{gif,jpg,png}",GLOB_BRACE);
$img = array_rand($img_array);
$imgurl = $img_array[$img];
$mimetype = end(getimagesize($imgurl));
$imgdata = file_get_contents($imgurl);
header("Content-type: ".$mimetype);
echo $imgdata;
}
/*
* 随机文章特色图 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/feature
*/
function feature_gallery() {
$img_array = glob(get_wp_root_path(). "/themes/Sakura/feature/gallery/*.{gif,jpg,png}",GLOB_BRACE);
$img = array_rand($img_array);
$imgurl = $img_array[$img];
$mimetype = end(getimagesize($imgurl));
$imgdata = file_get_contents($imgurl);
header("Content-type: ".$mimetype);
echo $imgdata;
}

View File

@ -82,7 +82,7 @@ if ( akina_option('toggle-menu') == 'no') { ?>
if (akina_option('cover_img')) {
$imgurl = akina_option('cover_img');
} else {
$imgurl = get_site_url()."/wp-content/themes/Sakura/cover/index.php";
$imgurl = rest_url('sakura/v1/image/cover');
}

View File

@ -75,7 +75,7 @@ mashiro_option.float_player_on = true;
if (akina_option('cover_img')) {
$imgurl = akina_option('cover_img');
} else {
$imgurl = get_site_url()."/wp-content/themes/Sakura/cover/index.php";
$imgurl = rest_url('sakura/v1/image/cover');
} ?>
mashiro_option.cover_api = "<?php echo $imgurl; ?>";

View File

@ -44,7 +44,7 @@ function get_avatar_profile_url(){
*/
function get_random_bg_url(){
if ( empty( akina_option('default_feature_image' )) ) {
return get_template_directory_uri().'/feature/index.php?'.rand(1,1000);
return rest_url('sakura/v1/image/feature').'?'.rand(1,1000);
} else {
return akina_option('default_feature_image').'?'.rand(1,1000);
}