惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
美团技术团队
NISL@THU
NISL@THU
C
Cisco Blogs
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Cloudbric
Cloudbric
雷峰网
雷峰网
T
Tailwind CSS Blog
博客园 - 司徒正美
The Register - Security
The Register - Security
L
LangChain Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threat Research - Cisco Blogs
I
InfoQ
S
Schneier on Security
L
Lohrmann on Cybersecurity
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
D
Docker
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Hacker News: Front Page
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Webroot Blog
Webroot Blog
MongoDB | Blog
MongoDB | Blog

ImageKit.io Blog

Next.js Image Optimization with ImageKit Use Video as a Background in Your Next.js Project How to Fix Autoplay Video in Next.js How Durian Scaled a Visual-First Retail Experience to 350K Monthly Visitors Online How Matsmart accelerated image delivery across countries with ImageKit AI in Digital Asset Management: From Smart Workflows to Agentic Automation How Joseph Joseph unified and secured global video delivery with ImageKit How Modall powers fast, effortless media delivery across 40+ projects with ImageKit Digital Asset Management (DAM) Trends: 2026 Report How to add a poster image to Video.js player (and automate it) HLS streaming with Video.js + React Building the future of storytelling with fast, AI-powered video delivery How PushOwl delivers 100M+ image-rich notifications seamlessly with ImageKit How Homify delivers millions of interior design images seamlessly with ImageKit Better event discovery with lightning‑fast videos & images Adding video player in React Native Video player in Angular applications Crop and resize videos in React Next.js image and video upload React image and video upload React video optimization How we quadrupled our traffic to 625K monthly page views How Apollo 24|7 boosted performance & reduced costs with ImageKit Simplify your media workflows with ImageKit DAM integrations Extending Lighthouse for custom image and video optimization analysis Brand Asset Management: What is it? How does it work? WordPress Digital Asset Management Guide - Manage your WP media assets better Why Shopify retailers need a digital asset management solution DAM vs. SharePoint: Which is best for you? AI-powered Metadata and Tagging in Digital Asset Management How Hopscotch built India's largest online Kids' fashion brand with ImageKit Dropbox Vs. DAM: Which Is The Right Tool For Digital Asset Management Digital Asset Management for Photographers: A Complete Guide Why digital asset management for agencies is essential Helping both Top and Bottom Line: SaffronStays rapid, profitable growth with ImageKit How KreditBee simplified media experiences with ImageKit Google Drive alternatives for businesses (with fast-growing teams) Node.js image upload ImageKit: The Secret Ingredient in Swiggy’s Expansion Journey Streamlining the Design Approval Process: A Comprehensive Guide AV1 Codec - Complete guide for video application devs PHP image and video upload Angular image & video upload AV1 vs VP9: Which codec should you choose? Adding video player in Next.js React Video Player VP8 vs VP9 - In the context of online video delivery Exploring WebM vs MP4 7 Free Digital Asset Management Software that are not Open-Source Comparing 9 Top Digital Asset Management Tools in the Market What are Brand Standards and Why do they Matter? Boost Sales and Brand Appeal: Essential Tips for eCommerce Image Management Brand Recall: The Strategy to Create Unforgettable Brands How to upload files in HTML? Branding for Small Businesses (2025 Edition) Everything you need to know about VP9 codec Recent updates from ImageKit and what's next Best Ways to Write RFP For Digital Asset Management (+ with free RFP template) What is Brand Dilution? How to Avoid It? Explained with [Examples] The Importance of Brand Identity: Leveraging Digital Asset Management for Impact From Launch to Scale: How to Launch a Brand Campaign Digital Asset Management Requirements - What do You Need to Evaluate and How? Marketing Collateral Management: A Quick End-to-End Guide Video Content Management System: What Is It And How To Choose One? Dropbox vs. Google Drive vs. Onedrive: The Best Cloud Storage Solution How to Build Brand Trust: Get Started In 2025 Google Drive vs. Box: A Detailed Comparison How Digital Asset Management Solutions Help Protect Brand Equity A DAM Solution Can Safeguard Your Digital Intellectual Property - Here’s How WebP Vs. PNG: Which Image Format Should You Use and Why? How to Resize Images in Bootstrap Easily Progressive jpegs (PJPEG): the key to loading images faster on your website Dropbox vs. Google Drive: The Best Cloud Storage For Digital Assets Dropbox Pros & Cons In 2024: An In-Depth Analysis and Why A DAM Solution Stands Out Google Drive Vs OneDrive: The Better Storage Option For Digital Assets Manage your video assets better with video metadata Understanding DAM's Role in Strengthening Brand Identity Digital asset management strategy: What to know before creating one The Ultimate Guide To Marketing Agency Onboarding 6 Solutions To Simplify Large File Sharing Over The Web A Step-by-Step Breakdown of a Video Production Workflow 13 Digital Asset Management Use Cases You Should Know How to Conduct a Brand Audit and Manage Your Brand Assets Costly Consequences of Inconsistent Branding And How DAM Can Help Dynamic Asset Transformation: What It Is, Why You Need It, and How ImageKit Can Help Everything You Need to Know About HTML Video Autoplay How To Select Your DAM Vendor: A Complete Guide How to Boost User Experience with Smart Digital Asset Management React Image Optimization: A Guide for Web Developers Why Should DAM Be A Part Of Your MarTech Stack? Unleashing the Power of Content Repurposing with ImageKit MKV vs MP4: Which Video File Format Is Better for Your Needs? Digital Asset Management For Ecommerce: A Complete Guide How an Image Tagging Software can Transform Your Image Search How to Manage Your Content Lifecycle Effectively M4V vs MP4: Which Video Format Should You Use and Why? Why Every Business Needs An Image Management System All The Questions To Ask During A Dam Demo Which is the Best Image Format for Your Website? Uploading Multiple Files Using JavaScript: A Comprehensive Guide
Image compression techniques in JavaScript (Updated 2025)
Rahul Nanwani · 2023-01-30 · via ImageKit.io Blog

High-quality images go a long way in impressing your website visitors. However, they can slow down your website and hamper your user experience. So, it helps compress these images in file size to remain appealing to the users without hindering UX.

Image compression can be done in two ways — lossy or lossless.

What is lossy image compression?

Lossy compression is where the original image file is removed and replaced with the compressed image file. Since the original image cannot be restored, it is also referred to as irreversible compression. However, if done correctly, the data loss is not usually noticeable.

What is lossless image compression?

On the other hand, lossless compression does not lose data in the process. Instead, it “packs” the data into smaller sizes. Imagine writing a logo para in shorthand that a system can read and understand. Lossless compression is akin to that.

Lossless compression is favored for archiving purposes because it can retain the original asset without resulting in data loss. It is often used in medical imaging, technical drawings, clip art, comics, etc.

In addition to these techniques, you can compress images using JavaScript and dedicated tools.

This tutorial will look at compressing images using canvas, browser-image-compression JavaScript library, and ImageKit.‌‌

Why do you need image compression?

Image compression delivers several benefits, including:

✅ Reduced file size

The most fundamental benefit of image compression is that it reduces file size. You can keep compressing the image until it's the size you want, depending on the sort of file you're squeezing.

✅ SEO-friendliness

Google search engines use the loading speeds of online pages to rank web pages. As a result, the lower the image weight, the higher the web's score and the chances of scoring higher SERPs.

✅ Faster website loading speed

Using images that are less in weight will help the web load faster on mobile phones. Google does more weightage to websites that perform well on mobile devices.

Also, some web hosting firms also require compressed images to develop websites that load faster than their competitors.

Now that we know the need for image compression let’s get cut to the chase on how to do it using Canvas.

Image compression and demo using Canvas

‌‌To get started, we'll need to set up our project.

We will run the code command to start Visual Studio Code from the terminal.‌‌

Note: If you don't have Visual Studio Code installed, you won't be able to use code command.

Using the command below, we'll create the script.js and index.html files.

touch index.html script.js

We'll put the following snippet inside index.html to make a simple HTML structure referencing the  script.js file we created earlier.‌‌

<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        Image compression and demo using Canvas
    </title>
</head>
<body>
   <div id="container">
      <h3>  Image compression and demo using Canvas</h3>
      <input id="imgId" type="file" accept="image/*"/>
  </div>
  <script src="script.js"></script>
</body>
</html>

We'll add the code snippet below into script.js to declare max_width and  max_height , which we'll use to compute the image size uploaded later in the tutorial.

const MAX_WIDTH = 320;
const MAX_HEIGHT = 180;

const INPUT = document.getElementById("imgId");

We can now read the file, utilize the onload callback to process and also create a helper image object using the blob URL as the image source, as shown below:


//...

INPUT.onchange = function (event) {
  const file = event.target.files[0]; // get the file
  const blobURL = URL.createObjectURL(file);
  const img = new Image();
  img.src = blobURL;
  
  img.onload = function () {
    // Calculate image size and Create canvas
  }:
};

We need to create a function to handle image size calculation and read image bytes before calculating image size and creating a canvas for image compression.

Let us add the snippet below to create the calculateSize function.


function calculateSize(img, maxWidth, maxHeight) {
  let width = img.width;
  let height = img.height;

  // calculate the width and height, constraining the proportions
  if (width > height) {
    if (width > maxWidth) {
      height = Math.round((height * maxWidth) / width);
      width = maxWidth;
    }
  } else {
    if (height > maxHeight) {
      width = Math.round((width * maxHeight) / height);
      height = maxHeight;
    }
  }
  return [width, height];
}

We will also create a readableBytes function with the snippet below:


function readableBytes(bytes) {
  const i = Math.floor(Math.log(bytes) / Math.log(1024)),
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

  return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];
} 

We can now update the img.onload callback similar to the snippet shown below:


const MAX_WIDTH = 320;
const MAX_HEIGHT = 180;

const INPUT = document.getElementById("imgId");


INPUT.onchange = function (event) {
  const file = event.target.files[0]; // get the file
  const blobURL = URL.createObjectURL(file);
  const img = new Image();
  img.src = blobURL;
  
  img.onload = function () {
    const [newWidth, newHeight] = calculateSize(img, MAX_WIDTH, MAX_HEIGHT);
    const canvas = document.createElement("canvas");
    canvas.width = newWidth;
    canvas.height = newHeight;
    const ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, newWidth, newHeight);
    canvas.toBlob(
      (blob) => {
        // Handle the compressed image.
        const displayTag = document.createElement('h1');
        displayTag.innerText = `Original Image - ${readableBytes(file.size)} :::::: Compressed Image - ${readableBytes(blob.size)}`;
         document.getElementById('container').append(displayTag);
      },
    );
  };
};

function calculateSize(img, maxWidth, maxHeight) {
  let width = img.width;
  let height = img.height;

  // calculate the width and height, constraining the proportions
  if (width > height) {
    if (width > maxWidth) {
      height = Math.round((height * maxWidth) / width);
      width = maxWidth;
    }
  } else {
    if (height > maxHeight) {
      width = Math.round((width * maxHeight) / height);
      height = maxHeight;
    }
  }
  return [width, height];
}

function readableBytes(bytes) {
  const i = Math.floor(Math.log(bytes) / Math.log(1024)),
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

  return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];
}

Now, let’s head over to our browser to test the application. If everything is done right, we should have something similar to the screen below:

As shown above, we have the original image size on the left-hand side while the compressed image size is on the right-hand side. We have successfully compressed the image using the Canvas approach. You can test the same direction using this codepen link.

Image compression using browser-image-compression JS library

Browser-image-compression is a JS module used for image compression in web browsers. They aid in compressing jpeg and png images by reducing their resolution or file size before uploading them, which results in lesser bandwidth consumption.

To create a new project, we use the npx create-react-app command to scaffold a new project in a directory of our choice.

To install the dependencies, we will be using the command below:

cd <project name> 
npm install browser-image-compression

We'll get a message with instructions for browsing our site and running it locally once the app has been created and the dependencies have been installed. This is done using the command.

npm start

React.js will start a hot-reloading development environment accessible by default at http://localhost:3000.

To get started, we will update App.js with the snippet below:

//...
import "./styles.css";

export default function App() {

  return (
    <div>
      <h1>Image compression with "browser-image-compression" dependency</h1>
      <div className="container">
        <input type="file" />
      </div>
      <br />
      <button>Compress</button>
      <div className="container">
        <h3>Original Image Size: </h3>
        <h3>Compressed Image size: </h3>
      </div>
    </div>
  );
}

We should have something similar to what is shown below:

Let us now implement the image compression process using the browser-image-compression.

Using the snippet below, we will create different state variables and functions to handle image compression and file upload.


import React, { useState } from "react";
import imageCompression from "browser-image-compression";
import "./styles.css";

export default function App() {

  const [uploaded, setUploaded] = useState(0);
  const [originalImage, setOriginalImage] = useState("");
  const [compressedImage, setCompressedImage] = useState("");

  // File Upload
  const handleFileUpload = (e) => {
    setOriginalImage(e.target.files[0]);
    setUploaded(1);
  };

  // File Compression
  const handleCompression = (e) => {
    if (uploaded) {
      const options = {
        maxSizeMB: 2,
        maxWidthOrHeight: 500,
        useWebWorker: true
      };
      imageCompression(originalImage, options).then((img) => {
        setCompressedImage(img);
      });
    }
  };
  return (
    <div>
      {...}    
    </div>
  );
}

In the snippet above, we utilize the browser-image-compression library to handle the compression and update our compressedImage variable with the result. At the same time, we also set originalImage using the handleFileUpload method.

Let us update the App.js component with the snippet below:


import React, { useState } from "react";
import imageCompression from "browser-image-compression";
import "./styles.css";

export default function App() {
  
    //...

  return (
    <div>
      <h1>Image compression with "browser-image-compression" dependency</h1>
      <div className="container">
        <input type="file" onChange={handleFileUpload} />
      </div>
      <br />
      <button onClick={handleCompression}>Compress</button>
      <div className="container">
        <h3>Original Image Size: {originalImage.size}</h3>
        <h3>Compressed Image size: {compressedImage.size}</h3>
      </div>
    </div>
  );
}

We called the handleFileUpload method in the onChange attribute of the input tag and the handleCompression method in the onClick attribute of the Compress button in the snippet above.

With all these settings in place, we can now test our application, and we should have something similar to what we have below.

We can also try out the demo via this code sandbox link.

We compressed the image using the Canvas method and the JavaScript library in the previous steps; however, using this strategy, the image was compressed at the expense of image quality, effect, and other criteria.

To compress our image flawlessly, we'll use ImageKit compression with its URL-based image transformations.

Image compression made easier with ImageKit

ImageKit is a cloud-based image compression and optimization tool. It can perform 50+ real-time image transformations that will help deliver a superior user experience to end users on the web and mobile apps.

Also, ImageKit’s image compression is not browser or canvas based but instead based on a no-code cloud-hosted solution far superior in performance and capabilities than browser-based image compression tools.

In other words, non-technical users like designers and marketers can transform images easily without writing a single line of code.

Further, ImageKit can perform lossy compression compared to Canvas API without image resizing and automatic file format conversions. In other words, ImageKit is a powerful image transformation tool that does not require prior technical knowledge or new learning.

Let’s understand how image transformations in ImageKit work with the help of an image. In the following sections, you will see how the original image can be transformed in several ways.

The expression below shows how the transformation works using the ImageKit URL.

URL-endpoint           transformation   image path

┌──────────────────────┐┌────────┐┌───────┐

[https://ik.imagekit.io/](https://ik.imagekit.io/demo/tr:w-300,h-300/medium_cafe_B1iTdD0C.jpg)[dpkl6lhc7vk](https://ik.imagekit.io/dpkl6lhc7vk/tr:ar-4-3,w-400/h_7HHOax6CU.png)[/tr:w-300,h-300/](https://ik.imagekit.io/demo/tr:w-300,h-300/medium_cafe_B1iTdD0C.jpg)[h_7HHOax6CU.png](https://ik.imagekit.io/dpkl6lhc7vk/tr:ar-4-3,w-400/h_7HHOax6CU.png)

In the following sections, we will demonstrate how ImageKit's image transformation capabilities can be used for specific business use cases using the below sample image.

https://ik.imagekit.io/dpkl6lhc7vk/girl_zsxRhmgBV.jpeg?updatedAt=1641684782272

URL-based resizing

One of the common transformations that developers require while using images on their website is the manipulation of the height and width of an image.

However, exchanging images between designers and end-users (like marketers) is extremely difficult. Most often, files get replaced resulting in the wrong file being worked upon. This probability for confusion and errors can be avoided with the help of URL-based resizing.

URL-based resizing allows you to change the size of the image by simply altering the height/width of the URL.

As shown below, we can scale the image width to any desired size by changing the URL's input. We will adjust the image's width to 200px from the URL in this example.

  • Resizing width to 200px

Some of the CMS platforms or social media media channels require the image to have a specific width. If your raw image is not of the stipulated width, you can resize it easily using ImageKit's URL-based resizing. No designer required. No coding required.

https://ik.imagekit.io/dpkl6lhc7vk/tr:w-200/girl_zsxRhmgBV.jpeg

Similarly, in addition to the width, the height of the image can also be altered. It i spossible to alter only the width or height alone depending on the requirement.

  • Resizing height to 200px

https://ik.imagekit.io/dpkl6lhc7vk/tr:h-200/girl_zsxRhmgBV.jpeg

  • Resizing the image using the Aspect ratio

In addition to height and width, you can also change the image's aspect ratio.  Aspect ratio of an image can be defined as the ratio of its width to its height, and is denoted like a mathematical ratio, two numberals separated by a coloon. For example: 4:3 is a popular aspect ratio for images.

This is typically useful when the image has to be resized across several social media platforms with varying image size requirements.

https://ik.imagekit.io/dpkl6lhc7vk/tr:ar-4-3,w-400/girl_zsxRhmgBV.jpeg

As illustrated in the steps above, we could adequately resize the image to our desired size. Check out ImageKit docs to know more aboutImageKit’s image transformation features.

Dynamic Resizing

Dynamic resizing refers to performing basix the image resizing as well as some additional transformations like cropping & preserving the aspect ration, adding a text overlay or watermark, adding a padding using a specific color, etc.

Let's see how to use URL parameters to resize and modify images to fit the website layout dynamically.

For adding a padding to our image with the color FF0000 without cropping the image, we add a simple URl parameter to insert a padding with a width of 400px and a height of 300px.

https://ik.imagekit.io/dpkl6lhc7vk/girl_zsxRhmgBV.jpeg?tr=w-400,h-300,cm-pad_resize,bg-FF0000

  • Adding watermark text - “Beach Time”

There could be times when you want to convey textual information on top of the image to educate the user or to provide context. Alternatively, you may want to add a watermark text to protect the asset from unauthorized usage. This could be a logo, your business name, or even generic text like 'copyright protected' or 'confidential'.

You can dynamically put text overlay on images to display any text on the image to achieve the above-mentioned purposes.

https://ik.imagekit.io/dpkl6lhc7vk/girl_zsxRhmgBV.jpeg?tr=l-text,i-Beach Time,pa-10,fs-50,co-FF0000,lx-N0,ly-30,l-end

Image Quality control

The perceived visual quality of an image is referred to as image quality. ImageKit rates image quality on a scale of one to ten, with ten being the highest and one being the worst. This section will show how we can easily control image quality.
There are two methods for controlling image quality: utilizing the URL parameter and using Image Quality from the dashboard.

  • Image quality control using the URL parameter

There are some default quality settings in ImageKit which you mightant to change change to better suit your needs.

Using the q parameter to override the default quality setting specified within the ImageKit dashboard.

https://ik.imagekit.io/dpkl6lhc7vk/girl_zsxRhmgBV.jpeg?tr=q-70

These are some use cases of image compression/optimization in ImageKit. There are still more variants that you may try to suit your purpose.

What makes ImageKit ideal for image optimization?

ImageKit is a one-stop facility for all your image processing requirements. As we've seen so far in this tutorial, there are several ways to compress an image, including utilizing the canvas method and the JavaScript library, both of which are inefficient and inconvenient compared to the ImageKit transformation approach. This section will go through some of the other cool features we get out of the box when using the ImageKIt transformation approach.

  • Automatic Image compression
  • A ready-to-use media storage system
  • Real-time image transformation (URL-based scaling and cropping)

Bringing it all together

While JavaScript is fast, flexible, and simple,  it requires some technical expertise to perform the transformation. If your business deals with many images regularly, you will have to deal with image compression from time to time. However, most free online image compression tools do not meet your requirements. When compressing images without losing quality, an image CDN like ImageKit.io is necessary.

Want to see how ImageKit works?

Sign up for a free trial. It is free until you are ready to upgrade.