From 2340e939ed35dfe1a1b807519a11f109b6d90b57 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 26 Feb 2024 11:52:35 +0000
Subject: [PATCH] Issue #3292350 by O'Briat, KlemenDEV, hswong3i, smustgrave,
 quietone: Update the file size in file_validate_image_resolution after
 resizing

(cherry picked from commit 1a3d72bc678dea3f73945e155df3f0e60c00a3b7)
---
 core/modules/file/file.module                              | 2 ++
 core/modules/file/tests/src/Kernel/LegacyValidatorTest.php | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 7c862ed005d5..4f251e03471b 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -289,6 +289,8 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
         if ($image->scale($width, $height)) {
           $scaling = TRUE;
           $image->save();
+          // Update the file size now that the image has been resized.
+          $file->setSize($image->getFileSize());
           if (!empty($width) && !empty($height)) {
             $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
               [
diff --git a/core/modules/file/tests/src/Kernel/LegacyValidatorTest.php b/core/modules/file/tests/src/Kernel/LegacyValidatorTest.php
index 357dcb357eb4..20c27fe2d8b0 100644
--- a/core/modules/file/tests/src/Kernel/LegacyValidatorTest.php
+++ b/core/modules/file/tests/src/Kernel/LegacyValidatorTest.php
@@ -37,6 +37,7 @@ protected function setUp(): void {
     /** @var \Drupal\Core\File\FileSystemInterface $file_system */
     $file_system = \Drupal::service('file_system');
     $this->image->setFilename($file_system->basename($this->image->getFileUri()));
+    $this->image->setSize(@filesize($this->image->getFileUri()));
 
     $this->nonImage = File::create();
     $this->nonImage->setFileUri('core/assets/vendor/jquery/jquery.min.js');
@@ -191,6 +192,8 @@ public function testFileValidateImageResolution() {
       // Verify that the image was scaled to the correct width and height.
       $this->assertLessThanOrEqual(10, $image->getWidth());
       $this->assertLessThanOrEqual(5, $image->getHeight());
+      // Verify that the file size has been updated after resizing.
+      $this->assertEquals($this->image->getSize(), $image->getFileSize());
 
       // Once again, now with negative width and height to force an error.
       copy('core/misc/druplicon.png', 'temporary://druplicon.png');
-- 
GitLab