-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor image-shrink types and unwrap promise chains
- Loading branch information
Showing
18 changed files
with
229 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
packages/image-shrink/src/utils/IccProfile/replaceIccProfile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 3 additions & 11 deletions
14
packages/image-shrink/src/utils/IccProfile/stripIccProfile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
import { replaceIccProfile } from './replaceIccProfile' | ||
import { imageLoader } from '../image/imageLoader' | ||
|
||
export const stripIccProfile = async ( | ||
inputFile: File | ||
): Promise<HTMLImageElement> => { | ||
export const stripIccProfile = async (blob: Blob): Promise<Blob> => { | ||
try { | ||
const file = await replaceIccProfile(inputFile, []) | ||
const image = await imageLoader(URL.createObjectURL(file as Blob)) | ||
|
||
URL.revokeObjectURL(image.src) | ||
|
||
return image | ||
return await replaceIccProfile(blob, []) | ||
} catch (e) { | ||
throw new Error(`Failed to strip ICC profile and not image ${e}`) | ||
throw new Error(`Failed to strip ICC profile: ${e}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
import { createCanvas } from './createCanvas' | ||
|
||
export const canvasResize = (img: CanvasImageSource, w: number, h: number) => { | ||
return new Promise<HTMLCanvasElement>((resolve, reject) => { | ||
try { | ||
const { ctx, canvas } = createCanvas() | ||
export const canvasResize = async ( | ||
img: CanvasImageSource, | ||
w: number, | ||
h: number | ||
) => { | ||
try { | ||
const { ctx, canvas } = createCanvas() | ||
|
||
canvas.width = w | ||
canvas.height = h | ||
canvas.width = w | ||
canvas.height = h | ||
|
||
ctx.imageSmoothingQuality = 'high' | ||
ctx.drawImage(img, 0, 0, w, h) | ||
ctx.imageSmoothingQuality = 'high' | ||
ctx.drawImage(img, 0, 0, w, h) | ||
|
||
// @ts-expect-error TODO: fix this | ||
img.src = '//:0' // for image | ||
// @ts-expect-error TODO: fix this | ||
img.width = img.height = 1 // for canvas | ||
|
||
resolve(canvas) | ||
} catch (e) { | ||
reject(`Failed to resize image. ${e}`) | ||
if (img instanceof HTMLImageElement) { | ||
img.src = '//:0' // free memory | ||
} | ||
if (img instanceof HTMLCanvasElement) { | ||
img.width = img.height = 1 // free memory | ||
} | ||
}) | ||
|
||
return canvas | ||
} catch (e) { | ||
throw new Error('Canvas resize error', { cause: e }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
export const canvasToBlob = ( | ||
canvas: HTMLCanvasElement, | ||
type: string, | ||
quality: number | undefined, | ||
callback: BlobCallback | ||
): void => { | ||
return canvas.toBlob(callback, type, quality) | ||
quality: number | undefined | ||
): Promise<Blob> => { | ||
return new Promise((resolve, reject) => { | ||
const callback: BlobCallback = (blob) => { | ||
if (!blob) { | ||
reject('Failed to convert canvas to blob') | ||
return | ||
} | ||
resolve(blob) | ||
} | ||
canvas.toBlob(callback, type, quality) | ||
canvas.width = canvas.height = 1 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
import { readJpegChunks } from '../image/JPEG/readJpegChunks' | ||
|
||
export const getExif = async (file: File) => { | ||
let isExif: DataView | null = null | ||
export const getExif = async (blob: Blob) => { | ||
let exif: DataView | null = null | ||
|
||
const { promiseReadJpegChunks, stack } = readJpegChunks() | ||
return promiseReadJpegChunks(file) | ||
return promiseReadJpegChunks(blob) | ||
.then(() => { | ||
stack.forEach(({ marker, view }) => { | ||
if (!isExif && marker === 0xe1) { | ||
if (!exif && marker === 0xe1) { | ||
if (view.byteLength >= 14) { | ||
if ( | ||
// check for "Exif\0" | ||
view.getUint32(0) === 0x45786966 && | ||
view.getUint16(4) === 0 | ||
) { | ||
isExif = view | ||
return isExif | ||
exif = view | ||
return | ||
} | ||
} | ||
} | ||
|
||
return isExif | ||
}) | ||
return exif | ||
}) | ||
.catch(() => isExif) | ||
.catch(() => exif) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.