From 17a9d0cb723a23c88ce0b25395ab4376108bfe8b Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 4 Dec 2020 15:52:27 -0500 Subject: [PATCH 1/3] take the extension options extension attribute into account --- 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 e5df438e4..aff200ed3 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->getOption('extension') ?: (pathinfo($savePath, PATHINFO_EXTENSION) ?: $this->extension); // Create and save an image based on it's extension switch (strtolower($extension)) { From 1f877b0adac373e61bcebe86e4795f1b89980728 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 4 Dec 2020 17:21:08 -0500 Subject: [PATCH 2/3] add getExtension() method and a testcase to test its behavior --- src/Database/Attach/Resizer.php | 7 ++++++- tests/Database/Attach/ResizerTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Database/Attach/Resizer.php b/src/Database/Attach/Resizer.php index aff200ed3..3fa96f562 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 = $this->getOption('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,9 @@ protected function getSizeByFit($maxWidth, $maxHeight) return [$optimalWidth, $optimalHeight]; } + + 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 960d743bb..3de9e4131 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -354,6 +354,25 @@ public function testCrop10x15() $this->assertImageSameAsFixture(__METHOD__); } + public function testGetExtension() + { + $this->setSource(self::SRC_SQUARE); // jpeg 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('jpeg', $extension); + + // no extension provided in options, extension provided in path, should return path extension + $extension = $this->callProtectedMethod($this->resizer, 'getExtension', ['dummy.gif']); + $this->assertEquals('gif', $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.gif']); + $this->assertEquals('png', $extension); + } + /** * Set the source path and set the extension to match. * @param string $source Path to the source image for the Resizer From ba1b865102166e2db4919459c3f50578b36b010f Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 4 Dec 2020 17:27:55 -0500 Subject: [PATCH 3/3] use gif for test file as there is no jpeg/jpg ambiguity --- tests/Database/Attach/ResizerTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Database/Attach/ResizerTest.php b/tests/Database/Attach/ResizerTest.php index 3de9e4131..0e54c6ffb 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -356,20 +356,20 @@ public function testCrop10x15() public function testGetExtension() { - $this->setSource(self::SRC_SQUARE); // jpeg extension + $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('jpeg', $extension); + $this->assertEquals('gif', $extension); // no extension provided in options, extension provided in path, should return path extension - $extension = $this->callProtectedMethod($this->resizer, 'getExtension', ['dummy.gif']); - $this->assertEquals('gif', $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.gif']); + $extension = $this->callProtectedMethod($this->resizer, 'getExtension', ['dummy.jpg']); $this->assertEquals('png', $extension); }