使用github作为图床,并利用jsdelivr加速访问。再结合picgo,更方便的上传图片。最后配置github action,每周定时压缩图片大小。
1.创建repo和token
可以创建一个专门放图片的repo,还需要创建一个token供picgo上传图片。
![创建token](https://cdn.jsdelivr.net/gh/liguoqinjim/images/2021/12/27/201702_ac86.png)
2.picgo的设置
![picgo设置](https://cdn.jsdelivr.net/gh/liguoqinjim/images/2021/12/27/202140_eb6b.png)
3.picgo插件
rename file插件
这个插件可以重新定义我们上传图片的路径。如:
安装方法,在picgo插件界面搜索rename即可。安装完成后配置:
![rename插件配置](https://cdn.jsdelivr.net/gh/liguoqinjim/images/2021/12/27/202938_da8b.png)
4.jsdelivr加速
众所周知,访问github的网络情况不稳定,所以我们可以使用jsdelivr来加速。用法就是在上面picgo的设置里面的设定自定义域名为https://cdn.jsdelivr.net/gh/{username}/{reponame}
。
设置完成之后,picgo设置好图片之后,就会把链接替换成jsdelivr的。
5.使用github action压缩图片
可以使用github action功能,定时处理repo中的图片。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| name: Compress images
on:
schedule:
- cron: '00 23 * * 0'
jobs:
build:
name: calibreapp/image-actions
runs-on: ubuntu-latest
# Only run on main repo on and PRs that match the main repo.
if: |
github.repository == 'liguoqinjim/images' &&
(github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository)
steps:
- name: Checkout Branch
uses: actions/checkout@v2
- name: Compress Images
id: calibre
uses: calibreapp/image-actions@main
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
# For non-Pull Requests, run in compressOnly mode and we'll PR after.
compressOnly: ${{ github.event_name != 'pull_request' }}
- name: Create Pull Request
id: cpr
# If it's not a Pull Request then commit any changes as a new PR.
if: |
github.event_name != 'pull_request' &&
steps.calibre.outputs.markdown != ''
uses: peter-evans/create-pull-request@v3
with:
title: Auto Compress Images
branch-suffix: timestamp
commit-message: Compress Images
labels: automerge
body: ${{ steps.calibre.outputs.markdown }}
|
会自动生成合并的PR:
![pr](https://cdn.jsdelivr.net/gh/liguoqinjim/images/2021/12/27/204747_8561.png)