Skip to content

Commit

Permalink
当压缩图更大时,返回原图
Browse files Browse the repository at this point in the history
  • Loading branch information
coderwhytop committed Aug 1, 2024
1 parent 740f56b commit e28bcc1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
35 changes: 3 additions & 32 deletions packages/baize-compress-image/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,6 @@ const store = localforage.createInstance(DEFAUTL_FORAGE_CONFIG);

const worker = new Worker();

// export const compressImageWorker = async (file: File, quality = DEFAULT_QUALITY) => {
// return new Promise(async (resolve, reject) => {
// const id = nanoid(8);
// const taskData = {
// file,
// quality,
// id,
// };
// const taskId = `baize-compress-image-${id}`;
// await store.setItem(taskId, taskData);
// worker.onmessage = async (event) => {
// const message = JSON.parse(event.data);
// if (message.type === "compressImageSuccess") {
// const result = await store.getItem(taskId);
// await store.removeItem(taskId);
// resolve(result);
// } else {
// reject(event.data);
// }
// };
// const message = {
// type: "compressImage",
// taskId,
// };
// worker.postMessage(JSON.stringify(message));
// });
// };



/** compressImageWorker 函数定义 */
// 使用 <canvas> 元素来处理所有非 PNG 图片的压缩。
Expand Down Expand Up @@ -96,8 +67,9 @@ export async function compressImageWorker(file: File, initialQuality: number = 0
console.log(`Compressed size: ${result.size / unit} kb`);
// 如果压缩后的文件大于或等于原始文件,且质量大于 0.1,则继续递归压缩
if (result.size >= originalSizeInBytes && initialQuality > 0.1) {
console.log('Compressed image is not smaller, attempting further compression.');
compressImageWorker(file, initialQuality - 0.1).then(newBlob => resolve(newBlob));
resolve(file);
// console.log('Compressed image is not smaller, attempting further compression.');
// compressImageWorker(file, initialQuality - 0.1).then(newBlob => resolve(newBlob));
} else {
resolve(transfer(result, [result]));
}
Expand All @@ -116,4 +88,3 @@ export async function compressImageWorker(file: File, initialQuality: number = 0
reader.readAsDataURL(file);
});
}

2 changes: 1 addition & 1 deletion packages/baize-compress-image/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function App() {
<div>
Hello localforage-worker!
<div>
<input type="file" onChange={handleFileChange} />
<input type="file" multiple onChange={handleFileChange} />
</div>
<div style={{display: originalImageUrl ? 'block' : 'none'}}>
<h2>原图</h2>
Expand Down

0 comments on commit e28bcc1

Please sign in to comment.