-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage-optimizer.js
More file actions
336 lines (271 loc) · 11.1 KB
/
image-optimizer.js
File metadata and controls
336 lines (271 loc) · 11.1 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* ============================================
IMAGE OPTIMIZER MODULE
Resize and compress images for optimal performance
============================================ */
class ImageOptimizer {
constructor(debugLog = console.log) {
this.debugLog = debugLog;
// Configuration
this.config = {
thumbnailSize: 80, // Max dimension for playlist thumbnails
coverSize: 400, // Max dimension for full cover art
quality: 0.85, // JPEG quality (0-1)
format: 'image/jpeg', // Output format
cacheEnabled: true // Enable IndexedDB caching
};
// Cache
this.memoryCache = new Map();
this.dbName = 'ImageOptimizerCache';
this.dbVersion = 1;
this.db = null;
// Statistics
this.stats = {
processed: 0,
cacheHits: 0,
cacheMisses: 0,
bytesReduced: 0
};
this.initDatabase();
}
// ========== DATABASE INITIALIZATION ==========
async initDatabase() {
if (!this.config.cacheEnabled) return;
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onerror = () => {
this.debugLog('❌ Failed to open IndexedDB', 'error');
reject(request.error);
};
request.onsuccess = () => {
this.db = request.result;
this.debugLog('✅ Image optimizer database ready', 'success');
resolve();
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
// Create object store for optimized images
if (!db.objectStoreNames.contains('optimizedImages')) {
const store = db.createObjectStore('optimizedImages', { keyPath: 'key' });
store.createIndex('timestamp', 'timestamp', { unique: false });
}
};
});
}
// ========== IMAGE OPTIMIZATION ==========
/**
* Optimize an image for display
* @param {string} imageUrl - Source image URL or data URL
* @param {string} size - 'thumbnail' or 'cover'
* @returns {Promise<string>} Optimized image data URL
*/
async optimizeImage(imageUrl, size = 'thumbnail') {
const cacheKey = `${imageUrl}_${size}`;
// Check memory cache first
if (this.memoryCache.has(cacheKey)) {
this.stats.cacheHits++;
return this.memoryCache.get(cacheKey);
}
// Check IndexedDB cache
if (this.config.cacheEnabled && this.db) {
const cached = await this.getCachedImage(cacheKey);
if (cached) {
this.stats.cacheHits++;
this.memoryCache.set(cacheKey, cached);
return cached;
}
}
this.stats.cacheMisses++;
try {
// Load and optimize image
const optimized = await this.processImage(imageUrl, size);
// Cache the result
this.memoryCache.set(cacheKey, optimized);
if (this.config.cacheEnabled && this.db) {
await this.cacheImage(cacheKey, optimized);
}
this.stats.processed++;
return optimized;
} catch (error) {
this.debugLog(`❌ Failed to optimize image: ${error.message}`, 'error');
// Return original on error
return imageUrl;
}
}
/**
* Process image: resize and compress
*/
async processImage(imageUrl, size) {
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
try {
const maxSize = size === 'thumbnail' ? this.config.thumbnailSize : this.config.coverSize;
// Calculate new dimensions
let width = img.width;
let height = img.height;
if (width > maxSize || height > maxSize) {
if (width > height) {
height = Math.round((height * maxSize) / width);
width = maxSize;
} else {
width = Math.round((width * maxSize) / height);
height = maxSize;
}
}
// Create canvas and resize
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
// Enable image smoothing for better quality
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = 'high';
// Draw resized image
ctx.drawImage(img, 0, 0, width, height);
// Convert to data URL with compression
const optimized = canvas.toDataURL(this.config.format, this.config.quality);
// Calculate size reduction
const originalSize = imageUrl.length;
const optimizedSize = optimized.length;
this.stats.bytesReduced += (originalSize - optimizedSize);
resolve(optimized);
} catch (error) {
reject(error);
}
};
img.onerror = () => {
reject(new Error('Failed to load image'));
};
// Handle data URLs and regular URLs
img.src = imageUrl;
});
}
/**
* Batch optimize multiple images
* @param {Array} images - Array of {url, size} objects
* @returns {Promise<Array>} Array of optimized data URLs
*/
async batchOptimize(images) {
const promises = images.map(img =>
this.optimizeImage(img.url, img.size || 'thumbnail')
);
return Promise.all(promises);
}
// ========== CACHE MANAGEMENT ==========
async getCachedImage(key) {
if (!this.db) return null;
return new Promise((resolve) => {
const transaction = this.db.transaction(['optimizedImages'], 'readonly');
const store = transaction.objectStore('optimizedImages');
const request = store.get(key);
request.onsuccess = () => {
if (request.result) {
// Check if cache entry is still valid (30 days)
const age = Date.now() - request.result.timestamp;
if (age < 30 * 24 * 60 * 60 * 1000) {
resolve(request.result.data);
} else {
resolve(null);
}
} else {
resolve(null);
}
};
request.onerror = () => resolve(null);
});
}
async cacheImage(key, data) {
if (!this.db) return;
return new Promise((resolve) => {
const transaction = this.db.transaction(['optimizedImages'], 'readwrite');
const store = transaction.objectStore('optimizedImages');
const entry = {
key: key,
data: data,
timestamp: Date.now()
};
const request = store.put(entry);
request.onsuccess = () => resolve();
request.onerror = () => resolve(); // Fail silently
});
}
/**
* Clear old cache entries
*/
async clearOldCache(maxAge = 30 * 24 * 60 * 60 * 1000) {
if (!this.db) return;
return new Promise((resolve) => {
const transaction = this.db.transaction(['optimizedImages'], 'readwrite');
const store = transaction.objectStore('optimizedImages');
const index = store.index('timestamp');
const cutoff = Date.now() - maxAge;
const range = IDBKeyRange.upperBound(cutoff);
const request = index.openCursor(range);
let deleted = 0;
request.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
cursor.delete();
deleted++;
cursor.continue();
} else {
this.debugLog(`🧹 Cleared ${deleted} old cache entries`, 'info');
resolve(deleted);
}
};
request.onerror = () => resolve(0);
});
}
/**
* Clear all caches
*/
clearAllCaches() {
this.memoryCache.clear();
if (this.db) {
const transaction = this.db.transaction(['optimizedImages'], 'readwrite');
const store = transaction.objectStore('optimizedImages');
store.clear();
this.debugLog('🧹 All image caches cleared', 'info');
}
}
/**
* Get cache statistics
*/
getStats() {
const hitRate = this.stats.cacheHits + this.stats.cacheMisses > 0
? (this.stats.cacheHits / (this.stats.cacheHits + this.stats.cacheMisses) * 100).toFixed(1)
: 0;
const bytesReduced = (this.stats.bytesReduced / 1024).toFixed(2);
return {
...this.stats,
hitRate: `${hitRate}%`,
bytesReduced: `${bytesReduced} KB`,
memoryCacheSize: this.memoryCache.size
};
}
/**
* Preload and optimize images for a playlist
* @param {Array} tracks - Array of track objects with metadata
*/
async preloadPlaylistImages(tracks) {
const images = tracks
.filter(track => track.metadata?.image)
.map(track => ({
url: track.metadata.image,
size: 'thumbnail'
}));
this.debugLog(`🖼️ Preloading ${images.length} playlist images...`, 'info');
// Process in batches to avoid overwhelming the system
const batchSize = 10;
for (let i = 0; i < images.length; i += batchSize) {
const batch = images.slice(i, i + batchSize);
await this.batchOptimize(batch);
}
this.debugLog('✅ Playlist images preloaded', 'success');
}
}
// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = ImageOptimizer;
}