|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_images; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_images\Interfaces\IIMTranslations; |
|
7
|
|
|
use kalanis\kw_images\Interfaces\ISizes; |
|
8
|
|
|
use kalanis\kw_images\Traits\TSizes; |
|
9
|
|
|
use kalanis\kw_images\Traits\TType; |
|
10
|
|
|
use kalanis\kw_mime\Interfaces\IMime; |
|
11
|
|
|
use kalanis\kw_mime\MimeException; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Graphics |
|
16
|
|
|
* Main image itself |
|
17
|
|
|
* @package kalanis\kw_images\Graphics |
|
18
|
|
|
*/ |
|
19
|
|
|
class Graphics |
|
20
|
|
|
{ |
|
21
|
|
|
use TType; |
|
22
|
|
|
use TSizes; |
|
23
|
|
|
|
|
24
|
|
|
protected Graphics\Processor $libGraphics; |
|
25
|
|
|
protected ?ISizes $libSizes = null; |
|
26
|
|
|
|
|
27
|
33 |
|
public function __construct(Graphics\Processor $libGraphics, IMime $libMime, ?IIMTranslations $lang = null) |
|
28
|
|
|
{ |
|
29
|
33 |
|
$this->initType($libMime, $lang); |
|
30
|
33 |
|
$this->libGraphics = $libGraphics; |
|
31
|
33 |
|
} |
|
32
|
|
|
|
|
33
|
16 |
|
public function setSizes(ISizes $sizes): self |
|
34
|
|
|
{ |
|
35
|
16 |
|
$this->libSizes = $sizes; |
|
36
|
16 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $tempPath path to temp file |
|
41
|
|
|
* @param string[] $realSourceName real file name for extension detection of source image |
|
42
|
|
|
* @throws ImagesException |
|
43
|
|
|
* @throws MimeException |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
8 |
|
public function check(string $tempPath, array $realSourceName): bool |
|
47
|
|
|
{ |
|
48
|
8 |
|
$this->getLibSizes(); |
|
49
|
6 |
|
$size = @filesize($tempPath); |
|
50
|
6 |
|
if (false === $size) { |
|
51
|
1 |
|
throw new ImagesException($this->getImLang()->imImageSizeExists()); |
|
52
|
|
|
} |
|
53
|
5 |
|
if ($this->getLibSizes()->getMaxFileSize() < $size) { |
|
54
|
1 |
|
throw new ImagesException($this->getImLang()->imImageSizeTooLarge()); |
|
55
|
|
|
} |
|
56
|
4 |
|
$this->libGraphics->load($this->getType($realSourceName), $tempPath); |
|
57
|
4 |
|
if ($this->getLibSizes()->getMaxInHeight() < $this->libGraphics->height()) { |
|
58
|
1 |
|
throw new ImagesException($this->getImLang()->imImageSizeTooLarge()); |
|
59
|
|
|
} |
|
60
|
3 |
|
if ($this->getLibSizes()->getMaxInWidth() < $this->libGraphics->width()) { |
|
61
|
1 |
|
throw new ImagesException($this->getImLang()->imImageSizeTooLarge()); |
|
62
|
|
|
} |
|
63
|
2 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $tempPath path to temp file which will be loaded and saved |
|
68
|
|
|
* @param string[] $realSourceName real file name for extension detection of source image |
|
69
|
|
|
* @param string[]|null $realTargetName real file name for extension detection of target image |
|
70
|
|
|
* @throws ImagesException |
|
71
|
|
|
* @throws MimeException |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
11 |
|
public function resize(string $tempPath, array $realSourceName, ?array $realTargetName = null): bool |
|
75
|
|
|
{ |
|
76
|
11 |
|
$this->getLibSizes(); |
|
77
|
10 |
|
$realTargetName = is_null($realTargetName) ? $realSourceName : $realTargetName; |
|
78
|
10 |
|
$this->libGraphics->load($this->getType($realSourceName), $tempPath); |
|
79
|
8 |
|
$sizes = $this->calculateSize( |
|
80
|
8 |
|
$this->libGraphics->width(), |
|
81
|
8 |
|
$this->getLibSizes()->getMaxStoreWidth(), |
|
82
|
8 |
|
$this->libGraphics->height(), |
|
83
|
8 |
|
$this->getLibSizes()->getMaxStoreHeight() |
|
84
|
|
|
); |
|
85
|
8 |
|
$this->libGraphics->resample($sizes['width'], $sizes['height']); |
|
86
|
8 |
|
$this->libGraphics->save($this->getType($realTargetName), $tempPath); |
|
87
|
8 |
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param float|null $angle positive to rotate clockwise, negative counter-clockwise |
|
92
|
|
|
* @param int|null $flipMode |
|
93
|
|
|
* @param string $tempPath path to temp file which will be loaded and saved |
|
94
|
|
|
* @param string[] $realSourceName real file name for extension detection of source image |
|
95
|
|
|
* @param string[]|null $realTargetName real file name for extension detection of target image |
|
96
|
|
|
* @throws ImagesException |
|
97
|
|
|
* @throws MimeException |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
4 |
|
public function rotate(?float $angle, ?int $flipMode, string $tempPath, array $realSourceName, ?array $realTargetName = null): bool |
|
101
|
|
|
{ |
|
102
|
4 |
|
if (is_null($angle) && is_null($flipMode)) { |
|
103
|
1 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
$realTargetName = is_null($realTargetName) ? $realSourceName : $realTargetName; |
|
107
|
3 |
|
$this->libGraphics->load($this->getType($realSourceName), $tempPath); |
|
108
|
2 |
|
if (!empty($angle)) { |
|
109
|
1 |
|
$this->libGraphics->rotate($angle); |
|
110
|
|
|
} |
|
111
|
2 |
|
if (!is_null($flipMode)) { |
|
112
|
1 |
|
$this->libGraphics->flip($flipMode); |
|
113
|
|
|
} |
|
114
|
2 |
|
$this->libGraphics->save($this->getType($realTargetName), $tempPath); |
|
115
|
2 |
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @throws ImagesException |
|
120
|
|
|
* @return ISizes |
|
121
|
|
|
*/ |
|
122
|
18 |
|
protected function getLibSizes(): ISizes |
|
123
|
|
|
{ |
|
124
|
18 |
|
if (empty($this->libSizes)) { |
|
125
|
3 |
|
throw new ImagesException($this->getImLang()->imSizesNotSet()); |
|
126
|
|
|
} |
|
127
|
15 |
|
return $this->libSizes; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|