All Projects → thangman22 → frontend-image-encode

thangman22 / frontend-image-encode

Licence: other
This is script and implementation of frontend image compression, convert, rotate, resize based on codecs in Google/Squoosh. All codecs are copy from the Squoosh repo without modifying. if you want to pre-process images before uploading them to the server, please use this repo for reference

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to frontend-image-encode

n2t-wasm
Emulator for the Hack CPU.
Stars: ✭ 41 (-19.61%)
Mutual labels:  wasm
material-yew
Yew wrapper for Material Web Components
Stars: ✭ 116 (+127.45%)
Mutual labels:  wasm
wasm-cn
[翻译中] WebAssembly 中文文档
Stars: ✭ 22 (-56.86%)
Mutual labels:  wasm
SkyEmu
Game Boy, Game Boy Color, and Game Boy Advanced Emulator
Stars: ✭ 59 (+15.69%)
Mutual labels:  wasm
js-image-compressor
A simple image compressor of javascript
Stars: ✭ 100 (+96.08%)
Mutual labels:  image-compression
wasm-bindgen-webcam-stream
A small example on how to get webcam stream on rust-wasm
Stars: ✭ 11 (-78.43%)
Mutual labels:  wasm
wasm.cljc
Spec compliant WebAssembly compiler, decompiler, and generator
Stars: ✭ 178 (+249.02%)
Mutual labels:  wasm
eosio-rust
EOSIO SDK for Rust – APIs for building smart contracts on EOSIO blockchains in Rust
Stars: ✭ 93 (+82.35%)
Mutual labels:  wasm
reactr
Function scheduler for Go & WebAssembly
Stars: ✭ 264 (+417.65%)
Mutual labels:  wasm
pico-ml
A toy programming language which is a subset of OCaml.
Stars: ✭ 36 (-29.41%)
Mutual labels:  wasm
wasmfun
Getting the hang of WASM - generate WASM from Python
Stars: ✭ 34 (-33.33%)
Mutual labels:  wasm
rust-monaco
Rust WASM bindings for the Monaco Editor
Stars: ✭ 23 (-54.9%)
Mutual labels:  wasm
Wasmite
Now WebAssembly has proper testing, unit-testing and debugging 🤗
Stars: ✭ 20 (-60.78%)
Mutual labels:  wasm
web logger
Rust Logger for Web Browsers
Stars: ✭ 19 (-62.75%)
Mutual labels:  wasm
zero-graphics
Application framework based on OpenGL ES 2.0. Runs on desktop machines, Android phones and the web
Stars: ✭ 72 (+41.18%)
Mutual labels:  wasm
swaplink
Site to perform peer to peer atomic swaps on the Algorand blockchain
Stars: ✭ 15 (-70.59%)
Mutual labels:  wasm
proxy-wasm-cpp-host
WebAssembly for Proxies (C++ host implementation)
Stars: ✭ 55 (+7.84%)
Mutual labels:  wasm
arima
ARIMA, SARIMA, SARIMAX and AutoARIMA models for time series analysis and forecasting in the browser and Node.js
Stars: ✭ 31 (-39.22%)
Mutual labels:  wasm
Uno.Wasm.Bootstrap
A simple nuget package to run C# code in a WASM-compatible browser
Stars: ✭ 242 (+374.51%)
Mutual labels:  wasm
whistle
🕴 One hella programming language
Stars: ✭ 27 (-47.06%)
Mutual labels:  wasm

Frontend Image compression

This is script and implementation of frontend image compression, convert, rotate, resize based on codecs in Google/Squoosh. All codecs are copy from the Squoosh repo without modifying. if you want to pre-process images before uploading them to the server, please use this repo for reference

Usage

<script type="module">
    import { loadImage, encodeWebP, encodeWebP2, encodeAvif, encodeJpeg, encodeJxl, rotateImage, resizeImage, encodeOnixPng, quantizeImage, resizePixelImage } from "./lib.js";

    // Load image before encode 
    const image = await loadImage("./assets/images/example.jpg");

    // Define options is nees
    const options = {
        minQuantizer: 33,
        maxQuantizer: 63,
        minQuantizerAlpha: 33,
        maxQuantizerAlpha: 63,
        tileColsLog2: 0,
        tileRowsLog2: 0,
        speed: 8,
        subsample: 1,
        module: {
            avifEncJs: "/squoosh/codecs/avif/enc/avif_enc.js"
        }
    }

    // Call function to encode image return is base64 format
    const webPImageResult = await encodeAvif(image, options);

    document.getElementById('webp-image').src = webPImageResult
</script>

Available methods & options

Encode WEBP

Convert image to WEBP format with https://github.com/webmproject/libwebp

// Available & Default options
const options = {
    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,
    module: {
        webpEncJs: "/squoosh/codecs/webp/enc/webp_enc.js"
    }
}

// Call function to encode image return is base64 format
const result = await encodeWebP(image, options);

Encode AVIF

Convert image to AVIF format

// Available & Default options
const options = {
    minQuantizer: 33,
    maxQuantizer: 63,
    minQuantizerAlpha: 33,
    maxQuantizerAlpha: 63,
    tileColsLog2: 0,
    tileRowsLog2: 0,
    speed: 8,
    subsample: 1,
    module: {
        avifEncJs: "/squoosh/codecs/avif/enc/avif_enc.js"
    }
}

// Call function to encode image return is base64 format
const result = await encodeWebP(image, options);

Encode JPEG

Encode JPEG image with mozjpeg

// Available & Default options
const options = {
    quality: 75,
    baseline: false,
    arithmetic: false,
    progressive: true,
    optimize_coding: true,
    smoothing: 0,
    color_space: 3 /*YCbCr*/,
    quant_table: 3,
    trellis_multipass: false,
    trellis_opt_zero: false,
    trellis_opt_table: false,
    trellis_loops: 1,
    auto_subsample: true,
    chroma_subsample: 2,
    separate_chroma_quality: false,
    chroma_quality: 75,
    module: {
        mozjpegEncJs: "/squoosh/codecs/mozjpeg/enc/mozjpeg_enc.js"
    }
}

// Call function to encode image return is base64 format
const result = await encodeJpeg(image, options);

Encode PNG

Encode PNG image with oxipng

// Available & Default options
const options = {
    level: 2,
    module: {
        pngEncDecJs: "/squoosh/codecs/png/pkg/squoosh_png.js",
        oxipngEncJs: "/squoosh/codecs/oxipng/pkg/squoosh_oxipng.js",
        squooshPngBgWasm: "/squoosh/codecs/png/pkg/squoosh_png_bg.wasm",
        squooshOxipngBgWasm: "/squoosh/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm"
    }
}

// Call function to encode image return is base64 format
const result = await encodeOnixPng(image, options);

Rotate Image

const degree = 90
// Call function to encode image return is base64 format
const result = await rotateImage(image, degree);

Resize Image

const outputWidth = 300
const outputHeight = 300
const options = {
    method: 3, // triangle = 0, catrom = 1, mitchell = 2, lanczos3 = 3
    fitMethod: 'stretch',
    premultiply: true,
    linearRGB: true,
    module: {
        squooshResizeJs: "/squoosh/codecs/resize/pkg/squoosh_resize.js",
        squooshResizeBgWasm: "/squoosh/codecs/resize/pkg/squoosh_resize_bg.wasm"
    }
}
const aspectRatio = true

// Call function to encode image return is base64 format
const result = await resizeImage(image, outputWidth, outputHeight, options, aspectRatio)

Resize Pixel Art Image

const options = {
    factor: 2,
    module: {
        squooshhqxJs: "/squoosh/codecs/hqx/pkg/squooshhqx.js",
        squooshhqxBgWasm: "/squoosh/codecs/hqx/pkg/squooshhqx_bg.wasm"
    }
}
const result = await resizePixelImage(image, options)

Reduce Color of Image

const options = {
    numColors: 255,
    dither: 1.0,
    module: {
        imagequantJs: "/squoosh/codecs/imagequant/imagequant.js"
    }
}
const result = await quantizeImage(image, options)

*** Browser Not Support yet ***

encode WEBP2

const options = {
    quality: 75,
    alpha_quality: 75,
    effort: 5,
    pass: 1,
    sns: 50,
    uv_mode: 0 /*UVMode.UVModeAuto*/,
    csp_type: 0 /*Csp.kYCoCg*/,
    error_diffusion: 0,
    use_random_matrix: false,
    module: {
        wp2EncJs : "/squoosh/codecs/wp2/enc/wp2_enc.js"
    }
}
const result = await encodeWebP2(image, options)

encode JPEGXL

const options = {
    speed: 4,
    quality: 75,
    progressive: false,
    epf: -1,
    nearLossless: 0,
    lossyPalette: false,
    module: {
        jxlEncJs: "/squoosh/codecs/jxl/enc/jxl_enc.js"
    }
}
const result = await encodeJxl(image, options)

Todo

  • Support Web Worker (Not check yet)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].