npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

front-end-min

v2.0.0

Published

minify and resize images on the front end

Downloads

8

Readme

Minify and resize images on the frontend


How to install

via npm: npm install front-end-min

via unpkg: <script src="https://unpkg.com/front-end-min@latest"></script>


Documentataion

all functions imported from front-end-min use the Mini namespace.


compressResizeBlobify

Resize an image, compress using lossy compression, and turn an image into a blob

Mini.compressResizeBlobify(file, {
  aspectRatioPreserved: aspectRatioPreserved,
  inputWidth: inputWidth,
  inputHeight: inputHeight,
  smoothingOptions: smoothingOptions,
  quality: quality,
}).then((result) => {
  console.log(result);
});

arguments

a file and an options object

file

file (required) - image url | image blob data | an image file if not added the functions fails immediately and returns an error. Currently only supports .jpeg and .png files

options object

The object has the following properties:

aspectRatioPreserved (Boolean)

true|false true - keep the proportions of the image (aspect ratio) the same false - disregard the aspect ratio of the image

inputHeight/inputWidth (Number)

a number greater than 0 measured in pixels. keep in mind that input height and input width represent the maximum dimensions and may not be the exact dimensions if the aspect ratio is preserved

smoothingOptions (String)

"bi-cubic" | "bi-linear" the smoothness of the image - when images changes dimensions, information is either lost or more informaiton is needed. Smoothing options dictate how the decision of how to turn 4 pixels into one is made (assuming the image is shrinking) Or how to come up with new pixels in the case of an enlarged image

quality (float)

positive floating point number from 0 - 1 represets the degree of image quality with 1 being highest quality and 0 being the lowest

return

a promise object with access to the following properties

blob (blob)

binary encoding of the image data

objUrl (String)

a local url that can be used to view the image on your local machine

dataUrl (String)

a url that can be used to access the image from any client

canvas (HTML Element)

an HTML element that fits the maximum dimensions specified in inputHeight/inputWidth


imageify

turns a file containing image data into an instance of the HTMLImageElement

Mini.imageify(file).then((image) => {
  document.append(image);
});

arguments

| arguments | description | | --------- | ---------------------------- | | file | a file containing image data |

return

| properties | description | | ---------- | ----------------------------------- | | image | an instance of the HTMLImageElement |


calculateNewDims

returns new dimensions with respect to the aspect ratio of the image, given an image and maximum dimensions

Mini.calculateNewDims(img, aspectRatioPreserved, inputWidth, inputHeight).then(
  ({ newHeight, newWidth }) => {
    console.log(newHeight, newWidth);
  },
);

arguments

(img, aspectRatioPreserved, inputWidth, inputHeight)

| arguments | description | | ---------------------- | ---------------------------------------------------------------------------------------------- | | img | an instance of the HTMLImageElement | | aspectRatioPreserved | Boolean - default is true | | inputWidth/inputHeight | a number greater than 0 measured in pixels. represents MAXIMUM dimensions not exact dimensions |

return

returns a promise with a newHeight and newWidth property

| properties | description | | ------------------ | ----------------------- | | newHeight/NewWidth | Number - new dimensions |


resizeImage

resizes an image and returns a canvas with the resized image drawn in the contex

Example:

Mini.resizeImage(img, smoothingOptions, newWidth, newHeight).then((canvas) => {
  document.append(canvas);
});

arguments

| arguments | description | example | | ---------------------- | ------------------------------------------- | ------------------------------------------ | | file | image data (jpeg, png) | image url | image blob data | image file | | smoothingOptions | String - represents image interpolation | "bi-linear" | | inputHeight/inputWidth | a number greater than 0 measured in pixels. | 0 - Infinity |

return

returns a promise object with a canvas property

| properties | description | | ---------- | --------------------------------------------------- | | canvas | a canvas element with an image drawn in the context |


blobifyCanvas

turns a canvas element into a binary encoding (blob)

Mini.blobifyCanvas(canvas).then((blob) => {
  console.log(blob);
});

arguments

| arguments | description | | --------- | --------------------------------------------------- | | canvas | a canvas element with an image drawn in the context |

return

returns a promise with a blob property

| properties | description | | ---------- | ----------------------------------- | | blob | binary encoding of a canvas element |


blobToDataUrl

accepts blob data and returns a promise object

Mini.blobToDataUrl(blob).then((dataURL) => {
  console.log(dataUrl);
});

arguments

| arguments | description | | --------- | ------------------------------------ | | blob | a binary representation of file data |

return

returns a promise object with a dataUrl property

| properties | description | | ---------- | -------------------------------------------- | | dataUrl | uniform resource locator that works anywhere |


blobToObjectUrl(blob)

turns a blob into an object Url

Mini.blobToObjectUrl(blob).then((objUrl) => {
  console.log(objUrl);
});

arguments

| arguments | description | | --------- | ------------------------------------ | | blob | a binary representation of file data |

return

returns a promise object with a object url property

| properties | description | | ---------- | -------------------------------------------------------------- | | objUrl | uniform resource locator that works only on your local machine |


Dependancies

none


Example Projects

Example open source project using front-end-min can bee seen here: https://github.com/MaxSultan/img-compressor