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