From 4cfea29f684f1c3cd384b64b623df74bbd30fea9 Mon Sep 17 00:00:00 2001 From: Hakadao Date: Mon, 24 Jun 2024 01:36:54 +0800 Subject: [PATCH] feat: add image compression and resizing for wallpaper upload --- .../Settings/components/ChangeWallpaper.vue | 34 ++++++--- src/utils/main.ts | 70 +++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/src/components/Settings/components/ChangeWallpaper.vue b/src/components/Settings/components/ChangeWallpaper.vue index abb2b560..7e584639 100644 --- a/src/components/Settings/components/ChangeWallpaper.vue +++ b/src/components/Settings/components/ChangeWallpaper.vue @@ -5,6 +5,7 @@ import Slider from '~/components/Slider.vue' import Tooltip from '~/components/Tooltip.vue' import { WALLPAPERS } from '~/constants/imgs' import { settings } from '~/logic' +import { compressAndResizeImage } from '~/utils/main' import SettingsItem from './SettingsItem.vue' import SettingsItemGroup from './SettingsItemGroup.vue' @@ -28,6 +29,14 @@ function changeWallpaperType(type: 'buildIn' | 'byUrl') { else { settings.value.searchPageWallpaperMode = type } + + // Set the wallpaper to empty if it's a locally uploaded wallpaper to prevent the `QUOTA_BYTES quota exceeded` error + if (type === 'byUrl') { + if (settings.value.wallpaper.startsWith('data:image/')) + settings.value.wallpaper = '' + else if (settings.value.searchPageWallpaper.startsWith('data:image/')) + settings.value.searchPageWallpaper = '' + } } function changeWallpaper(url: string) { @@ -59,16 +68,19 @@ function handleUploadWallpaper(e: Event) { const file = (e.target as HTMLInputElement)?.files?.[0] if (!file) return - // img to base64 on browser - fileToBase64(file).then((base64) => { - // 使用base64字符串 - changeWallpaper(base64 as string) - settings.value.customizeWallpaper = { - name: file.name, - url: base64 as string, - } - }).catch(() => { - // 处理错误 + + compressAndResizeImage(file, 2560, 1440, 0.9, (compressedFile: File) => { + // img to base64 on browser + fileToBase64(compressedFile).then((base64) => { + // 使用base64字符串 + changeWallpaper(base64 as string) + settings.value.customizeWallpaper = { + name: file.name, + url: base64 as string, + } + }).catch(() => { + // 处理错误 + }) }) } @@ -149,6 +161,7 @@ function handleRemoveCustomWallpaper() { pos="absolute top-4px right-4px" z-1 text="14px" flex="~ gap-1" >