In today's web development landscape, it's often desirable to resize images on the client-side before uploading them to the server. This approach can optimize image quality, reduce server load, and improve user experience by speeding up page loading times.
While Flash has been a common option for image resizing, many developers prefer to avoid its use. Fortunately, there is a solution that utilizes JavaScript to resize images effectively.
Open Source Image Resizing Algorithm
The open source algorithm available on GitHub (https://gist.github.com/dcollien/312bce1270a5f511bf4a) provides a reliable method for resizing images client-side. It offers an ES6 version and a .js version that can be included in script tags.
Implementation
To implement the resizing algorithm, follow these steps:
document.getElementById('select').onchange = function(evt) {
ImageTools.resize(this.files[0], {
width: 320, // maximum width
height: 240 // maximum height
}, function(blob, didItResize) {
// didItResize will be true if it managed to resize it, otherwise false (and will return the original file as 'blob')
document.getElementById('preview').src = window.URL.createObjectURL(blob);
// you can also now upload this blob using an XHR.
});
};
Features and Support
The algorithm boasts a range of features and support, including:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3