Skip to content

Commit

Permalink
WMF\Reader : Save as WMF (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 authored Sep 11, 2024
1 parent 786f81a commit 5817314
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 15 deletions.
14 changes: 14 additions & 0 deletions docs/changes/0.1.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 0.1.3

## Enhancements

- WMF\Reader : Save as WMF in [#7](https://github.com/PHPOffice/WMF/pull/7) by [@Progi1984](https://github/Progi1984)

## Bug fixes

- N/A

## Miscellaneous

- N/A

3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ nav:
- WMF: 'usage/wmf.md'
- Credits: 'credits.md'
- Releases:
- '0.1.2 (WIP)': 'changes/0.1.2.md'
- '0.1.3 (WIP)': 'changes/0.1.3.md'
- '0.1.2': 'changes/0.1.2.md'
- '0.1.1': 'changes/0.1.1.md'
- '0.1.0': 'changes/0.1.0.md'
- Developers:
Expand Down
6 changes: 2 additions & 4 deletions src/WMF/Reader/WMF/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class GD extends ReaderAbstract
* @var GdImage|resource|false
*/
protected $gd;
/**
* @var string
*/
protected $content;
/**
* @var int
*/
Expand Down Expand Up @@ -375,6 +371,8 @@ public function save(string $filename, string $format): bool
return imagewebp($this->getResource(), $filename);
case 'wbmp':
return imagewbmp($this->getResource(), $filename);
case 'wmf':
return (bool) (file_put_contents($filename, $this->content) > 0);
default:
if ($this->hasExceptionsEnabled()) {
throw new WMFException(sprintf('Format %s not supported', $format));
Expand Down
14 changes: 10 additions & 4 deletions src/WMF/Reader/WMF/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ class Imagick extends ReaderAbstract

public function load(string $filename): bool
{
return $this->loadContent($filename, false);
$this->content = file_get_contents($filename);

return $this->loadContent();
}

public function loadFromString(string $content): bool
{
return $this->loadContent($content, true);
$this->content = $content;

return $this->loadContent();
}

private function loadContent(string $content, bool $isBlob): bool
protected function loadContent(): bool
{
try {
$this->im = new ImagickBase();

return $isBlob ? $this->im->readImageBlob($content) : $this->im->readImage($content);
return $this->im->readImageBlob($this->content);
} catch (ImagickException $e) {
$this->im->clear();

Expand Down Expand Up @@ -64,6 +68,8 @@ public function save(string $filename, string $format): bool
$this->getResource()->setImageFormat(strtolower($format));

return $this->getResource()->writeImage($filename);
case 'wmf':
return (bool) (file_put_contents($filename, $this->content) > 0);
default:
if ($this->hasExceptionsEnabled()) {
throw new WMFException(sprintf('Format %s not supported', $format));
Expand Down
4 changes: 4 additions & 0 deletions src/WMF/Reader/WMF/ReaderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ abstract class ReaderAbstract implements ReaderInterface
* @var bool
*/
protected $hasExceptionsEnabled = true;
/**
* @var string
*/
protected $content;

/**
* Enable/Disable throwing exceptions
Expand Down
16 changes: 14 additions & 2 deletions tests/WMF/Reader/AbstractTestReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function dataProviderFilesWMFNotImplemented(): array
/**
* @return array<array<string>>
*/
public static function dataProviderMediaType(): array
public static function dataProviderMediaTypeWMF(): array
{
return [
[
Expand All @@ -86,12 +86,24 @@ public static function dataProviderMediaType(): array
'wbmp',
'image/vnd.wap.wbmp',
],
[
'wmf',
'image/x-wmf',
],
];
}

public function assertMimeType(string $filename, string $expectedMimeType): void
{
// Use GD
$gdInfo = getimagesize($filename);
$this->assertEquals($expectedMimeType, $gdInfo['mime']);
if (is_array($gdInfo)) {
$this->assertEquals($expectedMimeType, $gdInfo['mime']);

return;
}

// Use Imagick
$this->assertEquals($expectedMimeType, (new Imagick($filename))->getImageMimeType());
}
}
2 changes: 1 addition & 1 deletion tests/WMF/Reader/WMF/GDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testOutput(string $file): void
}

/**
* @dataProvider dataProviderMediaType
* @dataProvider dataProviderMediaTypeWMF
*/
public function testSave(string $extension, string $mediatype): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/WMF/Reader/WMF/ImagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testOutput(string $file): void
}

/**
* @dataProvider dataProviderMediaType
* @dataProvider dataProviderMediaTypeWMF
*/
public function testSave(string $extension, string $mediatype): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/WMF/Reader/WMF/MagicGDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testOutput(string $file): void
}

/**
* @dataProvider dataProviderMediaType
* @dataProvider dataProviderMediaTypeWMF
*/
public function testSave(string $extension, string $mediatype): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/WMF/Reader/WMF/MagicImagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testOutput(string $file): void
}

/**
* @dataProvider dataProviderMediaType
* @dataProvider dataProviderMediaTypeWMF
*/
public function testSave(string $extension, string $mediatype): void
{
Expand Down

0 comments on commit 5817314

Please sign in to comment.