diff --git a/system/Images/Handlers/BaseHandler.php b/system/Images/Handlers/BaseHandler.php index 9301b1573c23..2687fa29225c 100644 --- a/system/Images/Handlers/BaseHandler.php +++ b/system/Images/Handlers/BaseHandler.php @@ -720,29 +720,38 @@ public function __call(string $name, array $args = []) */ protected function reproportion() { - if (($this->width === 0 && $this->height === 0) || $this->image()->origWidth === 0 || $this->image()->origHeight === 0 || (! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height)) || ! ctype_digit((string) $this->image()->origWidth) || ! ctype_digit((string) $this->image()->origHeight)) { + $image = $this->image(); + $origW = $image->origWidth; + $origH = $image->origHeight; + $w = $this->width; + $h = $this->height; + + if ($origW === 0 || $origW === 0.0 || $origH === 0 || $origH === 0.0) { + return; + } + + if (($w === 0 || $w === 0.0) && ($h === 0 || $h === 0.0)) { return; } - // Sanitize - $this->width = (int) $this->width; - $this->height = (int) $this->height; + $w = (int) $w; + $h = (int) $h; + $origW = (int) $origW; + $origH = (int) $origH; + + $this->width = $w; + $this->height = $h; if ($this->masterDim !== 'width' && $this->masterDim !== 'height') { - if ($this->width > 0 && $this->height > 0) { - $this->masterDim = ((($this->image()->origHeight / $this->image()->origWidth) - ($this->height / $this->width)) < 0) ? 'width' : 'height'; - } else { - $this->masterDim = ($this->height === 0) ? 'width' : 'height'; - } - } elseif (($this->masterDim === 'width' && $this->width === 0) || ($this->masterDim === 'height' && $this->height === 0) - ) { + $this->masterDim = ($h === 0 || ($w > 0 && (($origH / $origW) - ($h / $w) < 0))) ? 'width' : 'height'; + } elseif (($this->masterDim === 'width' && $w === 0) || ($this->masterDim === 'height' && $h === 0)) { return; } if ($this->masterDim === 'width') { - $this->height = (int) ceil($this->width * $this->image()->origHeight / $this->image()->origWidth); + $this->height = (int) ceil($w * $origH / $origW); } else { - $this->width = (int) ceil($this->image()->origWidth * $this->height / $this->image()->origHeight); + $this->width = (int) ceil($origW * $h / $origH); } } diff --git a/tests/system/Images/BaseHandlerTest.php b/tests/system/Images/BaseHandlerTest.php index 17bc3c00eb70..dc862f636904 100644 --- a/tests/system/Images/BaseHandlerTest.php +++ b/tests/system/Images/BaseHandlerTest.php @@ -130,4 +130,24 @@ public function testImageHandled(): void $handler->withFile($this->path); $this->assertSame($this->path, $handler->getPathname()); } + + public function testReproportionWithFloatZero(): void + { + $handler = Services::image('gd', null, false); + $handler->withFile($this->path); + + $image = $handler->getFile(); + $image->origWidth = 0.0; + $image->origHeight = 0.0; + + $expectedWidth = $handler->getWidth(); + $expectedHeight = $handler->getHeight(); + + $invoker = $this->getPrivateMethodInvoker($handler, 'reproportion'); + + $invoker(); + + $this->assertSame($expectedWidth, $handler->getWidth()); + $this->assertSame($expectedHeight, $handler->getHeight()); + } }