Installations
npm install image-conversion
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
11.11.0
NPM Version
6.7.0
Releases
Unable to fetch releases
Download Statistics
Total Downloads
1,287,108
Last Day
1,131
Last Week
5,256
Last Month
28,461
Last Year
407,348
Bundle Size
6.84 kB
Minified
2.16 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.1.1
Package Id
image-conversion@2.1.1
Size
10.80 kB
NPM Version
6.7.0
Node Version
11.11.0
Publised On
23 Mar 2020
Total Downloads
Cumulative downloads
Total Downloads
1,287,108
Last day
-17.1%
1,131
Compared to previous day
Last week
-31.9%
5,256
Compared to previous week
Last month
-4.2%
28,461
Compared to previous month
Last year
11%
407,348
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
image-conversion
image-conversion is a simple and easy-to-use JS image convert tools, which provides many methods to convert between Image,Canvas,File and dataURL.
In addition,image-conversion can specify size to compress the image (test here).
Methods Map
Getting started
Install
1npm i image-conversion --save 2 3# or 4 5yarn add image-conversion
Include the library
in browser:
1<script src="https://cdn.jsdelivr.net/gh/WangYuLue/image-conversion/build/conversion.js"></script>
in CommonJS:
1const imageConversion = require("image-conversion")
in ES6:
1import * as imageConversion from 'image-conversion';
or
1import {compress, compressAccurately} from 'image-conversion';
Use examples
1<input id="demo" type="file" onchange="view()">
- Compress image to 200kb:
1function view(){ 2 const file = document.getElementById('demo').files[0]; 3 console.log(file); 4 imageConversion.compressAccurately(file,200).then(res=>{ 5 //The res in the promise is a compressed Blob type (which can be treated as a File type) file; 6 console.log(res); 7 }) 8} 9 10// or use an async function 11async function view() { 12 const file = document.getElementById('demo').files[0]; 13 console.log(file); 14 const res = await imageConversion.compressAccurately(file,200) 15 console.log(res); 16}
- Compress images at a quality of 0.9
1function view(){ 2 const file = document.getElementById('demo').files[0]; 3 console.log(file); 4 imageConversion.compress(file,0.9).then(res=>{ 5 console.log(res); 6 }) 7}
Methods list
image-conversion
provides many methods to convert between Image,Canvas,File and dataURL,as follow:
imagetoCanvas(image[, config]) → {Promise(Canvas)}
Description:
Convert an image object into a canvas object.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
image | image | a image object | |
config | object | optional | with this config you can zoom in, zoom out, and rotate the image |
Example:
1imageConversion.imagetoCanvas(image);
2
3//or
4
5imageConversion.imagetoCanvas(image,{
6 width: 300, //result image's width
7 height: 200, //result image's height
8 orientation:2,//image rotation direction
9 scale: 0.5, //the zoom ratio relative to the original image, range 0-10;
10 //Setting config.scale will override the settings of
11 //config.width and config.height;
12})
config.orientation
has many options to choose,as follow:
Options | Orientation |
---|---|
1 | 0° |
2 | horizontal flip |
3 | 180° |
4 | vertical flip |
5 | clockwise 90° + horizontal flip |
6 | clockwise 90° |
7 | clockwise 90° + vertical flip |
8 | Counterclockwise 90° |
Returns:
Promise that contains canvas object.
dataURLtoFile(dataURL[, type]) → {Promise(Blob)}
Description:
Convert a dataURL string to a File(Blob) object. you can determine the type of the File object when transitioning.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dataURL | string | a dataURL string | |
type | string | optional | determine the converted image type; the options are "image/png", "image/jpeg", "image/gif". |
Returns:
Promise that contains a Blob object.
compress(file, config) → {Promise(Blob)}
Description:
Compress a File(Blob) object.
Parameters:
Name | Type | Description |
---|---|---|
file | File(Blob) | a File(Blob) object |
config | number | object | if number type, range 0-1, indicate the image quality; if object type,you can pass parameters to the imagetoCanvas and dataURLtoFile method;Reference is as follow: |
Tips:
If you compress png transparent images, please select 'image/png' type.
Example:
1// number 2imageConversion.compress(file,0.8) 3 4//or 5 6// object 7imageConversion.compress(file,{ 8 quality: 0.8, 9 type: "image/jpeg", 10 width: 300, 11 height: 200, 12 orientation:2, 13 scale: 0.5, 14})
Returns:
Promise that contains a Blob object.
compressAccurately(file, config) → {Promise(Blob)}
Description:
Compress a File(Blob) object based on size.
Parameters:
Name | Type | Description |
---|---|---|
file | File(Blob) | a File(Blob) object |
config | number | object | if number type, specify the size of the compressed image(unit KB); if object type,you can pass parameters to the imagetoCanvas and dataURLtoFile method;Reference is as follow: |
Tips:
If you compress png transparent images, please select 'image/png' type
Example:
1// number
2imageConversion.compressAccurately(file,100); //The compressed image size is 100kb
3// object
4imageConversion.compressAccurately(file,{
5 size: 100, //The compressed image size is 100kb
6 accuracy: 0.9,//the accuracy of image compression size,range 0.8-0.99,default 0.95;
7 //this means if the picture size is set to 1000Kb and the
8 //accuracy is 0.9, the image with the compression result
9 //of 900Kb-1100Kb is considered acceptable;
10 type: "image/jpeg",
11 width: 300,
12 height: 200,
13 orientation:2,
14 scale: 0.5,
15})
Returns:
Promise that contains a Blob object.
canvastoDataURL(canvas[, quality, type]) → {Promise(string)}
Description:
Convert a Canvas object into a dataURL string, this method can be compressed.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
canvas | canvas | a Canvas object | |
quality | number | optional | range 0-1, indicate the image quality, default 0.92 |
type | string | optional | determine the converted image type; the options are "image/png", "image/jpeg", "image/gif",default "image/jpeg" |
Returns:
Promise that contains a dataURL string.
canvastoFile(canvas[, quality, type]) → {Promise(Blob)}
Description:
Convert a Canvas object into a Blob object, this method can be compressed.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
canvas | canvas | a Canvas object | |
quality | number | optional | range 0-1, indicate the image quality, default 0.92 |
type | string | optional | determine the converted image type; the options are "image/png", "image/jpeg", "image/gif",default "image/jpeg" |
Returns:
Promise that contains a Blob object.
dataURLtoImage(dataURL) → {Promise(Image)}
Description:
Convert a dataURL string to a image object.
Parameters:
Name | Type | Description |
---|---|---|
dataURL | string | a dataURL string |
Returns:
Promise that contains a Image object.
downloadFile(file[, fileName])
Description:
Download the image to local.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
file | File(Blob) | a File(Blob) object | |
fileName | string | optional | download file name, if none, timestamp named file |
filetoDataURL(file) → {Promise(string)}
Description:
Convert a File(Blob) object to a dataURL string.
Parameters:
Name | Type | Description |
---|---|---|
file | File(Blob) | a File(Blob) object |
Returns:
Promise that contains a dataURL string.
urltoBlob(url) → {Promise(Blob)}
Description:
Load the required Blob object through the image url.
Parameters:
Name | Type | Description |
---|---|---|
url | string | image url |
Returns:
Promise that contains a Blob object.
urltoImage(url) → {Promise(Image)}
Description:
Load the required Image object through the image url.
Parameters:
Name | Type | Description |
---|---|---|
url | string | image url |
Returns:
Promise that contains Image object.
License
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No security vulnerabilities found.