1.0.8 • Published 11 months ago

web-image-resize v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

✨ Features

  • Optimize image to target size
  • Resize image

📦Install

npm install web-image-resize

🔨Usage

import React, { useState } from "react";
import resize from "web-image-resize";

const Test = () => {

    const [file, setFile] = useState(null)
    const [img, setImg] = useState("")

    const handleFileChange = (e) => {
        setFile(e.target.files[0])
    }

    return (
        <div>
            <input type="file" onChange={handleFileChange} />
            <button onClick={async () => await resize.optimize({
                file,
                maxFileSize: 50,
                maxDimensionPixel: 2048,
                maxDeviation: 100,
                iteration: 8,
                targetType: 'image/webp',
                callback: (data) => {
                    console.log(data)
                    setImg(URL.createObjectURL(data))
                }
            })}>optimize</button>
            <button onClick={async () => await resize.resize({
                file,
                height: 50,
                autoScale: true,
                callback: (data) => {
                    console.log(data)
                    setImg(URL.createObjectURL(data))
                }
            })}>resize</button>
            <img src={img} />
        </div>
    )
}

export default Test

API

Optimize

file | Type | Default value | Description | | --- | --- | --- | | File | Requested | The original file |

maxFileSize | Type | Default value | Description | | --- | --- | --- | | number | Requested | Target size for optimization |

callback | Type | Default value | Description | | --- | --- | --- | | Function | Requested | A callback function for return optimized file, this will return a Blob |

optional

maxDeviation | Type | Default value | Description | | --- | --- | --- | | number | 100 | Deviation for optimization |

iteration | Type | Default value | Description | | --- | --- | --- | | number | 8 | Maximum times for optimization |

targetType | Type | Default value | Description | | --- | --- | --- | | string | 'image/webp' | Target file type |

Resize

file | Type | Default value | Description | | --- | --- | --- | | File | Requested | The original file |

callback | Type | Default value | Description | | --- | --- | --- | | Function | Requested | A callback function for return resized file, this will return a Blob |

optional

height | Type | Default value | Description | | --- | --- | --- | | number | null | |

width | Type | Default value | Description | | --- | --- | --- | | number | null | |

autoScale | Type | Default value | Description | | --- | --- | --- | | boolean | false | Fit original file scale |

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago