What is the reason behind the significant 80% reduction in PNG files by grunt-contrib-imagemin compared to the minimal reduction of less than 0.1%

Just getting started with Grunt here.

Recently, I've been experimenting with grunt-contrib-imagemin.

When it comes to compressing PNG files, it does an impressive job. It typically reduces the size by around 80%.

However, I'm finding that the JPEG compression is not as effective. It only shaves off a few KB from a 3mb picture.

Any tips on improving jpeg compression with Grunt?

Gruntfile.js

module.exports = function(grunt) {
  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      build: {
        src: 'src/*.js',
        dest: 'build/main.min.js'
      }
    },
      imagemin: {
        png: {
          options: {
            optimizationLevel: 7
          },
          files: [
            {
              expand: true,
              cwd: 'src/img/png/', // cwd is 'current working directory'
              src: ['**/*.png'],
              dest: 'build/img/png', // Could also match cwd.
              ext: '.png'
            }
          ]
        },
        jpg: {
          options: {
            progressive: true
          },
          files: [
            {
              expand: true, // Tell Grunt where to find our images and where to export them to.
              cwd: 'src/img/', // cwd is 'current working directory'
              src: ['**/*.jpg'],
              dest: 'build/img', // Could also match cwd.
              ext: '.jpg'
            }
          ]
        }
      }
  });

  // Load the plugins
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-imagemin');
  // Default tasks
  grunt.registerTask('default', ['uglify', 'imagemin']);



};

CLI

C:\Users\bmildren\Desktop\Sites\grunt-test>grunt
Running "uglify:build" (uglify) task
>> 1 file created.

Running "imagemin:png" (imagemin) task
? src/img/png/e4454c8df9.png (saved 1.37 MB)
Minified 1 image (saved 1.37 MB)

Running "imagemin:jpg" (imagemin) task
? src/img/5f468e98.jpg (saved 3.24 kB)
? src/img/photo-1414604582943-2fd913b3cb17.jpg (saved 3.24 kB)
? src/img/photo-1416424500327-a57ace7358b8.jpg (saved 3.24 kB)
? src/img/photo-1416838375725-e834a83f62b7.jpg (saved 3.24 kB)
? src/img/photo-1419332563740-42322047ff09.jpg (saved 3.24 kB)
Minified 5 images (saved 16.20 kB)

Done, without errors.

C:\Users\bmildren\Desktop\Sites\grunt-test>

Answer №1

When it comes to optimizing jpg files, grunt-contrib-imagemin relies on the use of jpegtran.

The specific options utilized by this tool include -copy none -optimize, which not only removes meta-data from the file but also enhances the Huffman table through a lossless process (more details can be found at this link).

It's important to note that jpegtran does not carry out any lossy operations like adjusting image quality. Therefore, if you're looking to further optimize jpg files beyond what grunt-contrib-imagemin offers, you'll need to explore alternative solutions.

On the other hand, when dealing with png files, optipng is employed to implement various algorithms that are specifically tailored for this file type, resulting in more efficient optimization while maintaining losslessness.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding an active class to a large image when a thumbnail image is clicked can enhance user experience and

I've created a photo gallery using jquery. Currently, when I click on "next", the image changes based on the index. However, I also want to be able to click on the thumbnails (small images) and display the larger image according to that specific inde ...

Discover the way to retrieve PHP SESSION variable within JavaScript

I'm currently working on developing a platform for image uploads. As part of this project, I assign a unique identifier to each user upon login using $_SESSION['id'] in PHP. Now, I am looking for a way to verify if the $_SESSION['id&apo ...

The name 'withStyles' is nowhere to be found

import * as React from "react"; import Button from "@material-ui/core/Button"; import * as PropTypes from "prop-types"; import {WithStyles} from '@material-ui/core'; import "./App.css"; import PageTwo from "./components/PageTwo"; ...

Manipulating JSON with ng-model in AngularJS

Let's say I have a simple JSON similar to this: { "Object 0": {} } I am trying to display it as a tree structure. This is the approach I am taking: <span>{{key}}</span> // Object 0 <span>{{value}}</span> // {} <ul> ...

Sinon - observing the constructor function

