- Notifications
You must be signed in to change notification settings - Fork2
JS library to transform the image
License
shellophobia/transform-image-js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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.
Using npm:
npm i @shellophobia/transform-image-js
Using yarn:
yarn add @shellophobia/transform-image-js
Using jsDelivr CDN:
<scriptsrc="https://cdn.jsdelivr.net/npm/@shellophobia/transform-image-js/lib/transform-image-js.min.js"></script>
Using unpkg CDN:
<scriptsrc="https://unpkg.com/@shellophobia/transform-image-js/lib/transform-image-js.min.js"></script>
in CommonJS:
consttransformImage=require("@shellophobia/transform-image-js")
in ES6:
importtransformImagefrom'@shellophobia/transform-image-js';
<inputid="demo"type="file"onchange="handleUpload">
functionhandleUpload(e){constfile=e.target.files[0];// The library will add a property `transformImageJS` on window once you import itconsttransformImage=newtransformImageJS.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 transformationconsole.log(res);}).catch(err=>{// handle error});}// using async functionasyncfunctionhandleUpload(e){constfile=e.target.files[0];consttransformImage=newtransformImageJS.TransformImage({});try{constres=awaittransformImage.resizeImage(file,{maxHeight:500,maxWidth:500,quality:0.9});console.log(res);}catch(e){// handle error}}
importReactfrom"react";importtransformImagefrom"@shellophobia/transform-image-js";consthandleUpload=async(e)=>{constfile=e.target.files[0];console.log(file);consttransformImage=newtransformImage({});try{constres=awaittransformImage.resizeImage(file,{maxHeight:500,maxWidth:500,quality:0.9});console.log(res);}catch(e){console.log(e);}}exportdefaultfunctionApp(){return(<divclassName="App"><inputtype="file"onChange={handleUpload}/></div>);}
Following options can be passed during initialization of transformImage that returns an object on which methods can be called
| 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"] |
Resize an image file
| 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 | "" |
| 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 |
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 |
| Name | Type | Description |
|---|---|---|
| originalHeight | int | Original image height |
| originalWidth | int | Original image width |
| resizedHeight | int | Resized image height |
| resizedWidth | int | Resized image width |
image_compress_plugin is a jquery plugin that allows to add a file upload and compress functionality.
<scripttype="text/javascript"src="https://cdn.jsdelivr.net/npm/@shellophobia/transform-image-js/jquery_plugin/image_compress_plugin.js"></script>
<p>This is a demo for the resize image jquery plugin. Feel free to go through the source code<ahref="https://github.com/shellophobia/UploadCompressImage/blob/master/jquery_plugin/image_compress_plugin.js">here</a></p><divid="fileinput"><buttonclass="btn-upload"><iclass="fas fa-cloud-upload-alt"></i> Click Here to Upload</button><pclass="drag-p">Or Drag N Drop the file</p><inputtype="file"multiple="true"></div><divid="preview"></div><scripttype="text/javascript"src="https://code.jquery.com/jquery-1.12.4.min.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/npm/@shellophobia/transform-image-js/jquery_plugin/image_compress_plugin.js"></script><scripttype="text/javascript">$(document).ready(function(){$("#fileinput").uploadFile({enablePreview:true,appendFileInput:false,autoSubmit:false,previewSelector:"#preview"});});</script>
About
JS library to transform the image
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.