Skip to content

Commit 786f81a

Browse files
authored
WMF\Reader\WMF\Magic : Backends sorted by priority (#6)
1 parent cd0396c commit 786f81a

File tree

14 files changed

+459
-83
lines changed

14 files changed

+459
-83
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
composer.lock
55
phpDocumentor.phar
66

7+
build/
78
public/
89
vendor/

docs/changes/0.1.2.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 0.1.2
2+
3+
## Enhancements
4+
5+
- WMF\Reader\WMF\Magic : Backends sorted by priority in [#6](https://github.com/PHPOffice/WMF/pull/6) by [@Progi1984](https://github/Progi1984)
6+
7+
## Bug fixes
8+
9+
- N/A
10+
11+
## Miscellaneous
12+
13+
- N/A
14+

docs/usage/wmf.md

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ You can load `.wmf` files.
77
You can one of two current backends : `gd` or `imagick`.
88
If you don't know which one used, you can use the magic one.
99

10+
By default, the order of the backends is Imagick, followed by GD.
11+
Each backend is tested on different criteria: extension loaded, format support.
12+
1013
```php
1114
<?php
1215

@@ -22,7 +25,41 @@ $reader = new Magic();
2225
$reader->load('sample.wmf');
2326
```
2427

25-
For next sample, I will use the magic one.
28+
For next samples, I will use the magic one.
29+
30+
### `getBackends`
31+
32+
This specific method for `Magic::class` returns backends sorted by priority.
33+
34+
```php
35+
<?php
36+
37+
use PhpOffice\WMF\Reader\WMF\Magic;
38+
39+
$reader = new Magic();
40+
41+
var_dump($reader->getBackends());
42+
```
43+
44+
### `setBackends`
45+
46+
This specific method for `Magic::class` defines backends sorted by priority.
47+
48+
```php
49+
<?php
50+
51+
use PhpOffice\WMF\Reader\WMF\GD;
52+
use PhpOffice\WMF\Reader\WMF\Imagick;
53+
use PhpOffice\WMF\Reader\WMF\Magic;
54+
55+
$reader = new Magic();
56+
$reader->setBackends([
57+
GD::class,
58+
Imagick::class,
59+
]);
60+
61+
var_dump($reader->getBackends());
62+
```
2663

2764
## Methods
2865

@@ -39,15 +76,14 @@ The `Imagick` backend returns a `Imagick` object.
3976
use PhpOffice\WMF\Reader\WMF\Magic;
4077

4178
$reader = new Magic();
79+
$reader->load('sample.wmf');
4280

43-
$wmf = $reader->load('sample.wmf');
44-
45-
var_dump($wmf->getResource());
81+
var_dump($reader->getResource());
4682
```
4783

4884
### `getMediaType`
4985

50-
The method returns the media type for a WMF file
86+
The method returns the media type for a WMF file.
5187

5288
```php
5389
<?php
@@ -63,32 +99,47 @@ echo 'The media type for a WMF file is ' . $$mediaType;
6399

64100
### `isWMF`
65101

66-
The method allows to know if the file is supported by the library.
102+
The method returns if the file is supported by the library.
67103

68104
```php
69105
<?php
70106

71107
use PhpOffice\WMF\Reader\WMF\Magic;
72108

73109
$reader = new Magic();
110+
$reader->load('sample.wmf');
74111

75-
$isWMF = $reader->isWMF('sample.wmf');
112+
$isWMF = $reader->isWMF();
76113

77114
echo 'The file sample.wmf ' . ($isWMF ? 'is a WMF file' : 'is not a WMF file');
78115
```
79116

80117
### `load`
81118

82-
The method load a WMF file in the object
119+
The method loads a WMF file in the object.
120+
The method returns `true` if the file has been correctly loaded, or `false` if it has not.
83121

84122
```php
85123
<?php
86124

87125
use PhpOffice\WMF\Reader\WMF\Magic;
88126

89127
$reader = new Magic();
128+
$reader->load('sample.wmf');
129+
```
90130

91-
$wmf = $reader->load('sample.wmf');
131+
### `loadFromString`
132+
133+
The method loads a WMF file in the object from a string.
134+
The method returns `true` if the file has been correctly loaded, or `false` if it has not.
135+
136+
```php
137+
<?php
138+
139+
use PhpOffice\WMF\Reader\WMF\Magic;
140+
141+
$reader = new Magic();
142+
$reader->loadFromString(file_get_contents('sample.wmf'));
92143
```
93144

94145
### `save`
@@ -101,7 +152,6 @@ The method transforms the loaded WMF file in an another image.
101152
use PhpOffice\WMF\Reader\WMF\Magic;
102153

103154
$reader = new Magic();
104-
105-
$wmf = $reader->load('sample.wmf');
106-
$wmf->save('sample.png', 'png');
155+
$reader->load('sample.wmf');
156+
$reader->save('sample.png', 'png');
107157
```

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ nav:
4343
- WMF: 'usage/wmf.md'
4444
- Credits: 'credits.md'
4545
- Releases:
46-
- '0.1.1 (WIP)': 'changes/0.1.1.md'
46+
- '0.1.2 (WIP)': 'changes/0.1.2.md'
47+
- '0.1.1': 'changes/0.1.1.md'
4748
- '0.1.0': 'changes/0.1.0.md'
4849
- Developers:
4950
- 'Coveralls': 'https://coveralls.io/github/PHPOffice/WMF'

src/WMF/Reader/WMF/GD.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public function __destruct()
6565
}
6666
}
6767

68-
public function isWMF(string $filename): bool
68+
public function isWMF(): bool
6969
{
70-
list(, $key) = unpack('L', substr(file_get_contents($filename), 0, 4));
70+
list(, $key) = unpack('L', substr($this->content, 0, 4));
7171

7272
return $key == (int) 0x9AC6CDD7;
7373
}
@@ -91,6 +91,10 @@ public function loadFromString(string $content): bool
9191
*/
9292
private function loadContent(): bool
9393
{
94+
if (!$this->isWMF()) {
95+
return false;
96+
}
97+
9498
$this->pos = 0;
9599
$this->gdiObjects = [];
96100
$k = 72 / 25.4;

src/WMF/Reader/WMF/Imagick.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ private function loadContent(string $content, bool $isBlob): bool
4242
}
4343
}
4444

45-
public function isWMF(string $filename): bool
45+
public function isWMF(): bool
4646
{
47-
$im = new ImagickBase();
48-
$im->readImage($filename);
49-
50-
return $im->getImageFormat() === 'WMF';
47+
return $this->im->getImageFormat() === 'WMF';
5148
}
5249

5350
public function getResource(): ImagickBase

src/WMF/Reader/WMF/Magic.php

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,70 @@
1111
class Magic extends ReaderAbstract
1212
{
1313
/**
14-
* @var ReaderInterface
14+
* @var array<string>
15+
*/
16+
protected $backends = [
17+
ImagickReader::class,
18+
GD::class,
19+
];
20+
21+
/**
22+
* @var ?ReaderInterface
1523
*/
1624
protected $reader;
1725

18-
public function __construct()
26+
protected function getBackend(): ?ReaderInterface
1927
{
20-
$reader = null;
21-
if (extension_loaded('imagick') && in_array('WMF', ImagickBase::queryformats())) {
22-
$reader = new ImagickReader();
28+
if ($this->reader) {
29+
return $this->reader;
2330
}
24-
if (!$reader && extension_loaded('gd')) {
25-
$reader = new GD();
31+
32+
$reader = null;
33+
foreach ($this->backends as $backend) {
34+
if ($backend === GD::class) {
35+
if (extension_loaded('gd')) {
36+
$reader = new GD();
37+
38+
break;
39+
}
40+
}
41+
if ($backend === ImagickReader::class) {
42+
if (extension_loaded('imagick') && in_array('WMF', ImagickBase::queryformats())) {
43+
$reader = new ImagickReader();
44+
}
45+
46+
break;
47+
}
2648
}
49+
2750
$this->reader = $reader;
51+
52+
return $this->reader;
2853
}
2954

3055
public function load(string $filename): bool
3156
{
32-
return $this->reader->load($filename);
57+
return $this->getBackend()->load($filename);
3358
}
3459

3560
public function loadFromString(string $content): bool
3661
{
37-
return $this->reader->loadFromString($content);
62+
return $this->getBackend()->loadFromString($content);
3863
}
3964

4065
public function save(string $filename, string $format): bool
4166
{
42-
return $this->reader->save($filename, $format);
67+
return $this->getBackend()->save($filename, $format);
4368
}
4469

4570
public function getMediaType(): string
4671
{
47-
return $this->reader->getMediaType();
72+
return $this->getBackend()->getMediaType();
4873
}
4974

50-
public function isWMF(string $filename): bool
75+
public function isWMF(): bool
5176
{
52-
return $this->reader->isWMF($filename);
77+
return $this->getBackend()->isWMF();
5378
}
5479

5580
/**
@@ -59,6 +84,29 @@ public function isWMF(string $filename): bool
5984
*/
6085
public function getResource()
6186
{
62-
return $this->reader->getResource();
87+
return $this->getBackend()->getResource();
88+
}
89+
90+
/**
91+
* @return array<string>
92+
*/
93+
public function getBackends(): array
94+
{
95+
return $this->backends;
96+
}
97+
98+
/**
99+
* @param array<string> $backends
100+
*/
101+
public function setBackends(array $backends): self
102+
{
103+
$this->backends = [];
104+
foreach ($backends as $backend) {
105+
if (is_a($backend, ReaderInterface::class, true)) {
106+
$this->backends[] = $backend;
107+
}
108+
}
109+
110+
return $this;
63111
}
64112
}

src/WMF/Reader/WMF/ReaderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface ReaderInterface extends ReaderInterfaceBase
88
{
9-
public function isWMF(string $filename): bool;
9+
public function isWMF(): bool;
1010
}

tests/WMF/Reader/AbstractTestReader.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,43 @@ public static function dataProviderFilesWMFNotImplemented(): array
5555
],
5656
];
5757
}
58+
59+
/**
60+
* @return array<array<string>>
61+
*/
62+
public static function dataProviderMediaType(): array
63+
{
64+
return [
65+
[
66+
'gif',
67+
'image/gif',
68+
],
69+
[
70+
'jpg',
71+
'image/jpeg',
72+
],
73+
[
74+
'jpeg',
75+
'image/jpeg',
76+
],
77+
[
78+
'png',
79+
'image/png',
80+
],
81+
[
82+
'webp',
83+
'image/webp',
84+
],
85+
[
86+
'wbmp',
87+
'image/vnd.wap.wbmp',
88+
],
89+
];
90+
}
91+
92+
public function assertMimeType(string $filename, string $expectedMimeType): void
93+
{
94+
$gdInfo = getimagesize($filename);
95+
$this->assertEquals($expectedMimeType, $gdInfo['mime']);
96+
}
5897
}

0 commit comments

Comments
 (0)