While I've come across a few related inquiries, none seem to address what I am specifically looking to achieve. My goal is to monitor a constructor method in such a way that when an object created with the constructor calls this method from a differe ...

Using JavaScript to replace a radio button with the term "selected"

I am currently in the process of developing a quiz that is powered by jQuery and possibly JSON, with data being stored in a database. Everything is functioning correctly at this point, but I would like to enhance the user interface by hiding the radio butt ...

Issue encountered with Cordova in Visual Studio 2015: Error code bld00401

Having some trouble getting Cordova to work with Visual Studio 2015. Every time I try to open a new project and run the standard template, it fails to build and shows the following errors: Error 1: BLD401 Error: BLD00401: Module C:\Users\Rami K ...

Vue.Js for a Single Page Application utilizing Two Data Sources

Currently, I am working on developing a Single Page Application using vue.js. My project consists of 2 bundles of pages stored in separate S3 buckets - one public and one private. The public bundle is meant to be accessible to all users, while the private ...

JavaScript, XML, and PHP all support the use of HTML entities within their code

Having some trouble as a newbie again))) Can you please help me out, guys? I'm having an XML file with the following data: <Page> <Content>&lt;p&gt;Article content&lt;/p&gt;&#13; &#13; &lt;h1 style="font-style ...

JSON nested error: Cannot read property 'length' of undefined

Having some trouble working with a nested array within a JSON in D3JS, specifically encountering a "property length undefined" error at the line: .attr("d", function(d) { return line(d.points); }). Below is the JSON data structure: [ { "aspectRatio" ...

The Angular2 Observable fails to be activated by the async pipe

Take a look at this simple code snippet using angular2/rxjs/typescript public rooms: Observable<Room[]>; constructor ( ... ) { this.rooms = this.inspectShipSubject .do(() => console.log('foo')) .switchMap(shi ...

Guide to hosting Word, Excel, PowerPoint, and PDF files on a Node.js server

I recently managed to transfer a file from my MongoDB database to my node.js server Below is a screenshot for reference: View the Screenshot here However, I am unsure of how to serve these files from the node.js server to the client side. Any help in a ...

Even after reinstalling Node, I am still facing issues with npm not functioning properly

C:\Users\Sahil Mandoliya> npm --version Program 'npm.cmd' encountered an error: The specified file could not be found on the systemAt line:1 char:1 + npm --version + ~~~~~~~~~~~~~. At line:1 char:1 + npm --version + ~~~~~~~~~~~~~ ...

Is there a way to align the image next to the form and ensure they are both the same size?

I've been struggling to resize the image to match the form size, but I can't seem to get it right. Can anyone provide me with some guidance on this issue? I have obtained this contact form from a website that I plan to modify slightly, but I need ...

Encountering a problem with `npm install` in Github Actions

One of the tasks in my workflow is to install npm packages using the command npm install, but unfortunately I am encountering an error that I cannot seem to understand. The specific error message is displayed below, and I am at a loss on how to resolve it. ...

Exploring Javascript bugs in Visual Studio (or any other JS debugger)

I am currently working with a .js file that is executed using cscript.exe and not in a browser environment. I am aware that I can use the //X parameter with cscript.exe to trigger a debugger selection prompt. This works well when choosing "Visual Studio 2 ...

Jquery is failing to handle multiple inputs

I am currently working on a system that can handle multiple Yes/No questions. However, every time I try to submit the form, I encounter an error in the console that I cannot identify, and no data is returned from the query. Previously, I used an Array to s ...

Error message "Exceeded the maximum call stack size" encountered during Vue-router authentication

I am currently in the process of developing a dashboard using Vue, Vuex, and Quasar. However, I have encountered an authentication error that is preventing me from progressing. Additionally, I need assistance in ensuring that when a user is already logged ...

Here is a way to return a 400 response in `express.js` when the JSON request body is invalid

How can I make my application send a response with status code 400 instead of throwing an error if the request body contains invalid JSON? import express from 'express' app.use(express.urlencoded({ extended: false })) app.use(express.json()) ...

Is there a way to load an image asynchronously when the page loads and show a loading gif during the loading process?

My image tag displays a dynamically generated graph from the database. The loading time can vary significantly, sometimes only taking a second while other times it may take up to 6 or 7 seconds for the graph image to appear. I am looking for a way to sho ...