import * as jpegJS from 'jpeg-js' ; let jpegTurbo ; try { jpegTurbo = require ( '@julusian/jpeg-turbo' ) ; } catch ( e ) { } export interface JPEGEncodeOptions { quality : number ; subsampling ? : number ; } const DEFAULT_QUALITY = 95 ; param buffer param width param height export async function encodeJPEG ( buffer , width , height , options ) { try { if ( jpegTurbo && jpegTurbo . bufferSize && jpegTurbo . compressSync ) { const encodeOptions = { format : jpegTurbo . FORMAT_RGBA , width , height , quality : DEFAULT_QUALITY , ... options , } ; if ( buffer . length === width * height * 4 ) { const tmpBuffer = Buffer . alloc ( jpegTurbo . bufferSize ( encodeOptions ) ) ; return jpegTurbo . compress ( buffer , tmpBuffer , encodeOptions ) ; } } } catch ( e ) { jpegTurbo = undefined ; } const jpegBuffer2 = jpegJS . encode ( { width , height , data : buffer , } , options ? options . quality : DEFAULT_QUALITY ) ; return Promise . resolve ( jpegBuffer2 . data ) ; }