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

@shellophobia/transform-image-js

v1.0.5

Published

resize uploaded images on front end

Downloads

35

Readme

transform-image-js

Build Status Version License minified size

transform-image-js is a library to perform transformations on an image file e.g. resize an image within defined constraints and also allows to adjust the quality of image. One of the use cases is when you want to perform a size optimization on the image before uploading.

Getting started

Installing

Using npm:

npm i @shellophobia/transform-image-js

Using yarn:

yarn add @shellophobia/transform-image-js

Using jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/@shellophobia/transform-image-js/lib/transform-image-js.min.js"></script>

Using unpkg CDN:

<script src="https://unpkg.com/@shellophobia/transform-image-js/lib/transform-image-js.min.js"></script>

Usage

Import

in CommonJS:

const transformImage = require("@shellophobia/transform-image-js")

in ES6:

import transformImage from '@shellophobia/transform-image-js';

Resize image to max 500x500 with quality as 0.9:

Vanilla JS and HTML
<input id="demo" type="file" onchange="handleUpload">
function handleUpload(e){
  const file = e.target.files[0];
  // The library will add a property `transformImageJS` on window once you import it
  const transformImage = new transformImageJS.TransformImage({});
  transformImage.resizeImage(file, {maxHeight: 500, maxWidth:500, quality:0.9}).then(res=>{
    //The response returns an object that has the output blob in output attribute and has metadata for image sizes before and after transformation
    console.log(res);
  }).catch(err => {
    // handle error
  });
}

// using async function
async function handleUpload(e) {
  const file = e.target.files[0];
  const transformImage = new transformImageJS.TransformImage({});
  try {
    const res = await transformImage.resizeImage(file, {maxHeight: 500, maxWidth:500, quality:0.9});
    console.log(res);
  } catch(e) {
    // handle error
  }
}
React JSX
import React from "react";
import transformImage from "@shellophobia/transform-image-js";

const handleUpload = async (e) => {
  const file = e.target.files[0];
  console.log(file);
  const transformImage = new transformImage({});
  try {
    const res = await transformImage.resizeImage(file, {maxHeight: 500, maxWidth:500, quality:0.9});
    console.log(res);
  } catch (e) {
    console.log(e);
  }
}

export default function App() {
  return (
    <div className="App">
      <input type="file" onChange={handleUpload} />
    </div>
  );
}

API

Initialization options

Description

Following options can be passed during initialization of transformImage that returns an object on which methods can be called

transformImage(options)

| Name | Type | Description | Default | |------------------|----------|----------------------------------------------------------------------|------------------------| | sizeLimit | int | the byte size limit for the input file/blob | 16777216 bytes = 16MB | | outputType | enum | defines the output object format. Allowed values :- blob/base64/file | blob | | allowedFileTypes | []string | allowed types for the input file/blob e.g. PNG, JPEG, JPG | ["jpg", "png", "jpeg"] |

Methods

resizeImage(imageFile, options, fileName) => {Promise}

Description:

Resize an image file

Parameters:

| Name | Type | Description | Required | Default | |----------|-----------|---------------------------------------------------------------------------------------------|----------|---------| | image | File/Blob | File object / Blob to be resized | Yes | N/A | | options | Object | Additional options described below. The values can also override the TransformImage options | No | {} | | fileName | string | Name of the file if outputType is file (Optional) | No | "" |

Options

| Name | Type | Description | Default | |-----------|-------|-------------------------------------------------------------------|---------| | maxWidth | int | the max width for the file in px | 500 | | maxHeight | int | the max height for the file in px | 500 | | quality | float | a value between 0 and 1 to denote the quality of the output image | 0.9 |

Returns:

Promise that resolves to the output object

| Name | Type | Description | |----------|--------------------|--------------------------------------------------------------------| | output | blob/base64 string | Blob or base64 string based on configuration | | metadata | object | Metadata about initial image dimensions and final image dimensions |

Metadata

| Name | Type | Description | |----------------|------|-----------------------| | originalHeight | int | Original image height | | originalWidth | int | Original image width | | resizedHeight | int | Resized image height | | resizedWidth | int | Resized image width |

JQuery Plugin

image_compress_plugin is a jquery plugin that allows to add a file upload and compress functionality.

Usage

Importing

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@shellophobia/transform-image-js/jquery_plugin/image_compress_plugin.js"></script>

Example

<p>This is a demo for the resize image jquery plugin. Feel free to go through the source code 
<a href="https://github.com/shellophobia/UploadCompressImage/blob/master/jquery_plugin/image_compress_plugin.js">here</a></p>
<div id="fileinput">
  <button class="btn-upload"><i class="fas fa-cloud-upload-alt"></i> Click Here to Upload</button>
  <p class="drag-p">Or Drag N Drop the file</p>
  <input type="file" multiple="true">
</div>
<div id="preview"></div>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@shellophobia/transform-image-js/jquery_plugin/image_compress_plugin.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $("#fileinput").uploadFile({
    enablePreview: true,
    appendFileInput: false,
    autoSubmit: false,
    previewSelector: "#preview"
  });
});
</script>

Configuration gist for jquery plugin

License

MIT