Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » How To Resize An Image Using Pure Vanilla JavaScript In Aspect Ratio?

How To Resize An Image Using Pure Vanilla JavaScript In Aspect Ratio?

Allowing users to upload photos directly to a website, without first resizing them, hurts the user experience for two reasons.

Large photos take time to upload ⏱
Large photos cost the user money in bandwidth 💸

Photos should be resized on the client before uploading, especially now that smartphones are available that can capture 100MP photos. Luckily there’s an easy way to do it using the canvas element and a few lines of JavaScript.

Usually, when you are implementing an image upload, that time you want to resize the image, or before uploading you want to change the image size. So let’s explain how to resize the image and convert it into base64 content.

In this tutorial, You will learn how to resize an image using javascript, and after resizing the image how-to shows the preview of resizing an image. Here you will learn the resizing of images and show a preview of resizing the image.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Create A Simple Countdown Timer Using Pure JavaScript


  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Resize An Image Using Pure Vanilla JavaScript In Aspect Ratio?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.


<h1>Select An Image to Resize 👇</h1>
<form id="my-form" action="" method="post"r>
<input type="file" name="photo" accept="image/*" required />
<button type="submit">Resize Image</button>
<img id="img-preview" style="width:150px;">


<script type="text/javascript">
const form = document.querySelector('#my-form');

form.addEventListener('submit', async (e) => {

// Get data URI of the selected image
const formData = new FormData(e.currentTarget);
const photoField = formData.get('photo');
const dataUri = await dataUriFromFormField(photoField);

const imgEl = document.createElement('img');
imgEl.addEventListener('load', () => {
const resizedDataUri = resizeImage(imgEl, 300);
document.querySelector('#img-preview').src = resizedDataUri;
imgEl.src = dataUri;

function dataUriFromFormField (field) {
return new Promise((resolve) => {
const reader = new FileReader();

reader.addEventListener('load', () => {


function resizeImage (imgEl, wantedWidth) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

const aspect = imgEl.width / imgEl.height;

canvas.width = wantedWidth;
canvas.height = wantedWidth / aspect;

ctx.drawImage(imgEl, 0, 0, canvas.width, canvas.height);
return canvas.toDataURL();


No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Recommended For You:
Simple PHP Web Page Counter Without Database

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *