惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
V
Visual Studio Blog
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
L
LangChain Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
P
Proofpoint News Feed
博客园_首页
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog

Hi, I Am I

[I Am I 年度简报] — 不知终日梦为鱼 初探 ESP32-CAM QQ 聊天记录 MHT 文件转 HTML [I Am I 年度简报] - 草木本无意,荣枯自有时。 写在当下 NKCTF 2024 1z_F0r3ns1c5 Writeup 春秋杯冬季赛 2023 Writeup [I Am I 年度简报] - 2023 某内网渗透内部赛 Writeup 强网拟态 2023 Writeup Github Actions 自动化部署 Hexo 浅析CobaltStrike流量解密 陇剑杯 2023 Writeup CTF线下赛AWDP总结 ISCC 2023 Writeup ISCC 2023 实战题 Writeup CISCN 2023 Writeup 福建闽盾杯网络空间安全大赛 2023 Writeup 天一永安杯宁波市网络安全大赛 2023 Writeup 贵阳大数据及网络安全精英对抗赛 2023 Writeup 红明谷杯 2023 Writeup Confetti 带来有仪式感的鼓励 记一次 JS 逆向密码加密 [I Am I 年度简报] – 2022 PHP 读取 Excel 文件内容并写入数据库 从0开始的 MoeCTF 开发之路 观安杯 2022 Writeup 利用微信服务号实现早安自动化 Cloudflare批量拉黑IP脚本 蓝帽杯 2022 Writeup
Hexo 中实现 Live Photos 支持
2024-11-17 · via Hi, I Am I

使用 Live Photos,您的 iPhone 会记录您拍照前后 1.5 秒发生的事情。 这段 3 秒的内容是由一张 JPG 图片以及一个 45 帧的 MOV 视频文件所组成。

之前一直想给博客添加实况图的支持,原本是打算手撸一个出来,但碍于时间原因一直没弄,最近发现原来苹果有一个官方支持库 LivePhotosKit JS,那么问题都迎刃而解了。

首先,由于不是所有文章都需要添加实况图,所以为了节省资源,我们可以在页面中动态加载 LivePhotosKit.js,当页面中包含 Live Photo 时才进行加载:

document.addEventListener('DOMContentLoaded', function () {
    const livePhotoElements = document.querySelectorAll('[data-live-photo]');
    if (livePhotoElements.length > 0) {
        const script = document.createElement('script');
        script.src = 'https://cdn.apple-livephotoskit.com/lpk/1/livephotoskit.js';
        document.head.appendChild(script);
    }
});

接下来,只需要在 Hexo 中实现 实况照片(Live Photo) 功能即可。通过查阅 Hexo Wiki,可以发现有两个功能对我们非常有帮助,分别是 标签插件(Tag)过滤器(Filter),我们可以任选其一(博主更推荐使用过滤器)。

标签(tag)

我们可以通过 Hexo 的自定义标签功能,定义一个 live 标签,将指定的图片和视频渲染为 Live Photo 格式。

在 Hexo 项目的 scripts 文件夹中创建一个文件(如 live_photo.js),并添加以下代码:

hexo.extend.tag.register('live', function (args) {
  const [imageSrc, videoSrc] = args;
  
  return `
<figure style="text-align: center; color:#0003">
  <div
    data-live-photo
    data-photo-src="${imageSrc}"
    data-video-src="${videoSrc}"
    style="width: 320px; height: 320px; margin: 0 auto; overflow: hidden;">
  </div>
  <figcaption stlye="margin-top: 4px;">${altText}</figcaption>
</figure>
  `;
}, { ends: false });

在你的 Markdown 文件中使用如下格式

{% live 文字描述 图片链接 视频链接 %}

当 Hexo 渲染页面时,上述自定义标签会被替换为

<figure style="text-align: center; color:#0003">
  <div
    data-live-photo
    data-photo-src="图片链接"
    data-video-src="视频链接"
    style="width: 320px; height: 320px; margin: 0 auto; display: block; overflow: hidden;">
  </div>
  <figcaption stlye="margin-top: 4px;">图片描述</figcaption>
</figure>

过滤器(filter)

另一种方法是通过 Hexo 的过滤器功能,将特定的 Markdown 语法自动转换为 Live Photo 的 HTML 结构。

scripts 文件夹中创建一个文件(如 live-photo-filter.js),并添加以下代码:

hexo.extend.filter.register('before_post_render', function (data) {
  data.content = data.content.replace(
    /!\[(.*?)\]\((.*?)\)\((.*?)\)/g,
    (match, altText, imageSrc, videoSrc) => `
<figure style="text-align: center; color:#0003">
  <div
    data-live-photo
    data-photo-src="${imageSrc}"
    data-video-src="${videoSrc}"
    style="width: 320px; height: 320px; margin: 0 auto; overflow: hidden;">
  </div>
  <figcaption stlye="margin-top: 4px;">${altText}</figcaption>
</figure>
`
  );
  return data;
});

在 Markdown 文件中使用以下格式

![示例图](https://5ime.cn/live-photos/1.jpg)(https://5ime.cn/live-photos/1.mov)

示例

示例图