From cd0396c208ce7740c29b2cb2671fe745d872eb41 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 10 Sep 2024 09:25:37 +0200 Subject: [PATCH] Load a WMF file from a string (#5) --- docs/changes/0.1.1.md | 14 ++++++++++++++ mkdocs.yml | 3 ++- src/WMF/Reader/ReaderInterface.php | 2 ++ src/WMF/Reader/WMF/GD.php | 18 +++++++++++++++--- src/WMF/Reader/WMF/Imagick.php | 12 +++++++++++- src/WMF/Reader/WMF/Magic.php | 5 +++++ tests/WMF/Reader/WMF/GDTest.php | 9 +++++++++ tests/WMF/Reader/WMF/ImagickTest.php | 9 +++++++++ tests/WMF/Reader/WMF/MagicTest.php | 9 +++++++++ 9 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 docs/changes/0.1.1.md diff --git a/docs/changes/0.1.1.md b/docs/changes/0.1.1.md new file mode 100644 index 0000000..81a59f7 --- /dev/null +++ b/docs/changes/0.1.1.md @@ -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 + \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 03afe4c..6cec629 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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' diff --git a/src/WMF/Reader/ReaderInterface.php b/src/WMF/Reader/ReaderInterface.php index 3c07bfd..e4381f6 100644 --- a/src/WMF/Reader/ReaderInterface.php +++ b/src/WMF/Reader/ReaderInterface.php @@ -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; diff --git a/src/WMF/Reader/WMF/GD.php b/src/WMF/Reader/WMF/GD.php index d224f86..197eda0 100644 --- a/src/WMF/Reader/WMF/GD.php +++ b/src/WMF/Reader/WMF/GD.php @@ -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; diff --git a/src/WMF/Reader/WMF/Imagick.php b/src/WMF/Reader/WMF/Imagick.php index 1510d2b..0fdc113 100644 --- a/src/WMF/Reader/WMF/Imagick.php +++ b/src/WMF/Reader/WMF/Imagick.php @@ -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(); diff --git a/src/WMF/Reader/WMF/Magic.php b/src/WMF/Reader/WMF/Magic.php index bf77c04..6060b0c 100644 --- a/src/WMF/Reader/WMF/Magic.php +++ b/src/WMF/Reader/WMF/Magic.php @@ -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); diff --git a/tests/WMF/Reader/WMF/GDTest.php b/tests/WMF/Reader/WMF/GDTest.php index c834c50..ec44952 100644 --- a/tests/WMF/Reader/WMF/GDTest.php +++ b/tests/WMF/Reader/WMF/GDTest.php @@ -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 */ diff --git a/tests/WMF/Reader/WMF/ImagickTest.php b/tests/WMF/Reader/WMF/ImagickTest.php index 2fbb1eb..1bbdd40 100644 --- a/tests/WMF/Reader/WMF/ImagickTest.php +++ b/tests/WMF/Reader/WMF/ImagickTest.php @@ -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 */ diff --git a/tests/WMF/Reader/WMF/MagicTest.php b/tests/WMF/Reader/WMF/MagicTest.php index 5bfa59b..1c6bdb4 100644 --- a/tests/WMF/Reader/WMF/MagicTest.php +++ b/tests/WMF/Reader/WMF/MagicTest.php @@ -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 */