1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Magickly project. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Magickly\Imagick; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Magickly\Exception\RuntimeException; |
11
|
|
|
use GravityMedia\Magickly\MagicklyInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The Magickly class. |
15
|
|
|
* |
16
|
|
|
* @package GravityMedia\Magickly\Imagick |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
class Magickly implements MagicklyInterface |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $version; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create Magickly object. |
27
|
|
|
* |
28
|
|
|
* @throws RuntimeException |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct() |
31
|
|
|
{ |
32
|
4 |
|
if (!class_exists('Imagick')) { |
33
|
|
|
throw new RuntimeException('Imagick not installed'); |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
$version = $this->getVersion(); |
37
|
4 |
|
if (version_compare('6.2.9', $version) > 0) { |
38
|
|
|
throw new RuntimeException(sprintf('ImageMagick version 6.2.9 or higher is required, %s provided', $version)); |
39
|
|
|
} |
40
|
4 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get version. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
4 |
|
public function getVersion() |
48
|
|
|
{ |
49
|
4 |
|
if (null === self::$version) { |
50
|
2 |
|
$imagick = new \Imagick(); |
51
|
2 |
|
$version = $imagick->getVersion(); |
52
|
|
|
|
53
|
2 |
|
list(self::$version) = sscanf($version['versionString'], 'ImageMagick %s %04d-%02d-%02d %s %s'); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
4 |
|
return self::$version; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
2 |
|
public function open($path) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
2 |
|
$imagick = new \Imagick($path); |
66
|
1 |
|
} catch (\ImagickException $exception) { |
67
|
|
|
throw new RuntimeException(sprintf('Unable to open image %s', $path), 0, $exception); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
return new Image($imagick); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.