diff --git a/src/Database/Attach/Resizer.php b/src/Database/Attach/Resizer.php index 777d4b972..d7c5bdfdc 100644 --- a/src/Database/Attach/Resizer.php +++ b/src/Database/Attach/Resizer.php @@ -451,7 +451,7 @@ public function save($savePath) } // Determine the image type from the destination file - $extension = pathinfo($savePath, PATHINFO_EXTENSION) ?: $this->extension; + $extension = $this->getExtension($savePath); // Create and save an image based on it's extension switch (strtolower($extension)) { @@ -687,4 +687,12 @@ protected function getSizeByFit($maxWidth, $maxHeight) return [$optimalWidth, $optimalHeight]; } + + /** + * Get the extension for the provided filename + */ + protected function getExtension(string $path): string + { + return $this->getOption('extension') ?: (pathinfo($path, PATHINFO_EXTENSION) ?: $this->extension); + } } diff --git a/tests/Database/Attach/ResizerTest.php b/tests/Database/Attach/ResizerTest.php index b2a14d95c..d7792939d 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -72,6 +72,29 @@ public function testReset() $this->assertImageSameAsFixture(self::COMMON_FIXTURES['reset']); } + /** + * Given a Resizer with any image + * Test to see if the getExtension() method properly returns the filename extension for the provided filename + */ + public function testGetExtension() + { + $this->setSource(self::SRC_PORTRAIT); // gif extension + $this->createFixtureResizer(); + + // no extension provided in path, no extension provided in options, should return source extension. + $extension = $this->callProtectedMethod($this->resizer, 'getExtension', ['dummy']); + $this->assertEquals('gif', $extension); + + // no extension provided in options, extension provided in path, should return path extension + $extension = $this->callProtectedMethod($this->resizer, 'getExtension', ['dummy.jpg']); + $this->assertEquals('jpg', $extension); + + // extension provided in options and in path, should return extension from options + $this->resizer->setOptions(['extension' => 'png']); + $extension = $this->callProtectedMethod($this->resizer, 'getExtension', ['dummy.jpg']); + $this->assertEquals('png', $extension); + } + /** * Given a Resizer with any image * When the resize method is called with 0x0