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 | /** * 图片压缩处理 * @author luwc * @date 2022-10-02 * @returns {Promise<unknown>} */ function loadJs(src) { //'/static/js/squoosh/src/avif_enc.js' // let src = '//res.xxxxx.com/h5/js/squoosh/avif_enc.js' return new Promise((resolve, reject) => { let script = document.createElement( 'script' ) script.type = 'text/javascript' script.src = src document.body.appendChild(script) script.onload = () => { resolve() } script.onerror = () => { reject() } }) } /** * * @param file * @returns {Promise<unknown>} */ function imgConver(file) { // loadJs() return loadJs( '//res.xxxx.com/h5/js/squoosh/webp_enc.js' ).then(() => { // 加载成功,进行后续操作 return new Promise((resolve, reject) => { let _canvas = document.createElement( 'canvas' ) let _img = new Image() _img.src = window.URL.createObjectURL(file) _img.onload = function () { _canvas.setAttribute( 'width' , this .width) _canvas.setAttribute( 'height' , this .height) _canvas.getContext( '2d' ).drawImage( this , 0, 0) let data = _canvas .getContext( '2d' ) .getImageData(0, 0, this .width, this .height) webp_enc().then((webp_enc_wasm) => { let avifImg = webp_enc_wasm.encode( data.data, this .width, this .height, { quality: 75, target_size: 0, target_PSNR: 0, method: 4, sns_strength: 50, filter_strength: 60, filter_sharpness: 0, filter_type: 1, partitions: 0, segments: 4, pass: 1, show_compressed: 0, preprocessing: 0, autofilter: 0, partition_limit: 0, alpha_compression: 1, alpha_filtering: 1, alpha_quality: 100, lossless: 0, exact: 0, image_hint: 0, emulate_jpeg_size: 0, thread_level: 0, low_memory: 0, near_lossless: 100, use_delta_palette: 0, use_sharp_yuv: 0, } ) if (!avifImg || avifImg == null || avifImg == undefined) { reject( '自动压缩文件时,读取文件失败!' ) } else { let outImgBlob = new Blob([avifImg.buffer], { type: 'image/avif' , }) resolve({ blob: outImgBlob, url: URL.createObjectURL(outImgBlob), size: (file.size / 1024).toFixed(1) + ' Kb -> ' + (outImgBlob.size / 1024).toFixed(1) + ' Kb' , }) } }) } }) }) } /** * 大文件读取文件失败不知道为啥 * @param file * @returns {Promise<unknown>} */ function imgConverAvif(file) { // loadJs() return loadJs( '//res.xxx.com/h5/js/squoosh/avif_enc.js' ).then(() => { // 加载成功,进行后续操作 return new Promise((resolve, reject) => { let _canvas = document.createElement( 'canvas' ) let _img = new Image() _img.src = window.URL.createObjectURL(file) _img.onload = function () { _canvas.setAttribute( 'width' , this .width) _canvas.setAttribute( 'height' , this .height) _canvas.getContext( '2d' ).drawImage( this , 0, 0) let data = _canvas .getContext( '2d' ) .getImageData(0, 0, this .width, this .height) avif_enc().then((avif_enc_wasm) => { let avifImg = avif_enc_wasm.encode( data.data, this .width, this .height, { minQuantizer: 33, maxQuantizer: 63, minQuantizerAlpha: 33, maxQuantizerAlpha: 63, tileColsLog2: 0, tileRowsLog2: 0, speed: 8, subsample: 1, } ) if (!avifImg || avifImg == null || avifImg == undefined) { reject( '自动压缩文件时,读取文件失败!' ) } else { let outImgBlob = new Blob([avifImg.buffer], { type: 'image/avif' , }) resolve({ blob: outImgBlob, url: URL.createObjectURL(outImgBlob), size: (file.size / 1024).toFixed(1) + ' Kb -> ' + (outImgBlob.size / 1024).toFixed(1) + ' Kb' , }) } }) } }) }) } module.exports = { imgConver, loadJs, } |