Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

image-minify-client

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

image-minify-client

JavaScript module for running image optimization in web browser.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source
  • English
  • 한국어

Image Minify Client

Image Minify Client scales down by removing metadata from the browser and adjusting quality using canvas APIs.

It was created to reduce bandwidth and server CPU load. :)

Demo website

imageMinify

Note

  • Support input type only bmp, png, gif, jpeg, webp
  • Support output type only jpeg and webp
  • Loses animation if it has animation

Install

npm install image-minify-client

Quickstart example code

<img id="img" src="" alt="">
<input id="input" type="file">
import imc from "image-minify-client"

const img = document.getElementById('img')
const input = document.getElementById('input')

input.addEventListener('change', event => {
  const file = event.target.files[0]
  
  if (!file) { return }

  imc(file).then(rslt => {
    const newFile = rslt[0]
    const oldFile = rslt[1]

    img.src = URL.createObjectURL(newFile)

    console.log(newFile, oldFile)
  })
})

Usage

Syntax

module(File[, options])

Parameter

File

Require. Image object of file type.

options

Optional.

  • quality : (default: 0.8) Number in the 0 < value <= 1 range.
  • maxWidth : (default: undefined) Greater then 0. If the width of the image is greater than maxWidth, the width is adjusted to maxWidth while maintaining the proportions.
  • maxHeight : (default: undefined) Greater then 0. If the height of the image is greater than maxHeight, the height is adjusted to maxHeight while maintaining the proportions.
  • outputType : (default: jpeg) String jpeg | webp

Return

Since this method is asynchronous, it returns a promise. In case of resolve, an array of File objects is returned as shown below.

[ newFile , oldFile ]

Syntax

imc(File, {
  quality: 0.75,
  maxHeight: 300,
  outputType: 'webp'
}).then(result => {
  console.log('Before minify file: ', result[1])
  console.log('After minify file: ', result[0])
})

error

If an error occurs during operation, an object such as { code: number, message: string } is returned as an error. Even with the same error code, the message may be different. Details are as follows.

CodeMessage
400The first parameter cannot be empty.
400The first parameter is not a File type.
404The image could not be loaded.
409The process is already running.
412Failed to minify image.
413Image is over than the size supported.
415The first parameter is not a Image File type.
415"${ fileFormat }" format is not support
501Does not support modules.

Keywords

image

FAQs

Package last updated on 30 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts