Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JS library to transform the image

License

NotificationsYou must be signed in to change notification settings

shellophobia/transform-image-js

Repository files navigation

Build StatusVersionLicenseminified 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:

<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>

Usage

Import

in CommonJS:

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

in ES6:

importtransformImagefrom'@shellophobia/transform-image-js';

Resize image to max 500x500 with quality as 0.9:

Vanilla JS and HTML
<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}}
React JSX
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>);}

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)

NameTypeDescriptionDefault
sizeLimitintthe byte size limit for the input file/blob16777216 bytes = 16MB
outputTypeenumdefines the output object format. Allowed values :- blob/base64/fileblob
allowedFileTypes[]stringallowed 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:

NameTypeDescriptionRequiredDefault
imageFile/BlobFile object / Blob to be resizedYesN/A
optionsObjectAdditional options described below. The values can also override the TransformImage optionsNo{}
fileNamestringName of the file if outputType is file (Optional)No""
Options
NameTypeDescriptionDefault
maxWidthintthe max width for the file in px500
maxHeightintthe max height for the file in px500
qualityfloata value between 0 and 1 to denote the quality of the output image0.9

Returns:

Promise that resolves to the output object

NameTypeDescription
outputblob/base64 stringBlob or base64 string based on configuration
metadataobjectMetadata about initial image dimensions and final image dimensions
Metadata
NameTypeDescription
originalHeightintOriginal image height
originalWidthintOriginal image width
resizedHeightintResized image height
resizedWidthintResized image width

JQuery Plugin

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

Usage

Importing

<scripttype="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<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>

Configuration gist for jquery plugin

License

MIT

Packages

 
 
 

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp