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

WMF\Reader\WMF\Magic : Backends sorted by priority #6

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
composer.lock
phpDocumentor.phar

build/
public/
vendor/
14 changes: 14 additions & 0 deletions docs/changes/0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 0.1.2

## Enhancements

- WMF\Reader\WMF\Magic : Backends sorted by priority in [#6](https://github.com/PHPOffice/WMF/pull/6) by [@Progi1984](https://github/Progi1984)

## Bug fixes

- N/A

## Miscellaneous

- N/A

74 changes: 62 additions & 12 deletions docs/usage/wmf.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ You can load `.wmf` files.
You can one of two current backends : `gd` or `imagick`.
If you don't know which one used, you can use the magic one.

By default, the order of the backends is Imagick, followed by GD.
Each backend is tested on different criteria: extension loaded, format support.

```php
<?php

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

For next sample, I will use the magic one.
For next samples, I will use the magic one.

### `getBackends`

This specific method for `Magic::class` returns backends sorted by priority.

```php
<?php

use PhpOffice\WMF\Reader\WMF\Magic;

$reader = new Magic();

var_dump($reader->getBackends());
```

### `setBackends`

This specific method for `Magic::class` defines backends sorted by priority.

```php
<?php

use PhpOffice\WMF\Reader\WMF\GD;
use PhpOffice\WMF\Reader\WMF\Imagick;
use PhpOffice\WMF\Reader\WMF\Magic;

$reader = new Magic();
$reader->setBackends([
GD::class,
Imagick::class,
]);

var_dump($reader->getBackends());
```

## Methods

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

$reader = new Magic();
$reader->load('sample.wmf');

$wmf = $reader->load('sample.wmf');

var_dump($wmf->getResource());
var_dump($reader->getResource());
```

### `getMediaType`

The method returns the media type for a WMF file
The method returns the media type for a WMF file.

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

### `isWMF`

The method allows to know if the file is supported by the library.
The method returns if the file is supported by the library.

```php
<?php

use PhpOffice\WMF\Reader\WMF\Magic;

$reader = new Magic();
$reader->load('sample.wmf');

$isWMF = $reader->isWMF('sample.wmf');
$isWMF = $reader->isWMF();

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

### `load`

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

```php
<?php

use PhpOffice\WMF\Reader\WMF\Magic;

$reader = new Magic();
$reader->load('sample.wmf');
```

$wmf = $reader->load('sample.wmf');
### `loadFromString`

The method loads a WMF file in the object from a string.
The method returns `true` if the file has been correctly loaded, or `false` if it has not.

```php
<?php

use PhpOffice\WMF\Reader\WMF\Magic;

$reader = new Magic();
$reader->loadFromString(file_get_contents('sample.wmf'));
```

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

$reader = new Magic();

$wmf = $reader->load('sample.wmf');
$wmf->save('sample.png', 'png');
$reader->load('sample.wmf');
$reader->save('sample.png', 'png');
```
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.1 (WIP)': 'changes/0.1.1.md'
- '0.1.2 (WIP)': 'changes/0.1.2.md'
- '0.1.1': 'changes/0.1.1.md'
- '0.1.0': 'changes/0.1.0.md'
- Developers:
- 'Coveralls': 'https://coveralls.io/github/PHPOffice/WMF'
Expand Down
8 changes: 6 additions & 2 deletions src/WMF/Reader/WMF/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function __destruct()
}
}

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

return $key == (int) 0x9AC6CDD7;
}
Expand All @@ -91,6 +91,10 @@ public function loadFromString(string $content): bool
*/
private function loadContent(): bool
{
if (!$this->isWMF()) {
return false;
}

$this->pos = 0;
$this->gdiObjects = [];
$k = 72 / 25.4;
Expand Down
7 changes: 2 additions & 5 deletions src/WMF/Reader/WMF/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ private function loadContent(string $content, bool $isBlob): bool
}
}

public function isWMF(string $filename): bool
public function isWMF(): bool
{
$im = new ImagickBase();
$im->readImage($filename);

return $im->getImageFormat() === 'WMF';
return $this->im->getImageFormat() === 'WMF';
}

public function getResource(): ImagickBase
Expand Down
76 changes: 62 additions & 14 deletions src/WMF/Reader/WMF/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,70 @@
class Magic extends ReaderAbstract
{
/**
* @var ReaderInterface
* @var array<string>
*/
protected $backends = [
ImagickReader::class,
GD::class,
];

/**
* @var ?ReaderInterface
*/
protected $reader;

public function __construct()
protected function getBackend(): ?ReaderInterface
{
$reader = null;
if (extension_loaded('imagick') && in_array('WMF', ImagickBase::queryformats())) {
$reader = new ImagickReader();
if ($this->reader) {
return $this->reader;
}
if (!$reader && extension_loaded('gd')) {
$reader = new GD();

$reader = null;
foreach ($this->backends as $backend) {
if ($backend === GD::class) {
if (extension_loaded('gd')) {
$reader = new GD();

break;
}
}
if ($backend === ImagickReader::class) {
if (extension_loaded('imagick') && in_array('WMF', ImagickBase::queryformats())) {
$reader = new ImagickReader();
}

break;
}
}

$this->reader = $reader;

return $this->reader;
}

public function load(string $filename): bool
{
return $this->reader->load($filename);
return $this->getBackend()->load($filename);
}

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

public function save(string $filename, string $format): bool
{
return $this->reader->save($filename, $format);
return $this->getBackend()->save($filename, $format);
}

public function getMediaType(): string
{
return $this->reader->getMediaType();
return $this->getBackend()->getMediaType();
}

public function isWMF(string $filename): bool
public function isWMF(): bool
{
return $this->reader->isWMF($filename);
return $this->getBackend()->isWMF();
}

/**
Expand All @@ -59,6 +84,29 @@ public function isWMF(string $filename): bool
*/
public function getResource()
{
return $this->reader->getResource();
return $this->getBackend()->getResource();
}

/**
* @return array<string>
*/
public function getBackends(): array
{
return $this->backends;
}

/**
* @param array<string> $backends
*/
public function setBackends(array $backends): self
{
$this->backends = [];
foreach ($backends as $backend) {
if (is_a($backend, ReaderInterface::class, true)) {
$this->backends[] = $backend;
}
}

return $this;
}
}
2 changes: 1 addition & 1 deletion src/WMF/Reader/WMF/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface ReaderInterface extends ReaderInterfaceBase
{
public function isWMF(string $filename): bool;
public function isWMF(): bool;
}
39 changes: 39 additions & 0 deletions tests/WMF/Reader/AbstractTestReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,43 @@ public static function dataProviderFilesWMFNotImplemented(): array
],
];
}

/**
* @return array<array<string>>
*/
public static function dataProviderMediaType(): array
{
return [
[
'gif',
'image/gif',
],
[
'jpg',
'image/jpeg',
],
[
'jpeg',
'image/jpeg',
],
[
'png',
'image/png',
],
[
'webp',
'image/webp',
],
[
'wbmp',
'image/vnd.wap.wbmp',
],
];
}

public function assertMimeType(string $filename, string $expectedMimeType): void
{
$gdInfo = getimagesize($filename);
$this->assertEquals($expectedMimeType, $gdInfo['mime']);
}
}
Loading
Loading