From 83429e75cbabeff59616636061a995fc8c451603 Mon Sep 17 00:00:00 2001
From: mondrake <28163-mondrake@users.noreply.drupalcode.org>
Date: Thu, 4 Jan 2024 16:14:42 +0000
Subject: [PATCH] Update file image.admin.inc

---
 core/modules/image/image.admin.inc | 32 ++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc
index 9921e7b94b30..f28431c098ca 100644
--- a/core/modules/image/image.admin.inc
+++ b/core/modules/image/image.admin.inc
@@ -42,13 +42,19 @@ function template_preprocess_image_style_preview(&$variables) {
     'width' => $original_image->getWidth(),
     'height' => $original_image->getHeight(),
   ];
-  if ($variables['original']['width'] > $variables['original']['height']) {
-    $variables['preview']['original']['width'] = min($variables['original']['width'], $sample_width);
-    $variables['preview']['original']['height'] = round($variables['preview']['original']['width'] / $variables['original']['width'] * $variables['original']['height']);
+  if (empty($variables['original']['width']) || empty($variables['original']['height'])) {
+    $variables['preview']['original']['width'] = $sample_width;
+    $variables['preview']['original']['height'] = $sample_height;
   }
   else {
-    $variables['preview']['original']['height'] = min($variables['original']['height'], $sample_height);
-    $variables['preview']['original']['width'] = round($variables['preview']['original']['height'] / $variables['original']['height'] * $variables['original']['width']);
+    if ($variables['original']['width'] > $variables['original']['height']) {
+      $variables['preview']['original']['width'] = min($variables['original']['width'], $sample_width);
+      $variables['preview']['original']['height'] = round($variables['preview']['original']['width'] / $variables['original']['width'] * $variables['original']['height']);
+    }
+    else {
+      $variables['preview']['original']['height'] = min($variables['original']['height'], $sample_height);
+      $variables['preview']['original']['width'] = round($variables['preview']['original']['height'] / $variables['original']['height'] * $variables['original']['width']);
+    }
   }
 
   // Set up derivative file information.
@@ -63,13 +69,19 @@ function template_preprocess_image_style_preview(&$variables) {
     'width' => $preview_image->getWidth(),
     'height' => $preview_image->getHeight(),
   ];
-  if ($variables['derivative']['width'] > $variables['derivative']['height']) {
-    $variables['preview']['derivative']['width'] = min($variables['derivative']['width'], $sample_width);
-    $variables['preview']['derivative']['height'] = round($variables['preview']['derivative']['width'] / $variables['derivative']['width'] * $variables['derivative']['height']);
+  if (empty($variables['derivative']['width']) || empty($variables['derivative']['height'])) {
+    $variables['preview']['derivative']['width'] = $sample_width;
+    $variables['preview']['derivative']['height'] = $sample_height;
   }
   else {
-    $variables['preview']['derivative']['height'] = min($variables['derivative']['height'], $sample_height);
-    $variables['preview']['derivative']['width'] = round($variables['preview']['derivative']['height'] / $variables['derivative']['height'] * $variables['derivative']['width']);
+    if ($variables['derivative']['width'] > $variables['derivative']['height']) {
+      $variables['preview']['derivative']['width'] = min($variables['derivative']['width'], $sample_width);
+      $variables['preview']['derivative']['height'] = round($variables['preview']['derivative']['width'] / $variables['derivative']['width'] * $variables['derivative']['height']);
+    }
+    else {
+      $variables['preview']['derivative']['height'] = min($variables['derivative']['height'], $sample_height);
+      $variables['preview']['derivative']['width'] = round($variables['preview']['derivative']['height'] / $variables['derivative']['height'] * $variables['derivative']['width']);
+    }
   }
 
   // Build the preview of the original image.
-- 
GitLab

