Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
fix(webgl-image): improve large-image zoom stability on iOS Safari (#259)
Copilot9c914132bd3f
diff --git a/app/components/photo/ProgressiveImage.vue b/app/components/photo/ProgressiveImage.vueindex 9b9037e7..b6b295cd 100644--- a/app/components/photo/ProgressiveImage.vue+++ b/app/components/photo/ProgressiveImage.vue@@ -52,10 +52,16 @@ const highResRendered = ref(false)const hasError = ref(false)const currentSrc = ref<string | null>()+const { loggedIn } = useUserSession()+const webglImageViewerDebug = useSettingRef('system:webglImageViewerDebug')const isDev = computed(() => import.meta.env.DEV)-const showDebugInfo = computed(- () => isDev.value && import.meta.env.VITE_SHOW_DEBUG_INFO === 'true',-)+const showDebugInfo = computed(() => {+ if (webglImageViewerDebug.value === true) {+ return !!loggedIn.value+ }++ return isDev.value && import.meta.env.VITE_SHOW_DEBUG_INFO === 'true'+})// 使用 WebGLImageViewer 的引用const webglViewerRef = ref()diff --git a/i18n/locales/en.json b/i18n/locales/en.jsonindex 8355d141..7000f700 100644--- a/i18n/locales/en.json+++ b/i18n/locales/en.json@@ -950,6 +950,11 @@}}}+ },+ "webglImageViewerDebug": {+ "label": "WebGL Image Viewer debug mode",+ "description": "Control whether the WebGL image viewer shows debug info in production",+ "help": "When enabled, logged-in users can view the WebGL debug panel in any environment; when disabled, debug info is shown only in development"}},"map": {diff --git a/i18n/locales/ja.json b/i18n/locales/ja.jsonindex 35c80ea1..633eb050 100644--- a/i18n/locales/ja.json+++ b/i18n/locales/ja.json@@ -885,6 +885,11 @@}}}+ },+ "webglImageViewerDebug": {+ "label": "WebGL Image Viewer デバッグモード",+ "description": "WebGL 画像ビューアのデバッグ情報を本番環境でも表示するかを制御します",+ "help": "有効にすると、ログイン中のユーザーは環境を問わず WebGL デバッグパネルを表示できます。無効時は開発環境のみ表示されます"}},"map": {diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.jsonindex e7356031..34b263fb 100644--- a/i18n/locales/zh-Hans.json+++ b/i18n/locales/zh-Hans.json@@ -954,6 +954,11 @@}}}+ },+ "webglImageViewerDebug": {+ "label": "WebGL Image Viewer 调试模式",+ "description": "控制 WebGL 图片查看器是否在生产环境也显示调试信息",+ "help": "开启后,登录用户在任意环境都可看到 WebGL 调试面板;关闭后仅开发环境生效"}},"map": {diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.jsonindex 6726c33e..b09812f4 100644--- a/i18n/locales/zh-Hant-HK.json+++ b/i18n/locales/zh-Hant-HK.json@@ -885,6 +885,11 @@}}}+ },+ "webglImageViewerDebug": {+ "label": "WebGL Image Viewer 偵錯模式",+ "description": "控制 WebGL 圖片檢視器是否喺生產環境都顯示偵錯資訊",+ "help": "啟用後,已登入用戶喺任何環境都可以睇到 WebGL 偵錯面板;關閉後只會喺開發環境生效"}},"map": {diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.jsonindex eb7bd521..2b695de3 100644--- a/i18n/locales/zh-Hant-TW.json+++ b/i18n/locales/zh-Hant-TW.json@@ -885,6 +885,11 @@}}}+ },+ "webglImageViewerDebug": {+ "label": "WebGL Image Viewer 偵錯模式",+ "description": "控制 WebGL 圖片檢視器是否在正式環境也顯示偵錯資訊",+ "help": "啟用後,已登入使用者在任何環境都可看到 WebGL 偵錯面板;關閉後僅在開發環境生效"}},"map": {diff --git a/package.json b/package.jsonindex 0cdcc5f5..ab36fba3 100644--- a/package.json+++ b/package.json@@ -18,8 +18,8 @@"build:deps": "pnpm --filter @chronoframe/webgl-image build","dev": "run-p dev:dep dev:wait-only","dev:dep": "pnpm --filter @chronoframe/webgl-image dev",- "dev:only": "nuxt dev",- "dev:wait-only": "wait-on packages/webgl-image/dist && nuxt dev",+ "dev:only": "nuxt dev --host",+ "dev:wait-only": "wait-on packages/webgl-image/dist && nuxt dev --host","lint": "oxlint","lint:fix": "oxlint --fix","fmt": "oxfmt",diff --git a/packages/webgl-image/src/constants.ts b/packages/webgl-image/src/constants.tsindex ed565e31..f64f9b22 100644--- a/packages/webgl-image/src/constants.ts+++ b/packages/webgl-image/src/constants.ts@@ -35,6 +35,14 @@ export const RENDER_CONFIG = {ANIMATION_THRESHOLD: 0.001,/** 最大纹理尺寸回退值 */MAX_TEXTURE_SIZE_FALLBACK: 4096,+ /** 单张纹理上传的像素预算 */+ MAX_TEXTURE_UPLOAD_PIXELS: 12000 * 12000,+ /** 启用瓦片渲染时允许的总像素预算 */+ MAX_TILE_TOTAL_PIXELS: 12000 * 12000 * 2,+ /** 纹理上传失败时最大重试次数 */+ TEXTURE_RETRY_LIMIT: 3,+ /** 纹理上传失败时缩放重试系数 */+ TEXTURE_RETRY_SCALE_FACTOR: 0.75,}export const EASING = {diff --git a/packages/webgl-image/src/core/WebGLImageViewerEngine.ts b/packages/webgl-image/src/core/WebGLImageViewerEngine.tsindex e92d296d..d178f0e9 100644--- a/packages/webgl-image/src/core/WebGLImageViewerEngine.ts+++ b/packages/webgl-image/src/core/WebGLImageViewerEngine.ts@@ -115,6 +115,7 @@ export class WebGLImageViewerEngine {private currentQuality: 'high' | 'medium' | 'low' | 'unknown' = 'unknown'private isLoadingTexture = falseprivate currentLoadingState: LoadingState = LoadingState.IDLE+ private lastRequestedSrc: string | null = nullconstructor(canvas: HTMLCanvasElement, config: Partial<EngineConfig> = {}) {this.canvas = canvas@@ -220,66 +221,155 @@ export class WebGLImageViewerEngine {const { type, payload } = event.dataswitch (type) {case 'loaded':- this.handleWorkerImageLoaded(payload)+ void this.handleWorkerImageLoaded(payload)breakcase 'load-error':- this.handleWorkerImageLoadError(payload)+ void this.handleWorkerImageLoadError(payload)break}}this.worker.onerror = (error) => {console.error('Worker error:', error)+ void this.handleWorkerImageLoadError(error)}}- private handleWorkerImageLoaded(payload: any) {+ private async handleWorkerImageLoaded(payload: any) {const { imageBitmap } = payloadtry {- this.image = imageBitmap+ const rendered = this.applyDecodedImage(imageBitmap)+ if (!rendered) {+ await this.renderWithMainThreadFallback(+ new Error('Failed to render worker ImageBitmap'),+ )+ }- const shouldUseTiles = this.shouldUseTiles(imageBitmap)- let usingTiles = false+ this.resolvePendingImageLoad()+ } catch (err) {+ console.error('Failed to render worker image:', err)+ this.rejectPendingImageLoad(err)+ }+ }- if (shouldUseTiles) {- this.emitLoadingStateChange(true, LoadingState.TILE_LOADING)- usingTiles = this.createTiles(imageBitmap)- }+ private async handleWorkerImageLoadError(error: any) {+ console.error('Image load error from worker:', error)- if (!usingTiles) {- this.emitLoadingStateChange(true, LoadingState.TEXTURE_LOADING)- this.createTexture(imageBitmap)- } else {- this.updatePositionBuffer()- }+ try {+ await this.renderWithMainThreadFallback(error)+ this.resolvePendingImageLoad()+ } catch (fallbackError) {+ this.rejectPendingImageLoad(fallbackError)+ }+ }- this.useTiles = usingTiles+ private applyDecodedImage(+ imageSource: HTMLCanvasElement | HTMLImageElement | ImageBitmap,+ ): boolean {+ const originalDimensions = this.getSourceDimensions(imageSource)+ if (!originalDimensions) {+ return false+ }- if (this.config.centerOnInit) {- this.centerImage()+ this.image = imageSource++ const shouldUseTiles = this.shouldUseTiles(imageSource)+ let usingTiles = false++ if (shouldUseTiles) {+ this.emitLoadingStateChange(true, LoadingState.TILE_LOADING)+ usingTiles = this.createTiles(imageSource)+ }++ if (!usingTiles) {+ this.emitLoadingStateChange(true, LoadingState.TEXTURE_LOADING)+ const texture = this.createTexture(imageSource)+ if (!texture) {+ return false}+ } else {+ this.updatePositionBuffer()+ }++ this.useTiles = usingTiles+ if (this.config.centerOnInit) {+ this.centerImage()+ }++ const finalWidth = this.image?.width ?? originalDimensions.width+ const finalHeight = this.image?.height ?? originalDimensions.height+ const widthRatio = finalWidth / originalDimensions.width+ const heightRatio = finalHeight / originalDimensions.height+ const minRatio = Math.min(widthRatio, heightRatio)++ if (minRatio < 0.6) {+ this.currentQuality = 'low'+ } else if (minRatio < 0.9) {+ this.currentQuality = 'medium'+ } else {this.currentQuality = 'high'+ }- this.emitLoadingStateChange(- false,- LoadingState.COMPLETE,- this.currentQuality,- )- this.render()- if (this.imageLoadingResolve) this.imageLoadingResolve()- } catch (err) {- console.error('Failed to create texture from ImageBitmap:', err)- this.emitLoadingStateChange(false, LoadingState.ERROR)- if (this.imageLoadingReject) this.imageLoadingReject(err as Error)+ this.emitLoadingStateChange(false, LoadingState.COMPLETE, this.currentQuality)+ this.render()++ return true+ }++ private async decodeImageOnMainThread(src: string): Promise<HTMLImageElement> {+ return await new Promise((resolve, reject) => {+ const image = new Image()+ image.crossOrigin = 'anonymous'+ image.decoding = 'async'+ image.onload = () => resolve(image)+ image.onerror = () => {+ reject(new Error('Failed to decode image on main thread'))+ }+ image.src = src+ })+ }++ private async renderWithMainThreadFallback(reason: unknown): Promise<void> {+ const src = this.lastRequestedSrc+ if (!src) {+ if (reason instanceof Error) {+ throw reason+ }+ throw new Error('No image source available for fallback decode')+ }++ console.warn('Falling back to main-thread decode/render.', reason)+ this.emitLoadingStateChange(true, LoadingState.IMAGE_LOADING)++ const image = await this.decodeImageOnMainThread(src)+ const rendered = this.applyDecodedImage(image)++ if (!rendered) {+ throw new Error('Main-thread decode succeeded but rendering still failed')}}- private handleWorkerImageLoadError(error: any) {- console.error('Image load error from worker:', error)+ private resolvePendingImageLoad(): void {+ const resolve = this.imageLoadingResolve+ this.imageLoadingResolve = null+ this.imageLoadingReject = null+ resolve?.()+ }++ private rejectPendingImageLoad(error: unknown): void {+ const reject = this.imageLoadingReject+ this.imageLoadingResolve = null+ this.imageLoadingReject = null++ const normalizedError =+ error instanceof Error+ ? error+ : new Error(typeof error === 'string' ? error : 'Unknown image load error')+this.emitLoadingStateChange(false, LoadingState.ERROR)- if (this.imageLoadingReject) this.imageLoadingReject(error as Error)+ reject?.(normalizedError)}private createBuffers(): void {@@ -377,6 +467,7 @@ export class WebGLImageViewerEngine {public async loadImage(src: string): Promise<void> {console.log('Post load image:', src)+ this.lastRequestedSrc = srcthis.emitLoadingStateChange(true, LoadingState.IMAGE_LOADING)return new Promise((resolve, reject) => {@@ -392,11 +483,16 @@ export class WebGLImageViewerEngine {type: 'load',payload: { src: absolute.toString() },})- } catch {- this.worker.postMessage({ type: 'load', payload: { src } })+ } catch (error) {+ console.warn('Worker postMessage failed, using main-thread fallback.', error)+ void this.renderWithMainThreadFallback(error)+ .then(() => this.resolvePendingImageLoad())+ .catch((fallbackError) => this.rejectPendingImageLoad(fallbackError))}} else {- reject(new Error('No worker available'))+ void this.renderWithMainThreadFallback(new Error('No worker available'))+ .then(() => this.resolvePendingImageLoad())+ .catch((error) => this.rejectPendingImageLoad(error))}})}@@ -415,67 +511,148 @@ export class WebGLImageViewerEngine {gl.deleteTexture(this.texture)}+ const dimensions = this.getSourceDimensions(imageSource)+ if (!dimensions) {+ return null+ }+// 获取最大纹理尺寸并判断是否需要缩放const maxTextureSize = getMaxTextureSize(gl)- const srcWidth =- (imageSource as HTMLCanvasElement).width ??- (imageSource as HTMLImageElement).width ??- (imageSource as ImageBitmap).width- const srcHeight =- (imageSource as HTMLCanvasElement).height ??- (imageSource as HTMLImageElement).height ??- (imageSource as ImageBitmap).height+ const maxTexturePixels = this.getTextureUploadPixelBudget(maxTextureSize)+ const sourcePixels = dimensions.width * dimensions.height++ let targetWidth = dimensions.width+ let targetHeight = dimensions.height++ if (+ sourcePixels > maxTexturePixels ||+ dimensions.width > maxTextureSize ||+ dimensions.height > maxTextureSize+ ) {+ const pixelScale = Math.min(1, Math.sqrt(maxTexturePixels / sourcePixels))+ const sizeScale = Math.min(+ pixelScale,+ maxTextureSize / dimensions.width,+ maxTextureSize / dimensions.height,+ )+ targetWidth = Math.max(1, Math.floor(dimensions.width * sizeScale))+ targetHeight = Math.max(1, Math.floor(dimensions.height * sizeScale))+ }let finalSource: HTMLCanvasElement | HTMLImageElement | ImageBitmap =imageSource-- // 如果超出最大纹理尺寸,使用离屏 canvas 等比缩放if (- typeof srcWidth === 'number' &&- typeof srcHeight === 'number' &&- (srcWidth > maxTextureSize || srcHeight > maxTextureSize)+ targetWidth !== dimensions.width ||+ targetHeight !== dimensions.height) {- const scale = Math.min(- maxTextureSize / srcWidth,- maxTextureSize / srcHeight,+ const resizedSource = this.resizeImageSource(+ imageSource,+ targetWidth,+ targetHeight,)+ if (resizedSource) {+ finalSource = resizedSource+ }+ }++ let uploadSuccess = false+ let attempt = 0- const targetWidth = Math.max(1, Math.floor(srcWidth * scale))- const targetHeight = Math.max(1, Math.floor(srcHeight * scale))+ while (attempt < RENDER_CONFIG.TEXTURE_RETRY_LIMIT) {+ this.texture = gl.createTexture()+ if (!this.texture) {+ break+ }- const offscreen = document.createElement('canvas')- offscreen.width = targetWidth- offscreen.height = targetHeight- const ctx = offscreen.getContext('2d')- if (ctx) {- ctx.imageSmoothingEnabled = true+ gl.bindTexture(gl.TEXTURE_2D, this.texture)++ // 设置纹理参数+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)++ let uploadError: unknown = null+ try {+ gl.texImage2D(+ gl.TEXTURE_2D,+ 0,+ gl.RGBA,+ gl.RGBA,+ gl.UNSIGNED_BYTE,+ finalSource,+ )+ } catch (error) {+ uploadError = error+ }- ctx.imageSmoothingQuality = 'high'- ctx.drawImage(imageSource as any, 0, 0, targetWidth, targetHeight)- finalSource = offscreen+ const glError = gl.getError()+ if (!uploadError && glError === gl.NO_ERROR) {+ uploadSuccess = true+ break+ }++ if (uploadError) {+ console.warn('Texture upload error, retrying with smaller source.', {+ attempt: attempt + 1,+ error: uploadError,+ })} else {- finalSource = imageSource+ console.warn('Texture upload failed, retrying with smaller source.', {+ attempt: attempt + 1,+ glError,+ })}- }- this.texture = gl.createTexture()- gl.bindTexture(gl.TEXTURE_2D, this.texture)+ if (this.texture) {+ gl.deleteTexture(this.texture)+ this.texture = null+ }- // 设置纹理参数- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)+ const currentDimensions = this.getSourceDimensions(finalSource)+ if (!currentDimensions) {+ break+ }- // 上传图像数据(使用可能缩放后的来源)- gl.texImage2D(- gl.TEXTURE_2D,- 0,- gl.RGBA,- gl.RGBA,- gl.UNSIGNED_BYTE,- finalSource,- )+ const retryWidth = Math.max(+ 1,+ Math.floor(+ currentDimensions.width * RENDER_CONFIG.TEXTURE_RETRY_SCALE_FACTOR,+ ),+ )+ const retryHeight = Math.max(+ 1,+ Math.floor(+ currentDimensions.height * RENDER_CONFIG.TEXTURE_RETRY_SCALE_FACTOR,+ ),+ )++ if (+ retryWidth >= currentDimensions.width &&+ retryHeight >= currentDimensions.height+ ) {+ break+ }++ const resizedSource = this.resizeImageSource(+ finalSource,+ retryWidth,+ retryHeight,+ )+ if (!resizedSource) {+ break+ }++ finalSource = resizedSource+ attempt += 1+ }++ gl.bindTexture(gl.TEXTURE_2D, null)++ if (!uploadSuccess || !this.texture) {+ this.texture = null+ return null+ }// 更新内部 image 引用为最终用于渲染的尺寸this.image = finalSource as any@@ -484,6 +661,43 @@ export class WebGLImageViewerEngine {return this.texture}+ private getSourceDimensions(source: {+ width: number+ height: number+ }): { width: number; height: number } | null {+ const { width, height } = source+ if (!isFinite(width) || !isFinite(height) || width <= 0 || height <= 0) {+ return null+ }+ return { width, height }+ }++ private getTextureUploadPixelBudget(maxTextureSize: number): number {+ const maxTexturePixels = Math.max(1, maxTextureSize * maxTextureSize)+ return Math.min(RENDER_CONFIG.MAX_TEXTURE_UPLOAD_PIXELS, maxTexturePixels)+ }++ private resizeImageSource(+ source: CanvasImageSource,+ targetWidth: number,+ targetHeight: number,+ ): HTMLCanvasElement | null {+ const offscreen = document.createElement('canvas')+ offscreen.width = targetWidth+ offscreen.height = targetHeight+ const ctx = offscreen.getContext('2d')+ if (!ctx) {+ return null+ }++ ctx.imageSmoothingEnabled = true+ ctx.imageSmoothingQuality = 'high'+ ctx.clearRect(0, 0, targetWidth, targetHeight)+ ctx.drawImage(source, 0, 0, targetWidth, targetHeight)++ return offscreen+ }+private cleanupTiles(): void {if (!this.tiles.length) return@@ -502,13 +716,18 @@ export class WebGLImageViewerEngine {): boolean {if (!source) return false+ if (!this.config.tileEnabled) return false++ const sourcePixels = source.width * source.height+ if (sourcePixels > RENDER_CONFIG.MAX_TILE_TOTAL_PIXELS) {+ return false+ }+const maxTextureSize = getMaxTextureSize(this.gl)if (source.width > maxTextureSize || source.height > maxTextureSize) {return true}- if (!this.config.tileEnabled) return false-return (source.width > this.config.tileSize ||source.height > this.config.tileSize@@ -546,6 +765,10 @@ export class WebGLImageViewerEngine {return false}+ if (width * height > RENDER_CONFIG.MAX_TILE_TOTAL_PIXELS) {+ return false+ }+const columns = Math.ceil(width / baseTileSize)const rows = Math.ceil(height / baseTileSize)const offscreen = document.createElement('canvas')@@ -1281,11 +1504,17 @@ export class WebGLImageViewerEngine {if (usingTiles) {const recreated = this.createTiles(this.image)if (!recreated) {- this.createTexture(this.image)+ const texture = this.createTexture(this.image)+ if (!texture) {+ throw new Error('Failed to recreate texture after context restore')+ }usingTiles = false}} else {- this.createTexture(this.image)+ const texture = this.createTexture(this.image)+ if (!texture) {+ throw new Error('Failed to recreate texture after context restore')+ }}this.useTiles = usingTiles && this.tiles.length > 0diff --git a/server/services/settings/contants.ts b/server/services/settings/contants.tsindex 34b25599..3f327eaf 100644--- a/server/services/settings/contants.ts+++ b/server/services/settings/contants.ts@@ -91,6 +91,15 @@ export const DEFAULT_SETTINGS = [description: 'settings.system.upload.duplicateCheck.mode.description',isPublic: true,},+ {+ namespace: 'system',+ key: 'webglImageViewerDebug',+ type: 'boolean',+ defaultValue: false,+ label: 'settings.system.webglImageViewerDebug.label',+ description: 'settings.system.webglImageViewerDebug.description',+ isPublic: true,+ },// NAMESPACE: privacy{namespace: 'privacy',diff --git a/server/services/settings/ui-config.ts b/server/services/settings/ui-config.tsindex a427316d..d7d6b94f 100644--- a/server/services/settings/ui-config.ts+++ b/server/services/settings/ui-config.ts@@ -138,6 +138,10 @@ export const SYSTEM_SETTINGS_UI: Record<string, FieldUIConfig> = {],help: 'settings.system.upload.duplicateCheck.mode.help',},+ webglImageViewerDebug: {+ type: 'toggle',+ help: 'settings.system.webglImageViewerDebug.help',+ },}export const STORAGE_SETTINGS_UI: Record<string, FieldUIConfig> = {