image-resize-compress
is a lightweight library that enables you to compress, resize, or convert images effortlessly. It supports working with File
, Blob
, and even URLs without any additional dependencies.
β¨ Demo
npm install --save image-resize-compress
yarn add image-resize-compress
import { blobToURL, urlToBlob, fromBlob, fromURL } from 'image-resize-compress';
var imageResizeCompress = require('image-resize-compress');
Include the library in your HTML file:
<script src="https://cdn.jsdelivr.net/npm/image-resize-compress@1/dist/index.js"></script>
import { fromBlob, blobToURL } from 'image-resize-compress';
const handleBlob = async (blobFile) => {
const quality = 80; // For webp and jpeg formats
const width = 'auto'; // Original width
const height = 'auto'; // Original height
const format = 'webp'; // Output format
const resizedBlob = await fromBlob(blobFile, quality, width, height, format);
const url = await blobToURL(resizedBlob);
console.log('Resized Blob:', resizedBlob);
console.log('Blob URL:', url);
};
You can use the generated URL to display the image:
<img src="{url}" alt="Resized image" />
import { fromURL, blobToURL } from 'image-resize-compress';
const handleURL = async (imageUrl) => {
const quality = 80;
const width = 'auto';
const height = 'auto';
const format = 'jpeg';
const resizedBlob = await fromURL(imageUrl, quality, width, height, format);
const url = await blobToURL(resizedBlob);
console.log('Resized Blob:', resizedBlob);
console.log('Blob URL:', url);
};
Note: Ensure the server hosting the image allows CORS requests.
<script src="https://cdn.jsdelivr.net/npm/image-resize-compress/dist/index.min.js"></script>
<script>
async function resizeImage() {
const file = document.querySelector('#fileInput').files[0];
const resizedBlob = await imageResizeCompress.fromBlob(
file,
75,
0,
0,
'webp',
);
console.log(resizedBlob);
}
</script>
<input type="file" id="fileInput" onchange="resizeImage()" />
Converts a Blob
or File
into a Data URL
.
blob
(Blob | File): The file or blob to convert.
blobToURL(file).then((url) => console.log(url));
Fetches an image from a URL and converts it into a Blob
.
- url (string): The URL of the image.
urlToBlob('https://example.com/image.png').then((blob) => console.log(blob));
fromBlob(blob: Blob | File, quality?: number, width?: number | string, height?: number | string, format?: string) β Promise<Blob>
Resizes, compresses, and/or converts a Blob
or File
.
- blob (Blob | File): The input file or blob.
- quality (number): Compression quality (for webp or jpeg).
- width (number | string): Target width (use
auto
to scale based on height). - height (number | string): Target height (use
auto
to scale based on width). - format (string): Desired format (e.g., jpeg, webp).
fromBlob(file, 80, 'auto', 100, 'jpeg').then((resizedBlob) =>
console.log(resizedBlob),
);
fromURL(url: string, quality?: number, width?: number | string, height?: number | string, format?: string) β Promise<Blob>
Resizes, compresses, and/or converts an image from a URL.
See Cross-Origin Resource Sharing (CORS)
- url (string): The image URL.
- quality (number): Compression quality (for webp or jpeg).
- width (number | string): Target width (use
auto
to scale based on height). - height (number | string): Target height (use
auto
to scale based on width). - format (string): Desired format (e.g., jpeg, webp).
fromURL('https://example.com/image.png', 75, 200, 'auto', 'webp').then(
(resizedBlob) => console.log(resizedBlob),
);
image-resize-compress
supports most modern browsers. However:
Older browsers (e.g., IE) may require polyfills for Promise and Fetch API.
Feel free to contribute, report bugs, or suggest features! π