From 52d3da3e1c281edffd59eb1cdfeb4bb51b8c56be Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 4 Mar 2022 17:31:31 -0500 Subject: [PATCH 1/3] use the file extension if provided in the options --- src/Database/Attach/Resizer.php | 12 +++++++++++- tests/Database/Attach/ResizerTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Database/Attach/Resizer.php b/src/Database/Attach/Resizer.php index 777d4b972..c23e26b73 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,14 @@ protected function getSizeByFit($maxWidth, $maxHeight) return [$optimalWidth, $optimalHeight]; } + + /** + * Return the filename extension for the provided filename + * @param string $path The filename path + * @return string + */ + protected function getExtension($path) + { + 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..7cb6177e2 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -72,6 +72,30 @@ 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 + * @throws Exception + */ + 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 From 64b05c5ec1c77c672d789b06f08823cada0c53a8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 24 Mar 2022 11:33:32 -0400 Subject: [PATCH 2/3] add type hints --- src/Database/Attach/Resizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Attach/Resizer.php b/src/Database/Attach/Resizer.php index c23e26b73..46dc77244 100644 --- a/src/Database/Attach/Resizer.php +++ b/src/Database/Attach/Resizer.php @@ -693,7 +693,7 @@ protected function getSizeByFit($maxWidth, $maxHeight) * @param string $path The filename path * @return string */ - protected function getExtension($path) + protected function getExtension(string $path): string { return $this->getOption('extension') ?: (pathinfo($path, PATHINFO_EXTENSION) ?: $this->extension); } From ad49266009bac195a2764016e61087de95cd8f32 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 24 Mar 2022 13:40:57 -0400 Subject: [PATCH 3/3] fix docblocks --- src/Database/Attach/Resizer.php | 4 +--- tests/Database/Attach/ResizerTest.php | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Database/Attach/Resizer.php b/src/Database/Attach/Resizer.php index 46dc77244..d7c5bdfdc 100644 --- a/src/Database/Attach/Resizer.php +++ b/src/Database/Attach/Resizer.php @@ -689,9 +689,7 @@ protected function getSizeByFit($maxWidth, $maxHeight) } /** - * Return the filename extension for the provided filename - * @param string $path The filename path - * @return string + * Get the extension for the provided filename */ protected function getExtension(string $path): string { diff --git a/tests/Database/Attach/ResizerTest.php b/tests/Database/Attach/ResizerTest.php index 7cb6177e2..d7792939d 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -75,7 +75,6 @@ public function testReset() /** * Given a Resizer with any image * Test to see if the getExtension() method properly returns the filename extension for the provided filename - * @throws Exception */ public function testGetExtension() {