1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Image\Adapter\Imagick; |
4
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\API\Image\ImageInterface; |
6
|
|
|
use AmaTeam\Image\Projection\Image\EncodingOptions; |
7
|
|
|
use Imagick; |
8
|
|
|
use ImagickDraw; |
9
|
|
|
|
10
|
|
|
class Image implements ImageInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Imagick |
14
|
|
|
*/ |
15
|
|
|
private $resource; |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $width; |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $height; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Imagick $resource |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Imagick $resource) |
29
|
|
|
{ |
30
|
|
|
$this->resource = $resource; |
31
|
|
|
$this->width = $resource->getImageWidth(); |
32
|
|
|
$this->height = $resource->getImageHeight(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function getWidth() |
39
|
|
|
{ |
40
|
|
|
return $this->width; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function getHeight() |
47
|
|
|
{ |
48
|
|
|
return $this->height; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function getColorAt($x, $y) |
55
|
|
|
{ |
56
|
|
|
return Color::get($this->resource->getImagePixelColor($x, $y)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function setColorAt($x, $y, $color) |
63
|
|
|
{ |
64
|
|
|
$pixel = $this->resource->getImagePixelColor($x, $y); |
65
|
|
|
Color::set($pixel, $color); |
66
|
|
|
$drawer = new ImagickDraw(); |
67
|
|
|
$drawer->setFillColor($pixel); |
68
|
|
|
$drawer->point($x, $y); |
69
|
|
|
$this->resource->drawImage($drawer); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function getBinary($format, EncodingOptions $options = null) |
76
|
|
|
{ |
77
|
|
|
$options = $options ?: EncodingOptions::defaults(); |
78
|
|
|
$quality = $options->getQuality(); |
79
|
|
|
$compression = $options->getCompression(); |
80
|
|
|
$this->resource->setImageFormat($format); |
81
|
|
|
$this->resource->setImageCompression((int) ($compression * 10)); |
82
|
|
|
$this->resource->setImageCompressionQuality((int) ($quality * 100)); |
83
|
|
|
return $this->resource->getImageBlob(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function getResource() |
90
|
|
|
{ |
91
|
|
|
return $this->resource; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function __destruct() |
98
|
|
|
{ |
99
|
|
|
$this->resource->clear(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|