1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Compolomus\Compomage; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Image |
10
|
|
|
* @package Compolomus\Compomage |
11
|
|
|
* @method Image save(string $filename): bool |
12
|
|
|
* @method Image __toString(): string |
13
|
|
|
* @method Image getBase64(): string |
14
|
|
|
* @method Image resize(int $width, int $height): ImageInterface |
15
|
|
|
* @method Image resizeByHeight(int $height): ImageInterface |
16
|
|
|
* @method Image resizeByWidth(int $width): ImageInterface |
17
|
|
|
* @method Image resizeByPercent(int $percent): ImageInterface |
18
|
|
|
* @method Image resizeBy(string $mode, int $param): ImageInterface |
19
|
|
|
* @method Image resizeByTransparentBackground(int $width, int $height): ImageInterface |
20
|
|
|
* @method Image resizeByBlurBackground(int $width, int $height): ImageInterface |
21
|
|
|
* @method Image crop(int $width, int $height, int $x, int $y): ImageInterface |
22
|
|
|
* @method Image rotate(int $angle = 90): ImageInterface |
23
|
|
|
* @method Image flip(): ImageInterface |
24
|
|
|
* @method Image flop(): ImageInterface |
25
|
|
|
* @method Image brightness(int $level): ImageInterface |
26
|
|
|
* @method Image contrast(int $level): ImageInterface |
27
|
|
|
* @method Image negate(): ImageInterface |
28
|
|
|
* @method Image blur(): ImageInterface |
29
|
|
|
* @method Image grayscale(): ImageInterface |
30
|
|
|
* @method Image getImage() |
31
|
|
|
* @method Image getWidth(): int |
32
|
|
|
* @method Image getHeight(): int |
33
|
|
|
* @method Image watermark($watermark, string $position): ImageInterface |
34
|
|
|
* @method Image thumbnail(int $width, int $height): ImageInterface |
35
|
|
|
* @method Image evaluateImage(int $op, float $constant, int $channel = \Imagick::CHANNEL_DEFAULT) // bug |
36
|
|
|
* @method Image getFontsList(): array |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
class Image |
40
|
|
|
{ |
41
|
|
|
public const AUTO = 0; |
42
|
|
|
|
43
|
|
|
public const GD = 1; |
44
|
|
|
|
45
|
|
|
public const IMAGICK = 2; |
46
|
|
|
|
47
|
|
|
private $class = self::GD; |
48
|
|
|
|
49
|
|
|
private $object; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Image constructor. |
53
|
|
|
* @param string $filename |
54
|
|
|
* @param int $mode |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
17 |
|
public function __construct(string $filename, $mode = self::AUTO) |
58
|
|
|
{ |
59
|
17 |
|
$this->check($filename, $mode); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $filename |
64
|
|
|
* @param int $mode |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
17 |
|
private function check(string $filename, $mode = self::AUTO): void |
68
|
|
|
{ |
69
|
17 |
|
if ($mode === self::IMAGICK || ($mode === self::AUTO && extension_loaded('imagick') === true)) { |
70
|
13 |
|
$this->class = self::IMAGICK; |
71
|
13 |
|
$this->object = new Imagick2($filename); |
72
|
13 |
|
return; |
73
|
|
|
} |
74
|
14 |
|
$this->object = new GD($filename); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $method |
79
|
|
|
* @param $args |
80
|
|
|
* @return mixed |
81
|
|
|
* @throws InvalidArgumentException |
82
|
|
|
*/ |
83
|
14 |
|
public function __call(string $method, $args) |
84
|
|
|
{ |
85
|
14 |
|
if (!method_exists($this->object, $method)) { |
86
|
1 |
|
throw new InvalidArgumentException('Undefined method ' . $method); |
87
|
|
|
} |
88
|
13 |
|
return $this->object->$method(...$args); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|