Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Database/Attach/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions tests/Database/Attach/ResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down