|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Compolomus\Compomage; |
|
4
|
|
|
|
|
5
|
|
|
use Compolomus\Compomage\Interfaces\ImageInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Imagick extends AbstractImage implements ImageInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Imagick constructor. |
|
11
|
|
|
* @param string $image |
|
12
|
|
|
* @throws \Exception |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct(string $image) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->init($image); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $source |
|
21
|
|
|
* @return ImageInterface |
|
22
|
|
|
* @throws \Exception |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function tmp(string $source): ImageInterface |
|
25
|
|
|
{ |
|
26
|
|
|
$image = new \Imagick; |
|
27
|
|
|
if ($image->readImageBlob($source)) { |
|
28
|
|
|
if ($image->getImageAlphaChannel() !== \Imagick::ALPHACHANNEL_ACTIVATE) { |
|
29
|
|
|
$image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SET); // 8 |
|
30
|
|
|
#$image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE); // 6 |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
$background = new \Imagick; |
|
34
|
|
|
$background->newImage($image->getImageWidth(), $image->getImageHeight(), new \ImagickPixel('transparent')); |
|
35
|
|
|
$background->setImageBackgroundColor(new \ImagickPixel('transparent')); |
|
36
|
|
|
$image->compositeImage($background, \imagick::COMPOSITE_OVER, 0, 0); //Imagick::COMPOSITE_DISSOLVE |
|
37
|
|
|
$this->setImage($image); |
|
38
|
|
|
$this->getImage()->setFormat('png'); // save transparent |
|
39
|
|
|
$this->setSizes(); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function setSizes(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$args = $this->getImage()->getImageGeometry(); |
|
47
|
|
|
$this->setWidth($args['width']); |
|
48
|
|
|
$this->setHeight($args['height']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function newImage(int $width, int $height) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$background = new \Imagick; |
|
54
|
|
|
$background->newImage($image->getImageWidth(), $image->getImageHeight(), new \ImagickPixel('transparent')); |
|
|
|
|
|
|
55
|
|
|
$background->setImageBackgroundColor(new \ImagickPixel('transparent')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function resize(int $width, int $height): ImageInterface |
|
59
|
|
|
{ |
|
60
|
|
|
$this->getImage()->scaleImage($width, $height, false); |
|
61
|
|
|
$this->setSizes(); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function rotate(int $angle = 90): ImageInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$this->image->rotateImage(new \ImagickPixel('transparent'), $angle); |
|
69
|
|
|
$this->setSizes(); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function watermark(): ImageInterface |
|
75
|
|
|
{ |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function flip(): ImageInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$this->getImage()->flipImage(); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function flop(): ImageInterface |
|
87
|
|
|
{ |
|
88
|
|
|
$this->getImage()->flopImage(); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function grayscale(): ImageInterface |
|
94
|
|
|
{ |
|
95
|
|
|
$this->getImage()->modulateImage(100, 0, 100); |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function crop(int $width, int $height, int $startX, int $startY): ImageInterface |
|
101
|
|
|
{ |
|
102
|
|
|
$this->getImage()->cropImage($width, $height, $startX, $startY); |
|
103
|
|
|
$this->setSizes(); |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function save(string $filename): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function __toString(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->getImage()->getImageBlob(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.