压缩封面图

This commit is contained in:
tangly1024
2023-06-18 11:12:54 +08:00
parent 2cd67c1d82
commit 437f969115
17 changed files with 69 additions and 69 deletions

View File

@@ -1,13 +1,33 @@
import BLOG from '@/blog.config'
/**
* notion图片可以通过指定url-query参数来压缩裁剪图片 例如 ?width=200
* 压缩图片
* 1. Notion图床可以通过指定url-query参数来压缩裁剪图片 例如 ?xx=xx&width=400
* 2. UnPlash 图片可以通过api q=50 控制压缩质量 width=400 控制图片尺寸
* @param {*} image
*/
const compressImage = (image) => {
const compressImage = (image, width = 400) => {
if (image && image.indexOf(BLOG.NOTION_HOST) === 0) {
return image + '&width=200'
return `${image}&width=${width}`
}
// 压缩unsplash图片
if (image && image.indexOf('https://images.unsplash.com/') === 0) {
// 将URL解析为一个对象
const urlObj = new URL(image)
// 获取URL参数
const params = new URLSearchParams(urlObj.search)
// 将q参数的值替换
params.set('q', '50')
// 尺寸
params.set('width', width)
// 格式
params.set('fmt', 'webp')
// 生成新的URL
urlObj.search = params.toString()
return urlObj.toString()
}
return image
}
/**
@@ -18,7 +38,7 @@ const compressImage = (image) => {
* @param {*} value
* @returns
*/
const mapImgUrl = (img, block, type = 'block') => {
const mapImgUrl = (img, block, type = 'block', from) => {
if (!img) {
return null
}
@@ -30,16 +50,16 @@ const mapImgUrl = (img, block, type = 'block') => {
ret = img
}
// 书签的地址本身就是永久链接,无需处理
if (block?.type === 'bookmark') {
ret = img
}
// notion 图床转换为永久图床地址
// Notion 图床转换为永久地址
if (ret.indexOf('secure.notion-static.com') > 0 && (BLOG.IMG_URL_TYPE === 'Notion' || type !== 'block')) {
ret = BLOG.NOTION_HOST + '/image/' + encodeURIComponent(ret) + '?table=' + type + '&id=' + block.id
}
console.log('图床', ret)
// 文章封面
if (from === 'pageCoverThumbnail') {
ret = compressImage(ret)
}
return ret
}