【Colorful】更换背景图片,增加必应壁纸

    选择打赏方式

更换 Colorful 模板的背景图片,序号 1~10,共十张图片,jpg 格式。

壁纸图片-缩略图(95x58):/content/templates/colorful/images/bg/tmp

壁纸图片:/content/templates/colorful/images/bg

必应壁纸

/content/templates/colorful/header.php 约 130 行处新增

<li onclick="loadBing()"><a href="javascript:" title="设置网站背景图片为必应壁纸"><i class="fa fa-image"></i> 壁纸</a></li>

/content/templates/colorful/footer.php 标签 </body> 前新增

<script>
    function loadBing() {
        $(".blackground").fadeOut(100);
        $(".bg-image").attr("src", 'https://open.saintic.com/api/bingPic/');
        RootCookies.SetCookie("myhk_bg",0,30);
    }
</script>

PS:直接调用别人写好的必应壁纸的 API 即可,也可自建必应壁纸 API


2024.4 根据 时光本无罪 反馈,完善刷新页面无法使用必应壁纸。

/content/templates/colorful/footer.php 约 79 行处修改

<?php empty($_COOKIE['myhk_bg']) ? $bgimgsrc = 'https://open.saintic.com/api/bingPic/' : $bgimgsrc = TEMPLATE_URL . 'images/bg/' . $_COOKIE["myhk_bg"] . '.jpg' ;?>

将之前的随机背景图改成必应壁纸。

优化

将随机背景图改成必应壁纸后,empty($_COOKIE['myhk_bg']) 导致默认的背景图为必应壁纸,无法随机显示壁纸,因此优化刷新页面后的背景图,集成必应壁纸。

/content/templates/colorful/footer.php 约 79 行处修改(注释或删除)

<?php //empty($_COOKIE['myhk_bg']) ? $bgimgsrc = 'https://open.saintic.com/api/bingPic/' : $bgimgsrc = TEMPLATE_URL . 'images/bg/' . $_COOKIE["myhk_bg"] . '.jpg' ;?>

/content/templates/colorful/footer.php 约 80 行处修改

<img class="bg-image" src="<?php echo dice_colorful_bg_image(); ?>" /><div class="bg-image-pattern"></div>

/content/templates/colorful/module.php 最底部新增

<?php
/**
 * 获取背景图 For Dice
 * 支持必应壁纸、指定背景、随机背景
 */
function dice_colorful_bg_image()
{
    if (isset($_COOKIE['myhk_bg'])) { //cookie myhk_bg 背景图
        if (empty($_COOKIE['myhk_bg'])) { //myhk_bg 为空(之前设置的cookie 0)时调用必应壁纸
            if (isset($_COOKIE['myhk_bg_bing']) && filter_var($_COOKIE['myhk_bg_bing'], FILTER_VALIDATE_URL) !== false) { //cookie myhk_bg_bing 存在且是有效的 url
                return $_COOKIE['myhk_bg_bing'];
            }

            $json_string = file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'); //返回 json 格式数据,当天壁纸,请求一张,中国地区
            $data = json_decode($json_string, true);
            $time = strtotime("tomorrow");
            $expireTime = $time - time();
            setcookie('myhk_bg_bing', $data['images'][0]['url'], $expireTime); //设置必应壁纸仅当天有效
            return 'https://cn.bing.com' . $data['images'][0]['url'];
        }

        return  TEMPLATE_URL . 'images/bg/' . $_COOKIE['myhk_bg'] . '.jpg'; //设置的背景图
    }

    return TEMPLATE_URL . 'images/bg/' . rand(1, 10) . '.jpg?v=pro'; //随机背景图
}
?>
版权声明:若无特殊注明,本文为《红豆》原创,转载请保留文章出处。
本文链接:https://blog.52linglong.com/course/Colorful-bg-bing.html
正文到此结束

热门推荐

发表吐槽

你肿么看?

你还可以输入 250 / 250 个字

嘻嘻 大笑 可怜 吃惊 害羞 调皮 鄙视 示爱 大哭 开心 偷笑 嘘 奸笑 委屈 抱抱 愤怒 思考 日了狗 胜利 不高兴 阴险 乖 酷 滑稽

评论信息框
可使用QQ号实时获取昵称+头像

私密评论

吃奶的力气提交吐槽中...

已有1条吐槽

时光本无罪

2024-03-29 11:21 上海市浦东新区电信
谢谢博主。有个小缺点,自带的背景切换可以记录cookie后,刷新也不会切换回壁纸。使用bing背景 刷新就恢复原来背景了
 Windows 10 x64   Google Chrome 122.0.0.0
时光本无罪