3 Steps To Resize Images In Javascript (Simple Examples)

Welcome to a quick tutorial and example of how to resize images in Javascript. Yes, resizing images is no longer a server-side thing. We can also do it in Javascript with just a few lines of code.

We can use the canvas API to resize images in Javascript:

  1. Start by creating a new image and canvas.
    • var img = new Image();
    • var canvas = document.createElement("canvas");
    • var ctx = canvas.getContext("2d");
  2. Resize on image load – img.onload = () => { ctx.drawImage(img, 0, 0, NEW WIDTH, NEW HEIGHT); };
  3. Set the image source to start – img.src = "IMAGE.JPG";

That covers the quick basics, but let us walk through a few actual examples – Read on!

 

 

TLDR – QUICK SLIDES

[web_stories_embed url=”https://code-boxx.com/web-stories/resize-images-javascript/” title=”How To Resize Images In Javascript” poster=”https://code-boxx.com/wp-content/uploads/2021/11/STORY-JS-20230518.webp” width=”360″ height=”600″ align=”center”]

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

 

JAVASCRIPT RESIZE IMAGE

All right, let us now get into the examples of how to resize images in Javascript.

 

EXAMPLE 1) RESIZE IMAGE & SHOW ON PAGE

1A) THE HTML

1-resize.html
<!-- (A) HTML -->
<!-- (A1) ORIGINAL IMAGE -->
<h1>Original Image (512 X 512)</h1>
<img src="demo.png">
 
<!-- (A2) RESIZE USING CANVAS -->
<h1>Resized Image (50%)</h1>
<canvas id="resized"></canvas>

Nothing much here… All we actually need is an empty <canvas> to draw the resized image.

 

1B) THE JAVASCRIPT

1-resize.html
// (B1) NEW IMAGE OBJECT & HTML CANVAS
var img = new Image(),
    canvas = document.getElementById("resized"),
    ctx = canvas.getContext("2d");

// (B2) RESIZE ON IMAGE LOAD
img.onload = () => {
  let width = Math.floor(img.naturalWidth * 0.5),
      height = Math.floor(img.naturalHeight * 0.5);
  canvas.width = width;
  canvas.height = height;
  ctx.drawImage(img, 0, 0, width, height);
};

// (B3) GO!
img.src = "demo.png";

Yep, that’s all, and this should be pretty straightforward.

  • (B1) Create a new Image() object, get the HTML canvas.
  • (B2) When the image is fully loaded, we calculate the new dimensions and draw the resized version onto the <canvas>.
  • (B3) Set the image source, start loading the image.

 

 

1C) THE DEMO

Original Image (512 X 512)

Resized Image (50%)

 

EXAMPLE 2) RESIZE IMAGE & DOWNLOAD

2A) THE HTML

2-download.html
<!-- (A) DOWNLOAD LINK -->
<a id="dlink"></a>

Since we are offering the resized image as a download, all we need is an empty <a> download link.

 

2B) THE JAVASCRIPT

2-download.html
// (B1) NEW IMAGE OBJECT & CANVAS
var img = new Image(),
    canvas = document.createElement("canvas"),
    ctx = canvas.getContext("2d");

// (B2) RESIZE ON IMAGE LOAD
img.onload = () => {
  // RESIZE IMAGE
  let width = Math.floor(img.naturalWidth * 0.5),
      height = Math.floor(img.naturalHeight * 0.5);
  canvas.width = width;
  canvas.height = height;
  ctx.drawImage(img, 0, 0, width, height);

  // SET DOWNLOAD LINK
  let dlink = document.getElementById("dlink");
  dlink.innerHTML = "Download";
  dlink.setAttribute("download", "resized.png");
  dlink.href = canvas.toDataURL("image/png");
};

// (B3) GO!
img.src = "demo.png";

Look no further, this is essentially the same process as the previous example. The only difference here is that we turn the canvas into a downloadable link – canvas.toDataURL("image/png").

 

 

2C) THE DEMO

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

COMPATIBILITY CHECK

These examples should work on all modern browsers.

 

LINKS & REFERENCES

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

Resize Image In Javascript (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

3 thoughts on “3 Steps To Resize Images In Javascript (Simple Examples)”

    1. Indeed, my dear genius. You have also completely missed out on the entire point. CSS cannot permanently reduce the image dimensions and file size.

      P.S. It is best that you educate yourself on the basics of digital images to better understand – http://shutha.org/node/796

Leave a Comment

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