Problem/Motivation

PNG type images are not compressed enough. Getting from an image's size of 2.12M to just 2.1M.
JPEG and JPG do not seem to have this problem.

Steps to reproduce

Upload a PNG image to your application and check the size of it. Wherever it has been stored.
The image with which I have this issue has been attached.
My application is using Drupal version 8.9.20 with PHP 7.3.33.

Proposed resolution

imagepng() to be the problem. After obtaining the image from imagecreatefrompng(), imagejpeg() can be used on that resource and still keep the image type (png) on the output.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Lordy_Nathan created an issue. See original summary.

Lordy_Nathan’s picture

Issue summary: View changes
Lordy_Nathan’s picture

Lordy_Nathan’s picture

Version: 2.0.0 » 2.0.x-dev
3li’s picture

Assigned: Lordy_Nathan » Unassigned
Status: Active » Needs review

Shall try and review this when available.

Lordy_Nathan’s picture

Issue summary: View changes
StatusFileSize
new2.12 MB
avpaderno’s picture

niknak’s picture

Hello,

Anybody are already review this patch ?

Thx

anoopsingh92 made their first commit to this issue’s fork.

anoopsingh92’s picture

Assigned: Unassigned » anoopsingh92
StatusFileSize
new15.98 KB
new70.59 KB
anoopsingh92’s picture

Assigned: anoopsingh92 » Unassigned

Hi, I am not able to access this configuration "/admin/config/user-interface/image_compression".
I am getting this error
error

I also checked in the DB log getting this error.
Patch #2

I am using Drupal 9.5.0-dev PHP version 7.4.29 DB 10.4.24-MariaDB

Thanks

avpaderno’s picture

Title: function compressPng dot not compress enough » ImageCompressionServices::compressPng() doesn't compress enough
Issue summary: View changes
avpaderno’s picture

Status: Needs review » Needs work

imagejpeg() creates a JPEG file from the given image. It cannot be used to save a PNG image.
Surely, a JPEG image would be more compressed, but that's because JPEG uses lossy compression, while PNG uses lossless compression.

-    switch ($info['mime']) {
-      case 'image/jpg':
-      case 'image/jpeg':
-        for ($i = 0; $i < count($file_size); $i++) {
-          $current_value = $file_size[$i];
-          if ($size > $current_value) {
-            $file_compressed = TRUE;
-            $this->compressJpg($source, $serial_data[$current_value]);
-          }
-        }
-        break;
 
-      case 'image/png':
+    for ($i = 0; $i < count($file_size); $i++) {
+      $current_value = $file_size[$i];
+      if ($size > $current_value) {
         $file_compressed = TRUE;
-        $this->compressPng($source, 9);
-        break;
+        if ($info['mime'] === 'image/jpeg') {
+          $this->compressJpg($source, $serial_data[$current_value]);
+        }
+        else {
+          $this->compressPng($source, $serial_data[$current_value]);
+        }
+      }

The quality for JPEG images ranges from 0 to 100, while the quality for PNG images ranges from 0 to 9. (-1 is a special value that means use the default value in both the cases.) While a quality of 100 for a JPEG image means getting a bigger file, a quality of 9 for a PNG images means a smaller file. (It's all relative, since the smaller PNG file could still be bigger than the bigger JPEG file.)
This means the code needs to handle JPEG and PNG images differently.