Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load a WMF file from a string #5

Merged
merged 1 commit into from
Sep 10, 2024
Merged
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
14 changes: 14 additions & 0 deletions docs/changes/0.1.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 0.1.1

## Enhancements

- Load a WMF file from a string in [#5](https://github.com/PHPOffice/WMF/pull/5) 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.0 (WIP)': 'changes/0.1.0.md'
- '0.1.1 (WIP)': 'changes/0.1.1.md'
- '0.1.0': 'changes/0.1.0.md'
- Developers:
- 'Coveralls': 'https://coveralls.io/github/PHPOffice/WMF'
- 'Code Coverage': 'coverage/index.html'
Expand Down
2 changes: 2 additions & 0 deletions src/WMF/Reader/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface ReaderInterface
{
public function load(string $filename): bool;

public function loadFromString(string $content): bool;

public function save(string $filename, string $format): bool;

public function getMediaType(): string;
Expand Down
18 changes: 15 additions & 3 deletions src/WMF/Reader/WMF/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,25 @@ public function isWMF(string $filename): bool
return $key == (int) 0x9AC6CDD7;
}

/**
* @see https://github.com/affinitybridge/mpdf/blob/master/src/Image/Wmf.php
*/
public function load(string $filename): bool
{
$this->content = file_get_contents($filename);

return $this->loadContent();
}

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

return $this->loadContent();
}

/**
* @see https://github.com/affinitybridge/mpdf/blob/master/src/Image/Wmf.php
*/
private function loadContent(): bool
{
$this->pos = 0;
$this->gdiObjects = [];
$k = 72 / 25.4;
Expand Down
12 changes: 11 additions & 1 deletion src/WMF/Reader/WMF/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ class Imagick extends ReaderAbstract
protected $im;

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

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

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

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

Expand Down
5 changes: 5 additions & 0 deletions src/WMF/Reader/WMF/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function load(string $filename): bool
return $this->reader->load($filename);
}

public function loadFromString(string $content): bool
{
return $this->reader->loadFromString($content);
}

public function save(string $filename, string $format): bool
{
return $this->reader->save($filename, $format);
Expand Down
9 changes: 9 additions & 0 deletions tests/WMF/Reader/WMF/GDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function testLoad(string $file): void
$this->assertTrue($reader->load($this->getResourceDir() . $file));
}

/**
* @dataProvider dataProviderFilesWMF
*/
public function testLoadFromString(string $file): void
{
$reader = new GD();
$this->assertTrue($reader->loadFromString(file_get_contents($this->getResourceDir() . $file)));
}

/**
* @dataProvider dataProviderFilesWMF
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/WMF/Reader/WMF/ImagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function testLoad(string $file): void
$this->assertTrue($reader->load($this->getResourceDir() . $file));
}

/**
* @dataProvider dataProviderFilesWMF
*/
public function testLoadFromString(string $file): void
{
$reader = new ImagickReader();
$this->assertTrue($reader->loadFromString(file_get_contents($this->getResourceDir() . $file)));
}

/**
* @dataProvider dataProviderFilesWMF
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/WMF/Reader/WMF/MagicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public function testLoad(string $file): void
$this->assertTrue($reader->load($this->getResourceDir() . $file));
}

/**
* @dataProvider dataProviderFilesWMF
*/
public function testLoadFromString(string $file): void
{
$reader = new Magic();
$this->assertTrue($reader->loadFromString(file_get_contents($this->getResourceDir() . $file)));
}

/**
* @dataProvider dataProviderFilesWMF
*/
Expand Down
Loading