-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from traPtitech/feat/client-image-resize
クライアントサイドでのサムネイル生成
- Loading branch information
Showing
11 changed files
with
244 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Dimensions } from './size' | ||
|
||
export const loadImage = ( | ||
url: string, | ||
$img: HTMLImageElement | ||
): Promise<string> => { | ||
return new Promise(resolve => { | ||
$img.addEventListener( | ||
'load', | ||
() => { | ||
resolve() | ||
}, | ||
{ once: true } | ||
) | ||
$img.src = url | ||
}) | ||
} | ||
|
||
export const resetCanvas = ( | ||
$canvas: HTMLCanvasElement, | ||
{ width, height }: Dimensions, | ||
$img?: HTMLImageElement | ||
) => { | ||
$canvas.getContext('2d')?.clearRect(0, 0, $canvas.width, $canvas.height) | ||
$canvas.width = width | ||
$canvas.height = height | ||
if ($img) { | ||
$canvas.getContext('2d')?.drawImage($img, 0, 0) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const convertToDataUrl = (file: File): Promise<string | null> => { | ||
const reader = new FileReader() | ||
reader.readAsDataURL(file) | ||
return new Promise(resolve => { | ||
reader.addEventListener( | ||
'load', | ||
event => { | ||
// `readAsDataURL`を用いるため、結果の型はstring | ||
// see: https://developer.mozilla.org/ja/docs/Web/API/FileReader/result | ||
const thumbnailDataUrl = event.target?.result as string | null | ||
resolve(thumbnailDataUrl) | ||
}, | ||
{ once: true } | ||
) | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { start, finish, initVars } from './vars' | ||
import { loadImage, resetCanvas } from './canvas' | ||
import { needResize, getThumbnailDimensions } from './size' | ||
|
||
export const canResize = (mime: string) => | ||
['image/png', 'image/jpeg'].includes(mime) | ||
|
||
export const resize = async (inputFile: File): Promise<File | null> => { | ||
start() | ||
const { pica, $input, $output, $img } = await initVars() | ||
|
||
const inputUrl = URL.createObjectURL(inputFile) | ||
|
||
try { | ||
await loadImage(inputUrl, $img) | ||
const inputSize = { | ||
width: $img.width, | ||
height: $img.height | ||
} | ||
if (!needResize(inputSize)) { | ||
return finish(null, inputUrl) | ||
} | ||
|
||
const outputSize = getThumbnailDimensions(inputSize) | ||
|
||
resetCanvas($input, inputSize, $img) | ||
resetCanvas($output, outputSize) | ||
|
||
await pica.resize($input, $output, { | ||
quality: 2 | ||
}) | ||
const output = await pica.toBlob($output, 'image/png') | ||
const outputFile = new File([output], inputFile.name, { | ||
type: inputFile.type, | ||
lastModified: inputFile.lastModified | ||
}) | ||
|
||
return finish(outputFile, inputUrl) | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Failed to generate thumbnail image: ${e}`, e) | ||
return finish(null, inputUrl) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const MAX_HEIGHT = 1600 | ||
const MAX_WIDTH = 2560 | ||
|
||
export interface Dimensions { | ||
width: number | ||
height: number | ||
} | ||
|
||
export const needResize = ({ width, height }: Dimensions) => | ||
MAX_WIDTH < width || MAX_HEIGHT < height | ||
|
||
export const getThumbnailDimensions = ({ | ||
width, | ||
height | ||
}: Dimensions): Dimensions => { | ||
const widthRatio = width / MAX_WIDTH | ||
const heightRatio = height / MAX_HEIGHT | ||
const ratio = Math.max(widthRatio, heightRatio) | ||
return { width: width / ratio, height: height / ratio } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type Pica from 'pica' | ||
|
||
interface ResizeVars { | ||
pica: Pica | ||
$input: HTMLCanvasElement | ||
$output: HTMLCanvasElement | ||
$img: HTMLImageElement | ||
} | ||
|
||
// 使いまわす | ||
let resizeVars: ResizeVars | null = null | ||
let resizing = false | ||
const persistTime = 3000 // ms | ||
|
||
const loadPica = async () => { | ||
const Pica = (await import('pica')).default | ||
return new Pica() | ||
} | ||
|
||
// 存在しなかったらつくる | ||
export const initVars = async (): Promise<ResizeVars> => { | ||
if (resizeVars !== null) return resizeVars | ||
|
||
resizeVars = { | ||
pica: await loadPica(), | ||
$input: document.createElement('canvas'), | ||
$output: document.createElement('canvas'), | ||
$img: new Image() | ||
} | ||
|
||
setTimeout(deinitVars, persistTime) | ||
|
||
return resizeVars | ||
} | ||
// 使っていないときに消す | ||
export const deinitVars = () => { | ||
if (resizing) { | ||
setTimeout(deinitVars, persistTime) | ||
return | ||
} | ||
resizeVars = null | ||
} | ||
|
||
// リサイズ中に消さないようにする | ||
export const start = () => { | ||
resizing = true | ||
} | ||
// リサイズ後にリサイズ中に消さないようにするフラグを消す | ||
export const finish = <T>(output: T, inputUrl: string): T => { | ||
URL.revokeObjectURL(inputUrl) | ||
resizing = false | ||
return output | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'pica' { | ||
export default Pica | ||
} |
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